@@ -23,7 +23,10 @@ const join = (strings) => {
23
23
for ( let i = 1 ; i < strings . length ; i += 1 ) {
24
24
str += `_${ id } _${ i - 1 } _${ strings [ i ] } ` ;
25
25
}
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
+ ) ;
27
30
} ;
28
31
29
32
/**
@@ -56,8 +59,10 @@ const flatten_body = (array, target) => {
56
59
continue ;
57
60
}
58
61
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 ;
61
66
62
67
target . push ( statement . expression ) ;
63
68
continue ;
@@ -67,7 +72,7 @@ const flatten_body = (array, target) => {
67
72
}
68
73
69
74
return target ;
70
- }
75
+ } ;
71
76
72
77
/**
73
78
* @param {any[] } array
@@ -88,7 +93,7 @@ const flatten_properties = (array, target) => {
88
93
}
89
94
90
95
return target ;
91
- }
96
+ } ;
92
97
93
98
/**
94
99
* @param {any[] } nodes
@@ -109,7 +114,7 @@ const flatten = (nodes, target) => {
109
114
}
110
115
111
116
return target ;
112
- }
117
+ } ;
113
118
114
119
const EMPTY = { type : 'Empty' } ;
115
120
@@ -138,16 +143,19 @@ const acorn_opts = (comments, raw) => {
138
143
* @param {CommentWithLocation[] } comments
139
144
*/
140
145
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
+ ) ;
143
150
} ) ;
144
151
145
152
const { enter, leave } = get_comment_handlers ( comments , raw ) ;
146
153
147
154
walk ( node , {
148
155
enter,
149
156
150
- leave ( node , parent , key , index ) {
157
+ /** @param {any } node */
158
+ leave ( node ) {
151
159
if ( node . type === 'Identifier' ) {
152
160
re . lastIndex = 0 ;
153
161
const match = re . exec ( node . name ) ;
@@ -158,9 +166,19 @@ const inject = (raw, node, values, comments) => {
158
166
let value = values [ + match [ 1 ] ] ;
159
167
160
168
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
+ } ;
162
175
} 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
+ } ;
164
182
}
165
183
166
184
this . replace ( value || EMPTY ) ;
@@ -174,19 +192,28 @@ const inject = (raw, node, values, comments) => {
174
192
if ( node . type === 'Literal' ) {
175
193
if ( typeof node . value === 'string' ) {
176
194
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
+ ) ;
178
199
const has_changed = new_value !== node . value ;
179
200
node . value = new_value ;
180
201
if ( has_changed && node . raw ) {
181
202
// 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 ] } `;
183
207
}
184
208
}
185
209
}
186
210
187
211
if ( node . type === 'TemplateElement' ) {
188
212
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
+ ) ;
190
217
}
191
218
192
219
if ( node . type === 'Program' || node . type === 'BlockStatement' ) {
@@ -201,15 +228,22 @@ const inject = (raw, node, values, comments) => {
201
228
node . elements = flatten ( node . elements , [ ] ) ;
202
229
}
203
230
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
+ ) {
205
236
node . params = flatten ( node . params , [ ] ) ;
206
237
}
207
238
208
239
if ( node . type === 'CallExpression' || node . type === 'NewExpression' ) {
209
240
node . arguments = flatten ( node . arguments , [ ] ) ;
210
241
}
211
242
212
- if ( node . type === 'ImportDeclaration' || node . type === 'ExportNamedDeclaration' ) {
243
+ if (
244
+ node . type === 'ImportDeclaration' ||
245
+ node . type === 'ExportNamedDeclaration'
246
+ ) {
213
247
node . specifiers = flatten ( node . specifiers , [ ] ) ;
214
248
}
215
249
@@ -223,7 +257,7 @@ const inject = (raw, node, values, comments) => {
223
257
leave ( node ) ;
224
258
}
225
259
} ) ;
226
- }
260
+ } ;
227
261
228
262
/**
229
263
*
@@ -238,7 +272,9 @@ export function b(strings, ...values) {
238
272
const comments = [ ] ;
239
273
240
274
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
+ ) ;
242
278
243
279
inject ( str , ast , values , comments ) ;
244
280
@@ -261,7 +297,10 @@ export function x(strings, ...values) {
261
297
const comments = [ ] ;
262
298
263
299
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
+ ) ;
265
304
const match = / \S + / . exec ( str . slice ( expression . end ) ) ;
266
305
if ( match ) {
267
306
throw new Error ( `Unexpected token '${ match [ 0 ] } '` ) ;
@@ -288,7 +327,9 @@ export function p(strings, ...values) {
288
327
const comments = [ ] ;
289
328
290
329
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
+ ) ;
292
333
293
334
inject ( str , expression , values , comments ) ;
294
335
@@ -342,7 +383,9 @@ export const parseExpressionAt = (source, index, opts) => {
342
383
/** @type {CommentWithLocation[] } */
343
384
const comments = [ ] ;
344
385
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
+ ) ;
346
389
walk ( ast , { enter, leave } ) ;
347
390
return ast ;
348
391
} ;
0 commit comments