43
43
* @typedef {ReturnType<find> } Info
44
44
* @typedef {'html'|'svg' } Space
45
45
*
46
- * @typedef {(node: Node , context: Context) => EstreeJsxChild? } Handle
46
+ * @typedef {(node: any , context: Context) => EstreeJsxChild? } Handle
47
47
*
48
48
* @typedef Options
49
49
* @property {Space } [space='html']
@@ -69,17 +69,17 @@ import style from 'style-to-object'
69
69
import { position } from 'unist-util-position'
70
70
import { zwitch } from 'zwitch'
71
71
72
- var own = { } . hasOwnProperty
73
- var push = [ ] . push
72
+ const own = { } . hasOwnProperty
73
+ const push = [ ] . push
74
74
75
75
/**
76
- * @param {Node } tree
76
+ * @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression } tree
77
77
* @param {Options } options
78
78
* @returns {EstreeProgram }
79
79
*/
80
80
export function toEstree ( tree , options = { } ) {
81
81
/** @type {Context } */
82
- var context = {
82
+ const context = {
83
83
schema : options . space === 'svg' ? svg : html ,
84
84
comments : [ ] ,
85
85
esm : [ ] ,
@@ -104,8 +104,8 @@ export function toEstree(tree, options = {}) {
104
104
)
105
105
} )
106
106
}
107
- var result = context . handle ( tree , context )
108
- var body = context . esm
107
+ let result = context . handle ( tree , context )
108
+ const body = context . esm
109
109
110
110
if ( result ) {
111
111
if ( result . type !== 'JSXFragment' && result . type !== 'JSXElement' ) {
@@ -151,7 +151,7 @@ function ignore() {}
151
151
* @returns {EstreeJsxExpressionContainer }
152
152
*/
153
153
function comment ( node , context ) {
154
- var esnode = inherit ( node , { type : 'Block' , value : node . value } )
154
+ const esnode = inherit ( node , { type : 'Block' , value : node . value } )
155
155
156
156
context . comments . push ( esnode )
157
157
@@ -171,39 +171,27 @@ function comment(node, context) {
171
171
*/
172
172
// eslint-disable-next-line complexity
173
173
function element ( node , context ) {
174
- var parentSchema = context . schema
175
- var schema = parentSchema
176
- var props = node . properties || { }
177
- /** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute> } */
178
- var attributes = [ ]
179
- /** @type {Array.<EstreeJsxChild> } */
180
- var children
181
- /** @type {Info } */
182
- var info
183
- /** @type {string } */
184
- var prop
185
- /** @type {string } */
186
- var cssProp
187
- /** @type {Array.<EstreeProperty> } */
188
- var cssProperties
189
- /** @type {Properties[string] } */
190
- var value
191
- /** @type {EstreeJsxAttribute['value'] } */
192
- var attributeValue
193
- /** @type {Object.<string, string> } */
194
- var styleValue
174
+ const parentSchema = context . schema
175
+ let schema = parentSchema
176
+ const props = node . properties || { }
195
177
196
178
if ( parentSchema . space === 'html' && node . tagName . toLowerCase ( ) === 'svg' ) {
197
179
schema = svg
198
180
context . schema = schema
199
181
}
200
182
201
- children = all ( node , context )
183
+ const children = all ( node , context )
184
+ /** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute> } */
185
+ const attributes = [ ]
186
+ /** @type {string } */
187
+ let prop
202
188
203
189
for ( prop in props ) {
204
190
if ( own . call ( props , prop ) ) {
205
- value = props [ prop ]
206
- info = find ( schema , prop )
191
+ let value = props [ prop ]
192
+ const info = find ( schema , prop )
193
+ /** @type {EstreeJsxAttribute['value'] } */
194
+ let attributeValue
207
195
208
196
// Ignore nullish and `NaN` values.
209
197
// Ignore `false` and falsey known booleans.
@@ -228,11 +216,15 @@ function element(node, context) {
228
216
}
229
217
230
218
if ( prop === 'style' ) {
219
+ /** @type {Object.<string, string> } */
231
220
// @ts -ignore Assume `value` is then an object.
232
- styleValue =
221
+ const styleValue =
233
222
typeof value === 'string' ? parseStyle ( value , node . tagName ) : value
234
223
235
- cssProperties = [ ]
224
+ /** @type {Array.<EstreeProperty> } */
225
+ const cssProperties = [ ]
226
+ /** @type {string } */
227
+ let cssProp
236
228
237
229
for ( cssProp in styleValue ) {
238
230
// eslint-disable-next-line max-depth
@@ -316,8 +308,8 @@ function element(node, context) {
316
308
function mdxjsEsm ( node , context ) {
317
309
/** @type {EstreeProgram } */
318
310
// @ts -ignore Assume program.
319
- var estree = node . data && node . data . estree
320
- var comments = ( estree && estree . comments ) || [ ]
311
+ const estree = node . data && node . data . estree
312
+ const comments = ( estree && estree . comments ) || [ ]
321
313
322
314
if ( estree ) {
323
315
push . apply ( context . comments , comments )
@@ -334,9 +326,9 @@ function mdxjsEsm(node, context) {
334
326
function mdxExpression ( node , context ) {
335
327
/** @type {EstreeProgram } */
336
328
// @ts -ignore Assume program.
337
- var estree = node . data && node . data . estree
329
+ const estree = node . data && node . data . estree
338
330
/** @type {EstreeExpression } */
339
- var expression
331
+ let expression
340
332
341
333
if ( estree ) {
342
334
push . apply ( context . comments , estree . comments )
@@ -360,26 +352,10 @@ function mdxExpression(node, context) {
360
352
*/
361
353
// eslint-disable-next-line complexity
362
354
function mdxJsxElement ( node , context ) {
363
- var parentSchema = context . schema
364
- var schema = parentSchema
365
- var attrs = node . attributes || [ ]
366
- var index = - 1
367
- /** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute> } */
368
- var attributes = [ ]
369
- /** @type {Array.<EstreeJsxChild> } */
370
- var children
371
- /** @type {MDXJsxExpressionAttribute|MDXJsxAttribute } */
372
- var attr
373
- /** @type {MDXJsxAttributeValueExpression|string } */
374
- var value
375
- /** @type {EstreeExpression } */
376
- var expression
377
- /** @type {EstreeProgram } */
378
- var estree
379
- /** @type {EstreeJsxAttribute['value'] } */
380
- var attributeValue
381
- /** @type {EstreeJsxSpreadAttribute['argument'] } */
382
- var argumentValue
355
+ const parentSchema = context . schema
356
+ let schema = parentSchema
357
+ const attrs = node . attributes || [ ]
358
+ let index = - 1
383
359
384
360
if (
385
361
node . name &&
@@ -390,11 +366,15 @@ function mdxJsxElement(node, context) {
390
366
context . schema = schema
391
367
}
392
368
393
- children = all ( node , context )
369
+ const children = all ( node , context )
370
+ /** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute> } */
371
+ const attributes = [ ]
394
372
395
373
while ( ++ index < attrs . length ) {
396
- attr = attrs [ index ]
397
- value = attr . value
374
+ const attr = attrs [ index ]
375
+ const value = attr . value
376
+ /** @type {EstreeJsxAttribute['value'] } */
377
+ let attributeValue
398
378
399
379
if ( attr . type === 'mdxJsxAttribute' ) {
400
380
if ( value === undefined || value === null ) {
@@ -403,9 +383,11 @@ function mdxJsxElement(node, context) {
403
383
}
404
384
// `MDXJsxAttributeValueExpression`.
405
385
else if ( typeof value === 'object' ) {
386
+ /** @type {EstreeProgram } */
406
387
// @ts -ignore Assume program.
407
- estree = value . data && value . data . estree
408
- expression = null
388
+ const estree = value . data && value . data . estree
389
+ /** @type {EstreeExpression } */
390
+ let expression = null
409
391
410
392
if ( estree ) {
411
393
push . apply ( context . comments , estree . comments )
@@ -436,9 +418,11 @@ function mdxJsxElement(node, context) {
436
418
}
437
419
// MDXJsxExpressionAttribute.
438
420
else {
421
+ /** @type {EstreeProgram } */
439
422
// @ts -ignore Assume program.
440
- estree = attr . data && attr . data . estree
441
- argumentValue = null
423
+ const estree = attr . data && attr . data . estree
424
+ /** @type {EstreeJsxSpreadAttribute['argument'] } */
425
+ let argumentValue = null
442
426
443
427
if ( estree ) {
444
428
push . apply ( context . comments , estree . comments )
@@ -498,18 +482,16 @@ function mdxJsxElement(node, context) {
498
482
* @returns {EstreeJsxFragment }
499
483
*/
500
484
function root ( node , context ) {
501
- var children = all ( node , context )
485
+ const children = all ( node , context )
502
486
/** @type {Array.<EstreeJsxChild> } */
503
- var cleanChildren = [ ]
504
- var index = - 1
487
+ const cleanChildren = [ ]
488
+ let index = - 1
505
489
/** @type {Array.<EstreeJsxChild> } */
506
- var queue
507
- /** @type {EstreeJsxChild } */
508
- var child
490
+ let queue
509
491
510
492
// Remove surrounding whitespace nodes from the fragment.
511
493
while ( ++ index < children . length ) {
512
- child = children [ index ]
494
+ const child = children [ index ]
513
495
514
496
if (
515
497
child . type === 'JSXExpressionContainer' &&
@@ -520,8 +502,7 @@ function root(node, context) {
520
502
queue . push ( child )
521
503
}
522
504
} else {
523
- push . apply ( cleanChildren , queue )
524
- cleanChildren . push ( child )
505
+ cleanChildren . push ( ...( queue || [ ] ) , child )
525
506
queue = [ ]
526
507
}
527
508
}
@@ -539,7 +520,7 @@ function root(node, context) {
539
520
* @returns {EstreeJsxExpressionContainer }
540
521
*/
541
522
function text ( node ) {
542
- var value = String ( node . value || '' )
523
+ const value = String ( node . value || '' )
543
524
544
525
if ( ! value ) return
545
526
@@ -555,15 +536,13 @@ function text(node) {
555
536
* @returns {Array.<EstreeJsxChild> }
556
537
*/
557
538
function all ( parent , context ) {
558
- var children = parent . children || [ ]
559
- var index = - 1
539
+ const children = parent . children || [ ]
540
+ let index = - 1
560
541
/** @type {Array.<EstreeJsxChild> } */
561
- var results = [ ]
562
- /** @type {EstreeJsxChild|Array.<EstreeJsxChild> } */
563
- var result
542
+ const results = [ ]
564
543
565
544
while ( ++ index < children . length ) {
566
- result = context . handle ( children [ index ] , context )
545
+ const result = context . handle ( children [ index ] , context )
567
546
568
547
if ( Array . isArray ( result ) ) {
569
548
results . push ( ...result )
@@ -584,11 +563,11 @@ function all(parent, context) {
584
563
* @returns {T }
585
564
*/
586
565
function inherit ( hast , esnode ) {
587
- var left = hast . data
566
+ const left = hast . data
588
567
/** @type {Object.<string, unknown> } */
589
- var right
568
+ let right
590
569
/** @type {string } */
591
- var key
570
+ let key
592
571
593
572
create ( hast , esnode )
594
573
@@ -618,7 +597,7 @@ function inherit(hast, esnode) {
618
597
* @returns {T }
619
598
*/
620
599
function create ( hast , esnode ) {
621
- var p = position ( hast )
600
+ const p = position ( hast )
622
601
623
602
if ( p . start . line ) {
624
603
// @ts -ignore acorn-style.
@@ -649,13 +628,11 @@ const createJsxName =
649
628
* @returns {EstreeJsxElementName }
650
629
*/
651
630
function ( name , attribute ) {
652
- /** @type {Array.<string> } */
653
- var parts
654
631
/** @type {EstreeJsxElementName } */
655
- var node
632
+ let node
656
633
657
634
if ( ! attribute && name . includes ( '.' ) ) {
658
- parts = name . split ( '.' )
635
+ const parts = name . split ( '.' )
659
636
node = { type : 'JSXIdentifier' , name : parts . shift ( ) }
660
637
while ( parts . length > 0 ) {
661
638
node = {
@@ -665,7 +642,7 @@ const createJsxName =
665
642
}
666
643
}
667
644
} else if ( name . includes ( ':' ) ) {
668
- parts = name . split ( ':' )
645
+ const parts = name . split ( ':' )
669
646
node = {
670
647
type : 'JSXNamespacedName' ,
671
648
namespace : { type : 'JSXIdentifier' , name : parts [ 0 ] } ,
@@ -686,7 +663,7 @@ const createJsxName =
686
663
*/
687
664
function parseStyle ( value , tagName ) {
688
665
/** @type {Object.<string, string> } */
689
- var result = { }
666
+ const result = { }
690
667
691
668
try {
692
669
style ( value , iterator )
@@ -725,7 +702,7 @@ function styleReplacer(_, $1) {
725
702
* @returns {boolean }
726
703
*/
727
704
function jsxIdentifierName ( name ) {
728
- var index = - 1
705
+ let index = - 1
729
706
730
707
while ( ++ index < name . length ) {
731
708
if ( ! ( index ? cont : identifierStart ) ( name . charCodeAt ( index ) ) ) return false
0 commit comments