Skip to content

Commit 7178e86

Browse files
committed
Refactor to externalise some code
1 parent 46aaf86 commit 7178e86

File tree

1 file changed

+38
-57
lines changed

1 file changed

+38
-57
lines changed

test.js

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var generate = require('@babel/generator').default
66
var acorn = require('acorn')
77
var jsx = require('acorn-jsx')
88
var toBabel = require('estree-to-babel')
9+
var {walk} = require('estree-walker')
910
var h = require('hastscript')
1011
var s = require('hastscript/svg')
1112
var fromParse5 = require('hast-util-from-parse5')
@@ -110,31 +111,31 @@ test('hast-util-to-estree', function (t) {
110111

111112
t.deepEqual(
112113
toEstree(h('div')),
113-
cleanEstree(acornParse('<div/>')),
114+
acornClean(acornParse('<div/>')),
114115
'should match acorn'
115116
)
116117

117118
t.deepEqual(
118119
toEstree({type: 'root', children: [h('div')]}),
119-
cleanEstree(acornParse('<><div/></>')),
120+
acornClean(acornParse('<><div/></>')),
120121
'should support a root'
121122
)
122123

123124
t.deepEqual(
124125
toEstree({type: 'root', children: []}),
125-
cleanEstree(acornParse('<></>')),
126+
acornClean(acornParse('<></>')),
126127
'should support an empty root'
127128
)
128129

129130
t.deepEqual(
130131
toEstree({type: 'root'}),
131-
cleanEstree(acornParse('<></>')),
132+
acornClean(acornParse('<></>')),
132133
'should support a root w/o `chuldren`'
133134
)
134135

135136
t.deepEqual(
136137
toEstree({type: 'root', children: [{type: 'doctype', name: 'html'}]}),
137-
cleanEstree(acornParse('<></>')),
138+
acornClean(acornParse('<></>')),
138139
'should ignore a doctype'
139140
)
140141

@@ -146,19 +147,19 @@ test('hast-util-to-estree', function (t) {
146147

147148
t.deepEqual(
148149
toEstree({type: 'root', children: [{type: 'text', value: 'a'}]}),
149-
cleanEstree(acornParse('<>{"a"}</>')),
150+
acornClean(acornParse('<>{"a"}</>')),
150151
'should support a text'
151152
)
152153

153154
t.deepEqual(
154155
toEstree({type: 'text', value: 'a'}),
155-
cleanEstree(acornParse('<>{"a"}</>')),
156+
acornClean(acornParse('<>{"a"}</>')),
156157
'should support *just* a text'
157158
)
158159

159160
t.deepEqual(
160161
toEstree({type: 'root', children: [{type: 'text'}]}),
161-
cleanEstree(acornParse('<></>')),
162+
acornClean(acornParse('<></>')),
162163
'should support a text w/o `value`'
163164
)
164165

@@ -199,19 +200,19 @@ test('hast-util-to-estree', function (t) {
199200

200201
t.deepEqual(
201202
toEstree(h('a', {x: true})),
202-
cleanEstree(acornParse('<a x/>')),
203+
acornClean(acornParse('<a x/>')),
203204
'should support an attribute (boolean)'
204205
)
205206

206207
t.deepEqual(
207208
toEstree(h('a', {x: 'y'})),
208-
cleanEstree(acornParse('<a x="y"/>')),
209+
acornClean(acornParse('<a x="y"/>')),
209210
'should support an attribute (value)'
210211
)
211212

212213
t.deepEqual(
213214
toEstree(h('a', {style: 'width:1px'})),
214-
cleanEstree(acornParse('<a style={{width:"1px"}}/>')),
215+
acornClean(acornParse('<a style={{width:"1px"}}/>')),
215216
'should support an attribute (style)'
216217
)
217218

@@ -221,7 +222,7 @@ test('hast-util-to-estree', function (t) {
221222
tagName: 'a',
222223
properties: {style: {width: 1}}
223224
}),
224-
cleanEstree(acornParse('<a style={{width:"1"}}/>')),
225+
acornClean(acornParse('<a style={{width:"1"}}/>')),
225226
'should support an attribute (style, as object)'
226227
)
227228

@@ -237,7 +238,7 @@ test('hast-util-to-estree', function (t) {
237238
}
238239
}
239240
}),
240-
cleanEstree(
241+
acornClean(
241242
acornParse(
242243
'<a style={{WebkitBoxShadow: "0 0 1px 0 tomato", msBoxShadow: "0 0 1px 0 tomato", boxShadow: "0 0 1px 0 tomato"}}/>'
243244
)
@@ -254,7 +255,7 @@ test('hast-util-to-estree', function (t) {
254255
'-webkit-box-shadow: 0 0 1px 0 tomato; -ms-box-shadow: 0 0 1px 0 tomato; box-shadow: 0 0 1px 0 tomato'
255256
}
256257
}),
257-
cleanEstree(
258+
acornClean(
258259
acornParse(
259260
'<a style={{WebkitBoxShadow: "0 0 1px 0 tomato", msBoxShadow: "0 0 1px 0 tomato", boxShadow: "0 0 1px 0 tomato"}}/>'
260261
)
@@ -272,55 +273,55 @@ test('hast-util-to-estree', function (t) {
272273

273274
t.deepEqual(
274275
toEstree(h('a', [h('b')])),
275-
cleanEstree(acornParse('<a><b/></a>')),
276+
acornClean(acornParse('<a><b/></a>')),
276277
'should support a child'
277278
)
278279

279280
t.deepEqual(
280281
toEstree(h('a', ['\n', h('b'), '\n'])),
281-
cleanEstree(acornParse('<a>{"\\n"}<b/>{"\\n"}</a>')),
282+
acornClean(acornParse('<a>{"\\n"}<b/>{"\\n"}</a>')),
282283
'should support inter-element whitespace'
283284
)
284285

285286
t.deepEqual(
286287
toEstree({type: 'element', tagName: 'x', properties: {}}),
287-
cleanEstree(acornParse('<x/>')),
288+
acornClean(acornParse('<x/>')),
288289
'should support an element w/o `children`'
289290
)
290291

291292
t.deepEqual(
292293
toEstree({type: 'element', tagName: 'xYx', properties: {}}),
293-
cleanEstree(acornParse('<xYx/>')),
294+
acornClean(acornParse('<xYx/>')),
294295
'should support an element w/ casing in the `tagName`'
295296
)
296297

297298
t.deepEqual(
298299
toEstree({type: 'element', tagName: 'x', children: []}),
299-
cleanEstree(acornParse('<x/>')),
300+
acornClean(acornParse('<x/>')),
300301
'should support an element w/o `properties`'
301302
)
302303

303304
t.deepEqual(
304305
toEstree({type: 'element', tagName: 'x', properties: {y: null}}),
305-
cleanEstree(acornParse('<x/>')),
306+
acornClean(acornParse('<x/>')),
306307
'should ignore a `null` prop'
307308
)
308309

309310
t.deepEqual(
310311
toEstree({type: 'element', tagName: 'x', properties: {y: undefined}}),
311-
cleanEstree(acornParse('<x/>')),
312+
acornClean(acornParse('<x/>')),
312313
'should ignore an `undefined` prop'
313314
)
314315

315316
t.deepEqual(
316317
toEstree({type: 'element', tagName: 'x', properties: {y: NaN}}),
317-
cleanEstree(acornParse('<x/>')),
318+
acornClean(acornParse('<x/>')),
318319
'should ignore an `NaN` prop'
319320
)
320321

321322
t.deepEqual(
322323
toEstree({type: 'element', tagName: 'x', properties: {allowFullScreen: 0}}),
323-
cleanEstree(acornParse('<x/>')),
324+
acornClean(acornParse('<x/>')),
324325
'should ignore a falsey boolean prop'
325326
)
326327

@@ -330,7 +331,7 @@ test('hast-util-to-estree', function (t) {
330331
tagName: 'x',
331332
properties: {className: ['y', 'z']}
332333
}),
333-
cleanEstree(acornParse('<x className="y z"/>')),
334+
acornClean(acornParse('<x className="y z"/>')),
334335
'should support space-separated lists'
335336
)
336337

@@ -340,25 +341,25 @@ test('hast-util-to-estree', function (t) {
340341
tagName: 'x',
341342
properties: {srcSet: ['y', 'z']}
342343
}),
343-
cleanEstree(acornParse('<x srcSet="y, z"/>')),
344+
acornClean(acornParse('<x srcSet="y, z"/>')),
344345
'should support comma-separated lists'
345346
)
346347

347348
t.deepEqual(
348349
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"/>')),
350351
'should support SVG'
351352
)
352353

353354
t.deepEqual(
354355
toEstree(s('x', {g1: [1, 2]})),
355-
cleanEstree(acornParse('<x g1="1 2"/>')),
356+
acornClean(acornParse('<x g1="1 2"/>')),
356357
'should support SVG w/ an explicit `space` (check)'
357358
)
358359

359360
t.deepEqual(
360361
toEstree(s('x', {g1: [1, 2]}), {space: 'svg'}),
361-
cleanEstree(acornParse('<x g1="1, 2"/>')),
362+
acornClean(acornParse('<x g1="1, 2"/>')),
362363
'should support SVG w/ an explicit `space`'
363364
)
364365

@@ -381,13 +382,13 @@ test('hast-util-to-estree', function (t) {
381382
{type: 'text', value: '.'}
382383
]
383384
}),
384-
cleanEstree(acornParse('<p><b>{"a"}</b>{" "}<i>{"b"}</i>{"."}</p>')),
385+
acornClean(acornParse('<p><b>{"a"}</b>{" "}<i>{"b"}</i>{"."}</p>')),
385386
'should support whitespace between elements'
386387
)
387388

388389
t.deepEqual(
389390
toEstree({type: 'mdxJsxTextElement'}),
390-
cleanEstree(acornParse('<></>')),
391+
acornClean(acornParse('<></>')),
391392
'should support an custom `mdxJsxTextElement` node w/o name, attributes, or children'
392393
)
393394

@@ -717,22 +718,22 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
717718
var hast = toHast(mdast, {passThrough: types})
718719

719720
if (clean) {
720-
visit(hast, types, cleanEstree)
721+
visit(hast, types, acornClean)
721722
}
722723

723724
return recastSerialize(toEstree(hast))
724725

725-
function cleanEstree(node) {
726+
function acornClean(node) {
726727
if (node.data && node.data.estree) {
727728
delete node.data.estree
728729
}
729730

730731
if (typeof node.value === 'object') {
731-
cleanEstree(node.value)
732+
acornClean(node.value)
732733
}
733734

734735
if (node.attributes) {
735-
node.attributes.forEach(cleanEstree)
736+
node.attributes.forEach(acornClean)
736737
}
737738
}
738739
}
@@ -959,36 +960,16 @@ test('integration (@vue/babel-plugin-jsx, Vue 3)', function (t) {
959960
}
960961
})
961962

962-
function cleanEstree(node) {
963+
function acornClean(node) {
963964
node.sourceType = 'module'
964965

965-
one(node)
966+
walk(node, {enter: enter})
966967

967968
return JSON.parse(JSON.stringify(node))
968969

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) {
988971
delete node.start
989972
delete node.end
990-
991-
return node
992973
}
993974
}
994975

0 commit comments

Comments
 (0)