Skip to content

Commit a701247

Browse files
committed
appease typescript
1 parent 3fc9142 commit a701247

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

.prettierrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"useTabs": true,
3+
"singleQuote": true,
4+
"trailingComma": "none"
5+
}

src/index.js

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ const join = (strings) => {
2323
for (let i = 1; i < strings.length; i += 1) {
2424
str += `_${id}_${i - 1}_${strings[i]}`;
2525
}
26-
return str.replace(/([@#])(\w+)/g, (_m, sigil, name) => `_${id}_${sigils[sigil]}_${name}`);
26+
return str.replace(
27+
/([@#])(\w+)/g,
28+
(_m, sigil, name) => `_${id}_${sigils[sigil]}_${name}`
29+
);
2730
};
2831

2932
/**
@@ -56,8 +59,10 @@ const flatten_body = (array, target) => {
5659
continue;
5760
}
5861

59-
if (statement.leadingComments) statement.expression.leadingComments = statement.leadingComments;
60-
if (statement.trailingComments) statement.expression.trailingComments = statement.trailingComments;
62+
if (statement.leadingComments)
63+
statement.expression.leadingComments = statement.leadingComments;
64+
if (statement.trailingComments)
65+
statement.expression.trailingComments = statement.trailingComments;
6166

6267
target.push(statement.expression);
6368
continue;
@@ -67,7 +72,7 @@ const flatten_body = (array, target) => {
6772
}
6873

6974
return target;
70-
}
75+
};
7176

7277
/**
7378
* @param {any[]} array
@@ -88,7 +93,7 @@ const flatten_properties = (array, target) => {
8893
}
8994

9095
return target;
91-
}
96+
};
9297

9398
/**
9499
* @param {any[]} nodes
@@ -109,7 +114,7 @@ const flatten = (nodes, target) => {
109114
}
110115

111116
return target;
112-
}
117+
};
113118

114119
const EMPTY = { type: 'Empty' };
115120

@@ -138,16 +143,19 @@ const acorn_opts = (comments, raw) => {
138143
* @param {CommentWithLocation[]} comments
139144
*/
140145
const inject = (raw, node, values, comments) => {
141-
comments.forEach(comment => {
142-
comment.value = comment.value.replace(re, (m, i) => +i in values ? values[+i] : m);
146+
comments.forEach((comment) => {
147+
comment.value = comment.value.replace(re, (m, i) =>
148+
+i in values ? values[+i] : m
149+
);
143150
});
144151

145152
const { enter, leave } = get_comment_handlers(comments, raw);
146153

147154
walk(node, {
148155
enter,
149156

150-
leave(node, parent, key, index) {
157+
/** @param {any} node */
158+
leave(node) {
151159
if (node.type === 'Identifier') {
152160
re.lastIndex = 0;
153161
const match = re.exec(node.name);
@@ -158,9 +166,19 @@ const inject = (raw, node, values, comments) => {
158166
let value = values[+match[1]];
159167

160168
if (typeof value === 'string') {
161-
value = { type: 'Identifier', name: value, leadingComments: node.leadingComments, trailingComments: node.trailingComments };
169+
value = {
170+
type: 'Identifier',
171+
name: value,
172+
leadingComments: node.leadingComments,
173+
trailingComments: node.trailingComments
174+
};
162175
} else if (typeof value === 'number') {
163-
value = { type: 'Literal', value, leadingComments: node.leadingComments, trailingComments: node.trailingComments };
176+
value = {
177+
type: 'Literal',
178+
value,
179+
leadingComments: node.leadingComments,
180+
trailingComments: node.trailingComments
181+
};
164182
}
165183

166184
this.replace(value || EMPTY);
@@ -174,19 +192,28 @@ const inject = (raw, node, values, comments) => {
174192
if (node.type === 'Literal') {
175193
if (typeof node.value === 'string') {
176194
re.lastIndex = 0;
177-
const new_value = node.value.replace(re, (m, i) => +i in values ? values[+i] : m);
195+
const new_value = /** @type {string} */ (node.value).replace(
196+
re,
197+
(m, i) => (+i in values ? values[+i] : m)
198+
);
178199
const has_changed = new_value !== node.value;
179200
node.value = new_value;
180201
if (has_changed && node.raw) {
181202
// preserve the quotes
182-
node.raw = `${node.raw[0]}${JSON.stringify(node.value).slice(1, -1)}${node.raw[node.raw.length - 1]}`;
203+
node.raw = `${node.raw[0]}${JSON.stringify(node.value).slice(
204+
1,
205+
-1
206+
)}${node.raw[node.raw.length - 1]}`;
183207
}
184208
}
185209
}
186210

187211
if (node.type === 'TemplateElement') {
188212
re.lastIndex = 0;
189-
node.value.raw = node.value.raw.replace(re, (m, i) => +i in values ? values[+i] : m);
213+
node.value.raw = /** @type {string} */ (node.value.raw).replace(
214+
re,
215+
(m, i) => (+i in values ? values[+i] : m)
216+
);
190217
}
191218

192219
if (node.type === 'Program' || node.type === 'BlockStatement') {
@@ -201,15 +228,22 @@ const inject = (raw, node, values, comments) => {
201228
node.elements = flatten(node.elements, []);
202229
}
203230

204-
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression') {
231+
if (
232+
node.type === 'FunctionExpression' ||
233+
node.type === 'FunctionDeclaration' ||
234+
node.type === 'ArrowFunctionExpression'
235+
) {
205236
node.params = flatten(node.params, []);
206237
}
207238

208239
if (node.type === 'CallExpression' || node.type === 'NewExpression') {
209240
node.arguments = flatten(node.arguments, []);
210241
}
211242

212-
if (node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration') {
243+
if (
244+
node.type === 'ImportDeclaration' ||
245+
node.type === 'ExportNamedDeclaration'
246+
) {
213247
node.specifiers = flatten(node.specifiers, []);
214248
}
215249

@@ -223,7 +257,7 @@ const inject = (raw, node, values, comments) => {
223257
leave(node);
224258
}
225259
});
226-
}
260+
};
227261

228262
/**
229263
*
@@ -238,7 +272,9 @@ export function b(strings, ...values) {
238272
const comments = [];
239273

240274
try {
241-
const ast = /** @type {any} */ (acorn.parse(str, acorn_opts(comments, str)));
275+
const ast = /** @type {any} */ (
276+
acorn.parse(str, acorn_opts(comments, str))
277+
);
242278

243279
inject(str, ast, values, comments);
244280

@@ -261,7 +297,10 @@ export function x(strings, ...values) {
261297
const comments = [];
262298

263299
try {
264-
const expression = /** @type {Expression & { start: Number, end: number }} */ (acorn.parseExpressionAt(str, 0, acorn_opts(comments, str)));
300+
const expression =
301+
/** @type {Expression & { start: Number, end: number }} */ (
302+
acorn.parseExpressionAt(str, 0, acorn_opts(comments, str))
303+
);
265304
const match = /\S+/.exec(str.slice(expression.end));
266305
if (match) {
267306
throw new Error(`Unexpected token '${match[0]}'`);
@@ -288,7 +327,9 @@ export function p(strings, ...values) {
288327
const comments = [];
289328

290329
try {
291-
const expression = /** @type {any} */ (acorn.parseExpressionAt(str, 0, acorn_opts(comments, str)));
330+
const expression = /** @type {any} */ (
331+
acorn.parseExpressionAt(str, 0, acorn_opts(comments, str))
332+
);
292333

293334
inject(str, expression, values, comments);
294335

@@ -342,7 +383,9 @@ export const parseExpressionAt = (source, index, opts) => {
342383
/** @type {CommentWithLocation[]} */
343384
const comments = [];
344385
const { onComment, enter, leave } = get_comment_handlers(comments, source);
345-
const ast = /** @type {any} */ (acorn.parseExpressionAt(source, index, { onComment, ...opts }));
386+
const ast = /** @type {any} */ (
387+
acorn.parseExpressionAt(source, index, { onComment, ...opts })
388+
);
346389
walk(ast, { enter, leave });
347390
return ast;
348391
};

0 commit comments

Comments
 (0)