Skip to content

Commit 7bb31e2

Browse files
committed
Fix to remove initial/final whitespace inside root
1 parent 1ac52f4 commit 7bb31e2

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module.exports = toEstree
44

55
var commas = require('comma-separated-tokens')
6+
var whitespace = require('hast-util-whitespace')
67
var find = require('property-information/find')
78
var hastToReact = require('property-information/hast-to-react.json')
89
var html = require('property-information/html')
@@ -269,7 +270,29 @@ function mdxJsxElement(node, context) {
269270
}
270271

271272
function root(node, context) {
272-
return createJsxFragment(node, all(node, context))
273+
var children = all(node, context)
274+
var cleanChildren = []
275+
var index = -1
276+
var queue
277+
278+
// Remove surrounding whitespace nodes from the fragment.
279+
while (++index < children.length) {
280+
if (
281+
children[index].type === 'JSXExpressionContainer' &&
282+
children[index].expression.type === 'Literal' &&
283+
whitespace(children[index].expression.value)
284+
) {
285+
if (queue) {
286+
queue.push(children[index])
287+
}
288+
} else {
289+
push.apply(cleanChildren, queue)
290+
cleanChildren.push(children[index])
291+
queue = []
292+
}
293+
}
294+
295+
return createJsxFragment(node, cleanChildren)
273296
}
274297

275298
function text(node) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
],
3636
"dependencies": {
3737
"comma-separated-tokens": "^1.0.0",
38+
"hast-util-whitespace": "^1.0.0",
3839
"property-information": "^5.0.0",
3940
"space-separated-tokens": "^1.0.0",
4041
"style-to-object": "^0.3.0",

test.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,25 @@ test('integration (babel)', function (t) {
480480
'should format a root'
481481
)
482482

483+
t.deepEqual(
484+
generate(
485+
toBabel(
486+
toEstree({
487+
type: 'root',
488+
children: [
489+
{type: 'text', value: ' '},
490+
{type: 'text', value: 'x'},
491+
{type: 'text', value: ' '},
492+
{type: 'text', value: 'y'},
493+
{type: 'text', value: ' '}
494+
]
495+
})
496+
)
497+
).code,
498+
'<>{"x"}{" "}{"y"}</>;',
499+
'should ignore initial and trailing whitespace in a root'
500+
)
501+
483502
t.deepEqual(
484503
generate(toBabel(toEstree(s('svg', {viewBox: '0 0 1 1'})))).code,
485504
'<svg viewBox="0 0 1 1" />;',
@@ -584,7 +603,7 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
584603
transform(
585604
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!'
586605
),
587-
'import x from "y";\nexport const name = "World";\n<>{"\\n"}<h2>{"Hello, "}{name}{"!"}</h2></>;',
606+
'import x from "y";\nexport const name = "World";\n<><h2>{"Hello, "}{name}{"!"}</h2></>;',
588607
'should transform MDX.js ESM'
589608
)
590609

@@ -599,7 +618,7 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
599618
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!',
600619
true
601620
),
602-
'<>{"\\n"}<h2>{"Hello, "}{}{"!"}</h2></>;',
621+
'<><h2>{"Hello, "}{}{"!"}</h2></>;',
603622
'should transform ESM w/o estrees'
604623
)
605624

@@ -697,7 +716,7 @@ test('integration (@babel/plugin-transform-react-jsx, react)', function (t) {
697716
'export const name = "World";',
698717
'',
699718
'/*#__PURE__*/',
700-
'React.createElement(React.Fragment, null, "\\n", /*#__PURE__*/React.createElement("h2", null, "Hello, ", name, "!"));'
719+
'React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("h2", null, "Hello, ", name, "!"));'
701720
].join('\n'),
702721
'should integrate w/ `@babel/plugin-transform-react-jsx` (MDX.js ESM)'
703722
)
@@ -800,7 +819,7 @@ test('integration (@vue/babel-plugin-jsx, Vue 3)', function (t) {
800819
'import x from "y";',
801820
'export const name = "World";',
802821
'',
803-
'_createVNode(_Fragment, null, ["\\n", _createVNode("h2", null, ["Hello, ", name, "!"])]);'
822+
'_createVNode(_Fragment, null, [_createVNode("h2", null, ["Hello, ", name, "!"])]);'
804823
].join('\n'),
805824
'should integrate w/ `@vue/babel-plugin-jsx` (MDX.js ESM)'
806825
)

0 commit comments

Comments
 (0)