1
1
import { walk } from 'estree-walker'
2
2
import { name as isIdentifierName } from 'estree-util-is-identifier-name'
3
3
4
- var regex = / @ ( j s x | j s x F r a g | j s x I m p o r t S o u r c e | j s x R u n t i m e ) \s + ( \S + ) / g
4
+ const regex = / @ ( j s x | j s x F r a g | j s x I m p o r t S o u r c e | j s x R u n t i m e ) \s + ( \S + ) / g
5
5
6
6
/**
7
7
* @typedef {import('estree-jsx').Node } Node
@@ -52,11 +52,11 @@ var regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
52
52
* @returns {T }
53
53
*/
54
54
export function buildJsx ( tree , options = { } ) {
55
- var automatic = options . runtime === 'automatic'
55
+ let automatic = options . runtime === 'automatic'
56
56
/** @type {Annotations } */
57
- var annotations = { }
57
+ const annotations = { }
58
58
/** @type {{fragment?: boolean, jsx?: boolean, jsxs?: boolean} } */
59
- var imports = { }
59
+ const imports = { }
60
60
61
61
walk ( tree , { enter, leave} )
62
62
@@ -70,20 +70,16 @@ export function buildJsx(tree, options = {}) {
70
70
* @param {Node } node
71
71
*/
72
72
function enter ( node ) {
73
- /** @type {Comment[] } */
74
- var comments
75
- /** @type {number } */
76
- var index
77
- /** @type {RegExpMatchArray } */
78
- var match
79
-
80
73
if ( node . type === 'Program' ) {
81
- comments = node . comments || [ ]
82
- index = - 1
74
+ const comments = node . comments || [ ]
75
+ let index = - 1
83
76
84
77
while ( ++ index < comments . length ) {
85
78
regex . lastIndex = 0
86
79
80
+ /** @type {RegExpMatchArray } */
81
+ let match
82
+
87
83
while ( ( match = regex . exec ( comments [ index ] . value ) ) ) {
88
84
annotations [ match [ 1 ] ] = match [ 2 ]
89
85
}
@@ -125,40 +121,9 @@ export function buildJsx(tree, options = {}) {
125
121
*/
126
122
// eslint-disable-next-line complexity
127
123
function leave ( node ) {
128
- /** @type {Array.<Expression|SpreadElement> } */
129
- var parameters = [ ]
130
- /** @type {Array.<Expression> } */
131
- var children = [ ]
132
- /** @type {Array.<Expression> } */
133
- var objects = [ ]
134
- /** @type {Array.<Property> } */
135
- var fields = [ ]
136
- var index = - 1
137
- /** @type {JSXExpressionContainer|JSXElement|JSXFragment|JSXText|JSXSpreadChild } */
138
- var child
139
- /** @type {MemberExpression|Literal|Identifier } */
140
- var name
141
- /** @type {Expression } */
142
- var props
143
- /** @type {Array<JSXAttribute | JSXSpreadAttribute> } */
144
- var attributes
145
- /** @type {JSXAttribute | JSXSpreadAttribute } */
146
- var attribute
147
- /** @type {boolean } */
148
- var spread
149
- /** @type {Expression } */
150
- var key
151
- /** @type {MemberExpression|Literal|Identifier } */
152
- var callee
153
- /** @type {Array<ImportSpecifier> } */
154
- var specifiers
155
- /** @type {Property } */
156
- var prop
157
- /** @type {string } */
158
- var value
159
-
160
124
if ( node . type === 'Program' ) {
161
- specifiers = [ ]
125
+ /** @type {Array<ImportSpecifier> } */
126
+ const specifiers = [ ]
162
127
163
128
if ( imports . fragment ) {
164
129
specifiers . push ( {
@@ -202,17 +167,21 @@ export function buildJsx(tree, options = {}) {
202
167
return
203
168
}
204
169
170
+ /** @type {Array.<Expression> } */
171
+ const children = [ ]
172
+ let index = - 1
173
+
205
174
// Figure out `children`.
206
175
while ( ++ index < node . children . length ) {
207
- child = node . children [ index ]
176
+ const child = node . children [ index ]
208
177
209
178
if ( child . type === 'JSXExpressionContainer' ) {
210
179
// Ignore empty expressions.
211
180
if ( child . expression . type !== 'JSXEmptyExpression' ) {
212
181
children . push ( child . expression )
213
182
}
214
183
} else if ( child . type === 'JSXText' ) {
215
- value = child . value
184
+ const value = child . value
216
185
// Replace tabs w/ spaces.
217
186
. replace ( / \t / g, ' ' )
218
187
// Use line feeds, drop spaces around them.
@@ -235,6 +204,17 @@ export function buildJsx(tree, options = {}) {
235
204
}
236
205
}
237
206
207
+ /** @type {MemberExpression|Literal|Identifier } */
208
+ let name
209
+ /** @type {Array.<Property> } */
210
+ let fields = [ ]
211
+ /** @type {Array.<Expression> } */
212
+ const objects = [ ]
213
+ /** @type {Array.<Expression|SpreadElement> } */
214
+ let parameters = [ ]
215
+ /** @type {Expression } */
216
+ let key
217
+
238
218
// Do the stuff needed for elements.
239
219
if ( node . type === 'JSXElement' ) {
240
220
name = toIdentifier ( node . openingElement . name )
@@ -245,13 +225,15 @@ export function buildJsx(tree, options = {}) {
245
225
name = create ( name , { type : 'Literal' , value : name . name } )
246
226
}
247
227
248
- attributes = node . openingElement . attributes
249
- index = - 1
228
+ /** @type {boolean } */
229
+ let spread
230
+ const attributes = node . openingElement . attributes
231
+ let index = - 1
250
232
251
233
// Place props in the right order, because we might have duplicates
252
234
// in them and what’s spread in.
253
235
while ( ++ index < attributes . length ) {
254
- attribute = attributes [ index ]
236
+ const attribute = attributes [ index ]
255
237
256
238
if ( attribute . type === 'JSXSpreadAttribute' ) {
257
239
if ( fields . length > 0 ) {
@@ -262,7 +244,7 @@ export function buildJsx(tree, options = {}) {
262
244
objects . push ( attribute . argument )
263
245
spread = true
264
246
} else {
265
- prop = toProperty ( attribute )
247
+ const prop = toProperty ( attribute )
266
248
267
249
if (
268
250
automatic &&
@@ -315,6 +297,11 @@ export function buildJsx(tree, options = {}) {
315
297
objects . push ( { type : 'ObjectExpression' , properties : fields } )
316
298
}
317
299
300
+ /** @type {Expression } */
301
+ let props
302
+ /** @type {MemberExpression|Literal|Identifier } */
303
+ let callee
304
+
318
305
if ( objects . length > 1 ) {
319
306
// Don’t mutate the first object, shallow clone instead.
320
307
if ( objects [ 0 ] . type !== 'ObjectExpression' ) {
@@ -377,7 +364,7 @@ export function buildJsx(tree, options = {}) {
377
364
*/
378
365
function toProperty ( node ) {
379
366
/** @type {Expression } */
380
- var value
367
+ let value
381
368
382
369
if ( node . value ) {
383
370
if ( node . value . type === 'JSXExpressionContainer' ) {
@@ -416,14 +403,12 @@ function toProperty(node) {
416
403
*/
417
404
function toIdentifier ( node ) {
418
405
/** @type {MemberExpression|Identifier|Literal } */
419
- var replace
420
- /** @type {MemberExpression|Identifier|Literal } */
421
- var id
406
+ let replace
422
407
423
408
if ( node . type === 'JSXMemberExpression' ) {
424
409
// `property` is always a `JSXIdentifier`, but it could be something that
425
410
// isn’t an ES identifier name.
426
- id = toIdentifier ( node . property )
411
+ const id = toIdentifier ( node . property )
427
412
replace = {
428
413
type : 'MemberExpression' ,
429
414
object : toIdentifier ( node . object ) ,
@@ -452,15 +437,14 @@ function toIdentifier(node) {
452
437
* @returns {Identifier|Literal|MemberExpression }
453
438
*/
454
439
function toMemberExpression ( id ) {
455
- var identifiers = id . split ( '.' )
456
- var index = - 1
440
+ const identifiers = id . split ( '.' )
441
+ let index = - 1
457
442
/** @type {Identifier|Literal|MemberExpression } */
458
- var result
459
- /** @type {Identifier|Literal } */
460
- var prop
443
+ let result
461
444
462
445
while ( ++ index < identifiers . length ) {
463
- prop = isIdentifierName ( identifiers [ index ] )
446
+ /** @type {Identifier|Literal } */
447
+ const prop = isIdentifierName ( identifiers [ index ] )
464
448
? { type : 'Identifier' , name : identifiers [ index ] }
465
449
: { type : 'Literal' , value : identifiers [ index ] }
466
450
result = index
@@ -484,13 +468,11 @@ function toMemberExpression(id) {
484
468
* @returns {T }
485
469
*/
486
470
function create ( from , node ) {
487
- var fields = [ 'start' , 'end' , 'loc' , 'range' , 'comments' ]
488
- var index = - 1
489
- /** @type {string } */
490
- var field
471
+ const fields = [ 'start' , 'end' , 'loc' , 'range' , 'comments' ]
472
+ let index = - 1
491
473
492
474
while ( ++ index < fields . length ) {
493
- field = fields [ index ]
475
+ const field = fields [ index ]
494
476
if ( field in from ) {
495
477
node [ field ] = from [ field ]
496
478
}
0 commit comments