@@ -14,6 +14,7 @@ var style = require('style-to-object')
14
14
var position = require ( 'unist-util-position' )
15
15
var zwitch = require ( 'zwitch' )
16
16
17
+ var own = { } . hasOwnProperty
17
18
var push = [ ] . push
18
19
19
20
var handlers = {
@@ -45,7 +46,16 @@ function toEstree(tree, options) {
45
46
46
47
if ( result ) {
47
48
if ( result . type !== 'JSXFragment' && result . type !== 'JSXElement' ) {
48
- result = createJsxFragment ( tree , [ result ] )
49
+ result = create ( tree , {
50
+ type : 'JSXFragment' ,
51
+ openingFragment : {
52
+ type : 'JSXOpeningFragment' ,
53
+ attributes : [ ] ,
54
+ selfClosing : false
55
+ } ,
56
+ closingFragment : { type : 'JSXClosingFragment' } ,
57
+ children : [ result ]
58
+ } )
49
59
}
50
60
51
61
body . push ( create ( tree , { type : 'ExpressionStatement' , expression : result } ) )
@@ -70,7 +80,7 @@ function unknown(node) {
70
80
function ignore ( ) { }
71
81
72
82
function comment ( node , context ) {
73
- var esnode = create ( node , { type : 'Block' , value : node . value } )
83
+ var esnode = inherit ( node , { type : 'Block' , value : node . value } )
74
84
75
85
context . comments . push ( esnode )
76
86
@@ -138,51 +148,56 @@ function element(node, context) {
138
148
cssProperties = [ ]
139
149
140
150
for ( cssProp in value ) {
141
- cssProperties . push (
142
- create ( null , {
143
- type : 'Property' ,
144
- method : false ,
145
- shorthand : false ,
146
- computed : false ,
147
- key : create ( null , { type : 'Identifier' , name : cssProp } ) ,
148
- value : create ( null , {
149
- type : 'Literal' ,
150
- value : String ( value [ cssProp ] ) ,
151
- raw : JSON . stringify ( String ( value [ cssProp ] ) )
152
- } ) ,
153
- kind : 'init'
154
- } )
155
- )
151
+ cssProperties . push ( {
152
+ type : 'Property' ,
153
+ method : false ,
154
+ shorthand : false ,
155
+ computed : false ,
156
+ key : { type : 'Identifier' , name : cssProp } ,
157
+ value : {
158
+ type : 'Literal' ,
159
+ value : String ( value [ cssProp ] ) ,
160
+ raw : JSON . stringify ( String ( value [ cssProp ] ) )
161
+ } ,
162
+ kind : 'init'
163
+ } )
156
164
}
157
165
158
- value = create ( null , {
166
+ value = {
159
167
type : 'JSXExpressionContainer' ,
160
- expression : create ( null , {
161
- type : 'ObjectExpression' ,
162
- properties : cssProperties
163
- } )
164
- } )
168
+ expression : { type : 'ObjectExpression' , properties : cssProperties }
169
+ }
165
170
} else {
166
- value = create ( null , {
171
+ value = {
167
172
type : 'Literal' ,
168
173
value : String ( value ) ,
169
174
raw : JSON . stringify ( String ( value ) )
170
- } )
175
+ }
171
176
}
172
177
173
- attributes . push (
174
- create ( null , {
175
- type : 'JSXAttribute' ,
176
- name : create ( null , { type : 'JSXIdentifier' , name : prop } ) ,
177
- value : value
178
- } )
179
- )
178
+ attributes . push ( {
179
+ type : 'JSXAttribute' ,
180
+ name : { type : 'JSXIdentifier' , name : prop } ,
181
+ value : value
182
+ } )
180
183
}
181
184
182
185
// Restore parent schema.
183
186
context . schema = parentSchema
184
187
185
- return createJsxElement ( node , node . tagName , attributes , children )
188
+ return inherit ( node , {
189
+ type : 'JSXElement' ,
190
+ openingElement : {
191
+ type : 'JSXOpeningElement' ,
192
+ attributes : attributes ,
193
+ name : createJsxName ( node . tagName ) ,
194
+ selfClosing : ! children . length
195
+ } ,
196
+ closingElement : children . length
197
+ ? { type : 'JSXClosingElement' , name : createJsxName ( node . tagName ) }
198
+ : null ,
199
+ children : children
200
+ } )
186
201
}
187
202
188
203
function mdxjsEsm ( node , context ) {
@@ -205,7 +220,7 @@ function mdxExpression(node, context) {
205
220
expression = estree . body [ 0 ] && estree . body [ 0 ] . expression
206
221
}
207
222
208
- return create ( node , {
223
+ return inherit ( node , {
209
224
type : 'JSXExpressionContainer' ,
210
225
expression : expression || create ( node , { type : 'JSXEmptyExpression' } )
211
226
} )
@@ -221,6 +236,7 @@ function mdxJsxElement(node, context) {
221
236
var children
222
237
var attr
223
238
var value
239
+ var expression
224
240
var estree
225
241
226
242
if (
@@ -242,35 +258,33 @@ function mdxJsxElement(node, context) {
242
258
if ( value == null ) {
243
259
// Empty.
244
260
}
245
- // MDXJsxAttributeValueExpression.
261
+ // ` MDXJsxAttributeValueExpression` .
246
262
else if ( typeof value === 'object' ) {
247
263
estree = value . data && value . data . estree
248
- value = null
264
+ expression = null
249
265
250
266
if ( estree ) {
251
267
push . apply ( context . comments , estree . comments )
252
268
attachComments ( estree , estree . comments )
253
- value = estree . body [ 0 ] && estree . body [ 0 ] . expression
269
+ expression = estree . body [ 0 ] && estree . body [ 0 ] . expression
254
270
}
255
271
256
- // To do: `node` is wrong.
257
- value = create ( node , {
272
+ value = inherit ( value , {
258
273
type : 'JSXExpressionContainer' ,
259
- expression : value || create ( null , { type : 'JSXEmptyExpression' } )
274
+ expression : expression || { type : 'JSXEmptyExpression' }
260
275
} )
261
276
}
262
277
// Anything else.
263
278
else {
264
- // To do: use `value`?
265
- value = create ( null , {
279
+ value = {
266
280
type : 'Literal' ,
267
281
value : String ( value ) ,
268
282
raw : JSON . stringify ( String ( value ) )
269
- } )
283
+ }
270
284
}
271
285
272
286
attributes . push (
273
- create ( null , {
287
+ inherit ( attr , {
274
288
type : 'JSXAttribute' ,
275
289
name : createJsxName ( attr . name ) ,
276
290
value : value
@@ -294,10 +308,9 @@ function mdxJsxElement(node, context) {
294
308
}
295
309
296
310
attributes . push (
297
- create ( null , {
311
+ inherit ( attr , {
298
312
type : 'JSXSpreadAttribute' ,
299
- argument :
300
- value || create ( null , { type : 'ObjectExpression' , properties : { } } )
313
+ argument : value || { type : 'ObjectExpression' , properties : { } }
301
314
} )
302
315
)
303
316
}
@@ -306,9 +319,33 @@ function mdxJsxElement(node, context) {
306
319
// Restore parent schema.
307
320
context . schema = parentSchema
308
321
309
- return node . name
310
- ? createJsxElement ( node , node . name , attributes , children )
311
- : createJsxFragment ( node , children )
322
+ return inherit (
323
+ node ,
324
+ node . name
325
+ ? {
326
+ type : 'JSXElement' ,
327
+ openingElement : {
328
+ type : 'JSXOpeningElement' ,
329
+ attributes : attributes ,
330
+ name : createJsxName ( node . name ) ,
331
+ selfClosing : ! children . length
332
+ } ,
333
+ closingElement : children . length
334
+ ? { type : 'JSXClosingElement' , name : createJsxName ( node . name ) }
335
+ : null ,
336
+ children : children
337
+ }
338
+ : {
339
+ type : 'JSXFragment' ,
340
+ openingFragment : {
341
+ type : 'JSXOpeningFragment' ,
342
+ attributes : [ ] ,
343
+ selfClosing : false
344
+ } ,
345
+ closingFragment : { type : 'JSXClosingFragment' } ,
346
+ children : children
347
+ }
348
+ )
312
349
}
313
350
314
351
function root ( node , context ) {
@@ -334,7 +371,16 @@ function root(node, context) {
334
371
}
335
372
}
336
373
337
- return createJsxFragment ( node , cleanChildren )
374
+ return inherit ( node , {
375
+ type : 'JSXFragment' ,
376
+ openingFragment : {
377
+ type : 'JSXOpeningFragment' ,
378
+ attributes : [ ] ,
379
+ selfClosing : false
380
+ } ,
381
+ closingFragment : { type : 'JSXClosingFragment' } ,
382
+ children : cleanChildren
383
+ } )
338
384
}
339
385
340
386
function text ( node ) {
@@ -344,71 +390,42 @@ function text(node) {
344
390
345
391
return create ( node , {
346
392
type : 'JSXExpressionContainer' ,
347
- expression : create ( node , {
393
+ expression : inherit ( node , {
348
394
type : 'Literal' ,
349
395
value : value ,
350
396
raw : JSON . stringify ( value )
351
397
} )
352
398
} )
353
399
}
354
400
355
- function createJsxElement ( node , name , attributes , children ) {
356
- return create ( node , {
357
- type : 'JSXElement' ,
358
- openingElement : create ( null , {
359
- type : 'JSXOpeningElement' ,
360
- attributes : attributes ,
361
- name : createJsxName ( name ) ,
362
- selfClosing : ! children . length
363
- } ) ,
364
- closingElement : children . length
365
- ? create ( null , { type : 'JSXClosingElement' , name : createJsxName ( name ) } )
366
- : null ,
367
- children : children
368
- } )
369
- }
370
-
371
401
function createJsxName ( name ) {
372
402
var parts
373
403
var node
374
404
375
405
if ( name . indexOf ( '.' ) > - 1 ) {
376
406
parts = name . split ( '.' )
377
- node = create ( null , { type : 'JSXIdentifier' , name : parts . shift ( ) } )
407
+ node = { type : 'JSXIdentifier' , name : parts . shift ( ) }
378
408
while ( parts . length ) {
379
409
node = {
380
410
type : 'JSXMemberExpression' ,
381
411
object : node ,
382
- property : create ( null , { type : 'JSXIdentifier' , name : parts . shift ( ) } )
412
+ property : { type : 'JSXIdentifier' , name : parts . shift ( ) }
383
413
}
384
414
}
385
415
} else if ( name . indexOf ( ':' ) > - 1 ) {
386
416
parts = name . split ( ':' )
387
417
node = {
388
418
type : 'JSXNamespacedName' ,
389
- namespace : create ( null , { type : 'JSXIdentifier' , name : parts [ 0 ] } ) ,
390
- name : create ( null , { type : 'JSXIdentifier' , name : parts [ 1 ] } )
419
+ namespace : { type : 'JSXIdentifier' , name : parts [ 0 ] } ,
420
+ name : { type : 'JSXIdentifier' , name : parts [ 1 ] }
391
421
}
392
422
} else {
393
- node = create ( null , { type : 'JSXIdentifier' , name : name } )
423
+ node = { type : 'JSXIdentifier' , name : name }
394
424
}
395
425
396
426
return node
397
427
}
398
428
399
- function createJsxFragment ( node , children ) {
400
- return create ( node , {
401
- type : 'JSXFragment' ,
402
- openingFragment : create ( null , {
403
- type : 'JSXOpeningFragment' ,
404
- attributes : [ ] ,
405
- selfClosing : false
406
- } ) ,
407
- closingFragment : create ( null , { type : 'JSXClosingFragment' } ) ,
408
- children : children
409
- } )
410
- }
411
-
412
429
function all ( parent , context ) {
413
430
var children = parent . children || [ ]
414
431
var results = [ ]
@@ -425,19 +442,42 @@ function all(parent, context) {
425
442
return results
426
443
}
427
444
428
- function create ( hast , esnode , fromStart , fromEnd ) {
445
+ // Take positional info and data from `hast`.
446
+ function inherit ( hast , esnode ) {
447
+ var left = hast . data
448
+ var right
449
+ var key
450
+
451
+ create ( hast , esnode )
452
+
453
+ if ( left ) {
454
+ for ( key in left ) {
455
+ if ( own . call ( left , key ) && key !== 'estree' ) {
456
+ if ( ! right ) right = { }
457
+ right [ key ] = left [ key ]
458
+ }
459
+ }
460
+
461
+ if ( right ) {
462
+ esnode . data = right
463
+ }
464
+ }
465
+
466
+ return esnode
467
+ }
468
+
469
+ // Take just positional info.
470
+ function create ( hast , esnode ) {
429
471
var p = position ( hast )
430
- var left = fromStart || 0
431
- var right = fromEnd || 0
432
472
433
473
if ( p . start . line ) {
434
- esnode . start = p . start . offset + left
435
- esnode . end = p . end . offset - right
474
+ esnode . start = p . start . offset
475
+ esnode . end = p . end . offset
436
476
esnode . loc = {
437
- start : { line : p . start . line , column : p . start . column - 1 + left } ,
438
- end : { line : p . end . line , column : p . end . column - 1 - right }
477
+ start : { line : p . start . line , column : p . start . column - 1 } ,
478
+ end : { line : p . end . line , column : p . end . column - 1 }
439
479
}
440
- esnode . range = [ p . start . offset + left , p . end . offset - right ]
480
+ esnode . range = [ p . start . offset , p . end . offset ]
441
481
}
442
482
443
483
return esnode
0 commit comments