Skip to content

Commit 1ef8b2c

Browse files
committed
fix: adjacent self-closing jsx nodes parsing error - close #138
1 parent a613c3e commit 1ef8b2c

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

packages/eslint-mdx/src/traverse.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { last } from './helper'
2+
import { parser } from './parser'
23
import {
34
isCloseTag,
45
isComment,
@@ -29,7 +30,7 @@ export class Traverse {
2930
}
3031

3132
// fix #7
32-
combineJsxNodes(nodes: Node[]) {
33+
combineJsxNodes(nodes: Node[], parent?: Parent) {
3334
let offset = 0
3435
const jsxNodes: Node[] = []
3536
const { length } = nodes
@@ -41,8 +42,11 @@ export class Traverse {
4142
offset++
4243
jsxNodes.push(node)
4344
} else {
44-
if (isCloseTag(value)) {
45+
if (
46+
isCloseTag(value)
47+
) {
4548
offset--
49+
jsxNodes.push(node)
4650
}
4751
// prettier-ignore
4852
/* istanbul ignore next */
@@ -51,20 +55,26 @@ export class Traverse {
5155
!isSelfClosingTag(value) &&
5256
!isOpenCloseTag(value)
5357
) {
54-
// should never happen, just for robustness
55-
const { start } = node.position
56-
throw Object.assign(
57-
new SyntaxError('unknown jsx node: ' + JSON.stringify(value)),
58-
{
59-
lineNumber: start.line,
60-
column: start.column,
61-
index: start.offset,
62-
},
63-
)
58+
try {
59+
// fix #138
60+
const nodes = parser.normalizeJsxNode(node, parent)
61+
jsxNodes.push(...(Array.isArray(nodes) ? nodes : [nodes]))
62+
} catch {
63+
// should never happen, just for robustness
64+
const { start } = node.position
65+
throw Object.assign(
66+
new SyntaxError('unknown jsx node: ' + JSON.stringify(value)),
67+
{
68+
lineNumber: start.line,
69+
column: start.column,
70+
index: start.offset,
71+
},
72+
)
73+
}
74+
} else {
75+
jsxNodes.push(node)
6476
}
6577

66-
jsxNodes.push(node)
67-
6878
if (!offset) {
6979
acc.push(this.combineLeftJsxNodes(jsxNodes))
7080
jsxNodes.length = 0
@@ -92,8 +102,9 @@ export class Traverse {
92102
let children = node.children as Node[]
93103

94104
if (children) {
95-
children = node.children = this.combineJsxNodes(children)
96-
children.forEach(child => this.traverse(child, node as Parent))
105+
const parent = node as Parent
106+
children = node.children = this.combineJsxNodes(children, parent)
107+
children.forEach(child => this.traverse(child, parent))
97108
}
98109

99110
this._enter(node, parent)

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`fixtures should match all snapshots: adjacent.mdx 1`] = `
4+
"import Basic from './basic'
5+
6+
<Basic />
7+
<Basic />
8+
"
9+
`;
10+
311
exports[`fixtures should match all snapshots: basic.mdx 1`] = `
412
"import Basic from './basic'
513

test/fixtures/adjacent.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Basic from './basic'
2+
3+
<Basic /> <Basic />

0 commit comments

Comments
 (0)