Skip to content

Commit 4983b73

Browse files
committed
Fix to allow whitespace between elements
1 parent 084a556 commit 4983b73

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

index.js

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

55
var commas = require('comma-separated-tokens')
6-
var whitespace = require('hast-util-whitespace')
76
var find = require('property-information/find')
87
var hastToReact = require('property-information/hast-to-react.json')
98
var html = require('property-information/html')
@@ -276,7 +275,7 @@ function root(node, context) {
276275
function text(node) {
277276
var value = String(node.value || '')
278277

279-
if (whitespace(node)) return
278+
if (!value) return
280279

281280
return create(node, {
282281
type: 'JSXExpressionContainer',

package.json

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

test.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ test('hast-util-to-estree', function (t) {
263263

264264
t.deepEqual(
265265
toEstree(h('a', ['\n', h('b'), '\n'])),
266-
cleanEstree(ac.parse('<a><b/></a>')),
267-
'should ignore inter-element whitespace'
266+
cleanEstree(ac.parse('<a>{"\\n"}<b/>{"\\n"}</a>')),
267+
'should support inter-element whitespace'
268268
)
269269

270270
t.deepEqual(
@@ -347,6 +347,29 @@ test('hast-util-to-estree', function (t) {
347347
'should support SVG w/ an explicit `space`'
348348
)
349349

350+
t.deepEqual(
351+
toEstree({
352+
type: 'element',
353+
tagName: 'p',
354+
children: [
355+
{
356+
type: 'element',
357+
tagName: 'b',
358+
children: [{type: 'text', value: 'a'}]
359+
},
360+
{type: 'text', value: ' '},
361+
{
362+
type: 'element',
363+
tagName: 'i',
364+
children: [{type: 'text', value: 'b'}]
365+
},
366+
{type: 'text', value: '.'}
367+
]
368+
}),
369+
cleanEstree(ac.parse('<p><b>{"a"}</b>{" "}<i>{"b"}</i>{"."}</p>')),
370+
'should support whitespace between elements'
371+
)
372+
350373
t.deepEqual(
351374
toEstree({type: 'mdxJsxTextElement'}),
352375
cleanEstree(ac.parse('<></>')),
@@ -547,7 +570,7 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
547570

548571
t.deepEqual(
549572
transform('<x>\n - y\n</x>'),
550-
'<><x><ul><li>{"y"}</li></ul></x></>;',
573+
'<><x><ul>{"\\n"}<li>{"y"}</li>{"\\n"}</ul></x></>;',
551574
'should transform children in MDX.js elements'
552575
)
553576

@@ -561,7 +584,7 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
561584
transform(
562585
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!'
563586
),
564-
'import x from "y";\nexport const name = "World";\n<><h2>{"Hello, "}{name}{"!"}</h2></>;',
587+
'import x from "y";\nexport const name = "World";\n<>{"\\n"}<h2>{"Hello, "}{name}{"!"}</h2></>;',
565588
'should transform MDX.js ESM'
566589
)
567590

@@ -576,7 +599,7 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', function (t) {
576599
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!',
577600
true
578601
),
579-
'<><h2>{"Hello, "}{}{"!"}</h2></>;',
602+
'<>{"\\n"}<h2>{"Hello, "}{}{"!"}</h2></>;',
580603
'should transform ESM w/o estrees'
581604
)
582605

@@ -674,7 +697,7 @@ test('integration (@babel/plugin-transform-react-jsx, react)', function (t) {
674697
'export const name = "World";',
675698
'',
676699
'/*#__PURE__*/',
677-
'React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("h2", null, "Hello, ", name, "!"));'
700+
'React.createElement(React.Fragment, null, "\\n", /*#__PURE__*/React.createElement("h2", null, "Hello, ", name, "!"));'
678701
].join('\n'),
679702
'should integrate w/ `@babel/plugin-transform-react-jsx` (MDX.js ESM)'
680703
)
@@ -689,7 +712,7 @@ test('integration (@babel/plugin-transform-react-jsx, react)', function (t) {
689712
'/*#__PURE__*/',
690713
'_jsx(_Fragment, {',
691714
' children: /*#__PURE__*/_jsxs("h1", {',
692-
' children: ["Hi ", /*#__PURE__*/_jsx(Icon, {}), "!"]',
715+
' children: ["Hi ", /*#__PURE__*/_jsx(Icon, {}), " ", "!"]',
693716
' })',
694717
'});'
695718
].join('\n'),
@@ -698,7 +721,7 @@ test('integration (@babel/plugin-transform-react-jsx, react)', function (t) {
698721

699722
t.deepEqual(
700723
transform('# Hi <Icon /> {"!"}', {pragma: 'a', pragmaFrag: 'b'}),
701-
'a(b, null, a("h1", null, "Hi ", a(Icon, null), "!"));',
724+
'a(b, null, a("h1", null, "Hi ", a(Icon, null), " ", "!"));',
702725
'should integrate w/ `@babel/plugin-transform-react-jsx` (pragma, pragmaFrag)'
703726
)
704727

@@ -777,7 +800,7 @@ test('integration (@vue/babel-plugin-jsx, Vue 3)', function (t) {
777800
'import x from "y";',
778801
'export const name = "World";',
779802
'',
780-
'_createVNode(_Fragment, null, [_createVNode("h2", null, ["Hello, ", name, "!"])]);'
803+
'_createVNode(_Fragment, null, ["\\n", _createVNode("h2", null, ["Hello, ", name, "!"])]);'
781804
].join('\n'),
782805
'should integrate w/ `@vue/babel-plugin-jsx` (MDX.js ESM)'
783806
)

0 commit comments

Comments
 (0)