@@ -6,6 +6,7 @@ var generate = require('@babel/generator').default
6
6
var acorn = require ( 'acorn' )
7
7
var jsx = require ( 'acorn-jsx' )
8
8
var toBabel = require ( 'estree-to-babel' )
9
+ var { walk} = require ( 'estree-walker' )
9
10
var h = require ( 'hastscript' )
10
11
var s = require ( 'hastscript/svg' )
11
12
var fromParse5 = require ( 'hast-util-from-parse5' )
@@ -110,31 +111,31 @@ test('hast-util-to-estree', function (t) {
110
111
111
112
t . deepEqual (
112
113
toEstree ( h ( 'div' ) ) ,
113
- cleanEstree ( acornParse ( '<div/>' ) ) ,
114
+ acornClean ( acornParse ( '<div/>' ) ) ,
114
115
'should match acorn'
115
116
)
116
117
117
118
t . deepEqual (
118
119
toEstree ( { type : 'root' , children : [ h ( 'div' ) ] } ) ,
119
- cleanEstree ( acornParse ( '<><div/></>' ) ) ,
120
+ acornClean ( acornParse ( '<><div/></>' ) ) ,
120
121
'should support a root'
121
122
)
122
123
123
124
t . deepEqual (
124
125
toEstree ( { type : 'root' , children : [ ] } ) ,
125
- cleanEstree ( acornParse ( '<></>' ) ) ,
126
+ acornClean ( acornParse ( '<></>' ) ) ,
126
127
'should support an empty root'
127
128
)
128
129
129
130
t . deepEqual (
130
131
toEstree ( { type : 'root' } ) ,
131
- cleanEstree ( acornParse ( '<></>' ) ) ,
132
+ acornClean ( acornParse ( '<></>' ) ) ,
132
133
'should support a root w/o `chuldren`'
133
134
)
134
135
135
136
t . deepEqual (
136
137
toEstree ( { type : 'root' , children : [ { type : 'doctype' , name : 'html' } ] } ) ,
137
- cleanEstree ( acornParse ( '<></>' ) ) ,
138
+ acornClean ( acornParse ( '<></>' ) ) ,
138
139
'should ignore a doctype'
139
140
)
140
141
@@ -146,19 +147,19 @@ test('hast-util-to-estree', function (t) {
146
147
147
148
t . deepEqual (
148
149
toEstree ( { type : 'root' , children : [ { type : 'text' , value : 'a' } ] } ) ,
149
- cleanEstree ( acornParse ( '<>{"a"}</>' ) ) ,
150
+ acornClean ( acornParse ( '<>{"a"}</>' ) ) ,
150
151
'should support a text'
151
152
)
152
153
153
154
t . deepEqual (
154
155
toEstree ( { type : 'text' , value : 'a' } ) ,
155
- cleanEstree ( acornParse ( '<>{"a"}</>' ) ) ,
156
+ acornClean ( acornParse ( '<>{"a"}</>' ) ) ,
156
157
'should support *just* a text'
157
158
)
158
159
159
160
t . deepEqual (
160
161
toEstree ( { type : 'root' , children : [ { type : 'text' } ] } ) ,
161
- cleanEstree ( acornParse ( '<></>' ) ) ,
162
+ acornClean ( acornParse ( '<></>' ) ) ,
162
163
'should support a text w/o `value`'
163
164
)
164
165
@@ -199,19 +200,19 @@ test('hast-util-to-estree', function (t) {
199
200
200
201
t . deepEqual (
201
202
toEstree ( h ( 'a' , { x : true } ) ) ,
202
- cleanEstree ( acornParse ( '<a x/>' ) ) ,
203
+ acornClean ( acornParse ( '<a x/>' ) ) ,
203
204
'should support an attribute (boolean)'
204
205
)
205
206
206
207
t . deepEqual (
207
208
toEstree ( h ( 'a' , { x : 'y' } ) ) ,
208
- cleanEstree ( acornParse ( '<a x="y"/>' ) ) ,
209
+ acornClean ( acornParse ( '<a x="y"/>' ) ) ,
209
210
'should support an attribute (value)'
210
211
)
211
212
212
213
t . deepEqual (
213
214
toEstree ( h ( 'a' , { style : 'width:1px' } ) ) ,
214
- cleanEstree ( acornParse ( '<a style={{width:"1px"}}/>' ) ) ,
215
+ acornClean ( acornParse ( '<a style={{width:"1px"}}/>' ) ) ,
215
216
'should support an attribute (style)'
216
217
)
217
218
@@ -221,7 +222,7 @@ test('hast-util-to-estree', function (t) {
221
222
tagName : 'a' ,
222
223
properties : { style : { width : 1 } }
223
224
} ) ,
224
- cleanEstree ( acornParse ( '<a style={{width:"1"}}/>' ) ) ,
225
+ acornClean ( acornParse ( '<a style={{width:"1"}}/>' ) ) ,
225
226
'should support an attribute (style, as object)'
226
227
)
227
228
@@ -237,7 +238,7 @@ test('hast-util-to-estree', function (t) {
237
238
}
238
239
}
239
240
} ) ,
240
- cleanEstree (
241
+ acornClean (
241
242
acornParse (
242
243
'<a style={{WebkitBoxShadow: "0 0 1px 0 tomato", msBoxShadow: "0 0 1px 0 tomato", boxShadow: "0 0 1px 0 tomato"}}/>'
243
244
)
@@ -254,7 +255,7 @@ test('hast-util-to-estree', function (t) {
254
255
'-webkit-box-shadow: 0 0 1px 0 tomato; -ms-box-shadow: 0 0 1px 0 tomato; box-shadow: 0 0 1px 0 tomato'
255
256
}
256
257
} ) ,
257
- cleanEstree (
258
+ acornClean (
258
259
acornParse (
259
260
'<a style={{WebkitBoxShadow: "0 0 1px 0 tomato", msBoxShadow: "0 0 1px 0 tomato", boxShadow: "0 0 1px 0 tomato"}}/>'
260
261
)
@@ -272,55 +273,55 @@ test('hast-util-to-estree', function (t) {
272
273
273
274
t . deepEqual (
274
275
toEstree ( h ( 'a' , [ h ( 'b' ) ] ) ) ,
275
- cleanEstree ( acornParse ( '<a><b/></a>' ) ) ,
276
+ acornClean ( acornParse ( '<a><b/></a>' ) ) ,
276
277
'should support a child'
277
278
)
278
279
279
280
t . deepEqual (
280
281
toEstree ( h ( 'a' , [ '\n' , h ( 'b' ) , '\n' ] ) ) ,
281
- cleanEstree ( acornParse ( '<a>{"\\n"}<b/>{"\\n"}</a>' ) ) ,
282
+ acornClean ( acornParse ( '<a>{"\\n"}<b/>{"\\n"}</a>' ) ) ,
282
283
'should support inter-element whitespace'
283
284
)
284
285
285
286
t . deepEqual (
286
287
toEstree ( { type : 'element' , tagName : 'x' , properties : { } } ) ,
287
- cleanEstree ( acornParse ( '<x/>' ) ) ,
288
+ acornClean ( acornParse ( '<x/>' ) ) ,
288
289
'should support an element w/o `children`'
289
290
)
290
291
291
292
t . deepEqual (
292
293
toEstree ( { type : 'element' , tagName : 'xYx' , properties : { } } ) ,
293
- cleanEstree ( acornParse ( '<xYx/>' ) ) ,
294
+ acornClean ( acornParse ( '<xYx/>' ) ) ,
294
295
'should support an element w/ casing in the `tagName`'
295
296
)
296
297
297
298
t . deepEqual (
298
299
toEstree ( { type : 'element' , tagName : 'x' , children : [ ] } ) ,
299
- cleanEstree ( acornParse ( '<x/>' ) ) ,
300
+ acornClean ( acornParse ( '<x/>' ) ) ,
300
301
'should support an element w/o `properties`'
301
302
)
302
303
303
304
t . deepEqual (
304
305
toEstree ( { type : 'element' , tagName : 'x' , properties : { y : null } } ) ,
305
- cleanEstree ( acornParse ( '<x/>' ) ) ,
306
+ acornClean ( acornParse ( '<x/>' ) ) ,
306
307
'should ignore a `null` prop'
307
308
)
308
309
309
310
t . deepEqual (
310
311
toEstree ( { type : 'element' , tagName : 'x' , properties : { y : undefined } } ) ,
311
- cleanEstree ( acornParse ( '<x/>' ) ) ,
312
+ acornClean ( acornParse ( '<x/>' ) ) ,
312
313
'should ignore an `undefined` prop'
313
314
)
314
315
315
316
t . deepEqual (
316
317
toEstree ( { type : 'element' , tagName : 'x' , properties : { y : NaN } } ) ,
317
- cleanEstree ( acornParse ( '<x/>' ) ) ,
318
+ acornClean ( acornParse ( '<x/>' ) ) ,
318
319
'should ignore an `NaN` prop'
319
320
)
320
321
321
322
t . deepEqual (
322
323
toEstree ( { type : 'element' , tagName : 'x' , properties : { allowFullScreen : 0 } } ) ,
323
- cleanEstree ( acornParse ( '<x/>' ) ) ,
324
+ acornClean ( acornParse ( '<x/>' ) ) ,
324
325
'should ignore a falsey boolean prop'
325
326
)
326
327
@@ -330,7 +331,7 @@ test('hast-util-to-estree', function (t) {
330
331
tagName : 'x' ,
331
332
properties : { className : [ 'y' , 'z' ] }
332
333
} ) ,
333
- cleanEstree ( acornParse ( '<x className="y z"/>' ) ) ,
334
+ acornClean ( acornParse ( '<x className="y z"/>' ) ) ,
334
335
'should support space-separated lists'
335
336
)
336
337
@@ -340,25 +341,25 @@ test('hast-util-to-estree', function (t) {
340
341
tagName : 'x' ,
341
342
properties : { srcSet : [ 'y' , 'z' ] }
342
343
} ) ,
343
- cleanEstree ( acornParse ( '<x srcSet="y, z"/>' ) ) ,
344
+ acornClean ( acornParse ( '<x srcSet="y, z"/>' ) ) ,
344
345
'should support comma-separated lists'
345
346
)
346
347
347
348
t . deepEqual (
348
349
toEstree ( s ( 'svg' , { viewBox : '0 0 1 1' } ) ) ,
349
- cleanEstree ( acornParse ( '<svg viewBox="0 0 1 1"/>' ) ) ,
350
+ acornClean ( acornParse ( '<svg viewBox="0 0 1 1"/>' ) ) ,
350
351
'should support SVG'
351
352
)
352
353
353
354
t . deepEqual (
354
355
toEstree ( s ( 'x' , { g1 : [ 1 , 2 ] } ) ) ,
355
- cleanEstree ( acornParse ( '<x g1="1 2"/>' ) ) ,
356
+ acornClean ( acornParse ( '<x g1="1 2"/>' ) ) ,
356
357
'should support SVG w/ an explicit `space` (check)'
357
358
)
358
359
359
360
t . deepEqual (
360
361
toEstree ( s ( 'x' , { g1 : [ 1 , 2 ] } ) , { space : 'svg' } ) ,
361
- cleanEstree ( acornParse ( '<x g1="1, 2"/>' ) ) ,
362
+ acornClean ( acornParse ( '<x g1="1, 2"/>' ) ) ,
362
363
'should support SVG w/ an explicit `space`'
363
364
)
364
365
@@ -381,13 +382,13 @@ test('hast-util-to-estree', function (t) {
381
382
{ type : 'text' , value : '.' }
382
383
]
383
384
} ) ,
384
- cleanEstree ( acornParse ( '<p><b>{"a"}</b>{" "}<i>{"b"}</i>{"."}</p>' ) ) ,
385
+ acornClean ( acornParse ( '<p><b>{"a"}</b>{" "}<i>{"b"}</i>{"."}</p>' ) ) ,
385
386
'should support whitespace between elements'
386
387
)
387
388
388
389
t . deepEqual (
389
390
toEstree ( { type : 'mdxJsxTextElement' } ) ,
390
- cleanEstree ( acornParse ( '<></>' ) ) ,
391
+ acornClean ( acornParse ( '<></>' ) ) ,
391
392
'should support an custom `mdxJsxTextElement` node w/o name, attributes, or children'
392
393
)
393
394
@@ -717,22 +718,22 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
717
718
var hast = toHast ( mdast , { passThrough : types } )
718
719
719
720
if ( clean ) {
720
- visit ( hast , types , cleanEstree )
721
+ visit ( hast , types , acornClean )
721
722
}
722
723
723
724
return recastSerialize ( toEstree ( hast ) )
724
725
725
- function cleanEstree ( node ) {
726
+ function acornClean ( node ) {
726
727
if ( node . data && node . data . estree ) {
727
728
delete node . data . estree
728
729
}
729
730
730
731
if ( typeof node . value === 'object' ) {
731
- cleanEstree ( node . value )
732
+ acornClean ( node . value )
732
733
}
733
734
734
735
if ( node . attributes ) {
735
- node . attributes . forEach ( cleanEstree )
736
+ node . attributes . forEach ( acornClean )
736
737
}
737
738
}
738
739
}
@@ -959,36 +960,16 @@ test('integration (@vue/babel-plugin-jsx, Vue 3)', function (t) {
959
960
}
960
961
} )
961
962
962
- function cleanEstree ( node ) {
963
+ function acornClean ( node ) {
963
964
node . sourceType = 'module'
964
965
965
- one ( node )
966
+ walk ( node , { enter : enter } )
966
967
967
968
return JSON . parse ( JSON . stringify ( node ) )
968
969
969
- function one ( node ) {
970
- var key
971
- var index
972
-
973
- for ( key in node ) {
974
- if ( node [ key ] && typeof node [ key ] === 'object' ) {
975
- if ( 'length' in node [ key ] ) {
976
- index = - 1
977
- while ( ++ index < node [ key ] . length ) {
978
- if ( 'type' in node [ key ] [ index ] ) {
979
- one ( node [ key ] [ index ] )
980
- }
981
- }
982
- } else if ( 'type' in node [ key ] ) {
983
- one ( node [ key ] )
984
- }
985
- }
986
- }
987
-
970
+ function enter ( node ) {
988
971
delete node . start
989
972
delete node . end
990
-
991
- return node
992
973
}
993
974
}
994
975
0 commit comments