Skip to content

Commit 0feb2d3

Browse files
authored
fix: should only fallback on parse error (#335)
close #334
1 parent 442cc02 commit 0feb2d3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

packages/eslint-mdx/src/traverse.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export class Traverse {
2626
// fix #279
2727
if (parent && parent.position.indent?.length > 0) {
2828
end.offset += parent.position.indent.reduce(
29-
(acc, indent, index) => acc + (index ? indent + 1 : 0),
29+
(acc, indent, index) =>
30+
acc + (index ? /* istanbul ignore next */ indent + 1 : 0),
3031
0,
3132
)
3233
}
@@ -66,15 +67,17 @@ export class Traverse {
6667
) {
6768
jsxNodes.push(node)
6869
} else {
69-
// #272, we consider the first jsx node as open tag although it's not precise
70-
if (!index) {
71-
offset++
72-
hasOpenTag = true
73-
}
7470
try {
7571
// fix #138
7672
jsxNodes.push(...arrayify(parser.normalizeJsxNode(node, parent)))
7773
} catch {
74+
// #272, we consider the first jsx node as open tag although it's not precise
75+
// and #334, if there is no error thrown, do not fallback
76+
if (!index) {
77+
offset++
78+
hasOpenTag = true
79+
}
80+
7881
// #272 related
7982
/* istanbul ignore else */
8083
if (offset) {

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ exports[`fixtures should match all snapshots: 287.mdx 1`] = `Array []`;
6969

7070
exports[`fixtures should match all snapshots: 292.mdx 1`] = `Array []`;
7171

72+
exports[`fixtures should match all snapshots: 334.mdx 1`] = `Array []`;
73+
7274
exports[`fixtures should match all snapshots: adjacent.mdx 1`] = `Array []`;
7375

7476
exports[`fixtures should match all snapshots: basic.mdx 1`] = `Array []`;

test/fixtures/334.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
<img />
3+
</div>
4+
5+
```js
6+
module.exports = {
7+
plugins: [],
8+
}
9+
```

0 commit comments

Comments
 (0)