Skip to content

Commit bebc564

Browse files
authored
fix: parse error on void boolean attributes (#431)
close #429
1 parent e3ebd9c commit bebc564

File tree

6 files changed

+716
-48
lines changed

6 files changed

+716
-48
lines changed

.changeset/khaki-squids-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-mdx": patch
3+
---
4+
5+
fix: parse error on void boolean attributes

packages/eslint-mdx/src/tokens.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export const restoreTokens = (
5757
const nodeStart = nodePos.start.offset
5858
const nodeEnd = nodePos.end.offset
5959

60+
const lastCharOffset = prevCharOffset(nodeEnd - 2)
61+
6062
assert(nodeStart != null)
6163
assert(nodeEnd != null)
6264

@@ -79,6 +81,8 @@ export const restoreTokens = (
7981
const nodeName = node.name
8082
const nodeNameLength = nodeName?.length ?? 0
8183

84+
const selfClosing = text[lastCharOffset] === '/'
85+
8286
let nodeNameStart = nodeStart + 1
8387

8488
if (nodeName) {
@@ -200,15 +204,23 @@ export const restoreTokens = (
200204
assert(text[lastAttrOffset] === attrQuote)
201205
}
202206

203-
const nextOffset = nextCharOffset(lastAttrOffset + 1)
207+
let nextOffset = nextCharOffset(lastAttrOffset + 1)
208+
let nextChar = text[nextOffset]
204209

205-
const nextChar = text[nextOffset]
210+
const expectedNextChar = selfClosing ? '/' : '>'
206211

207-
// self closing tag
208-
if (nextChar === '/') {
212+
if (nextChar !== expectedNextChar) {
213+
nextOffset = /** @type {number} */ nextCharOffset(lastAttrOffset)
214+
nextChar = text[nextOffset]
215+
}
216+
217+
if (selfClosing) {
209218
tokens.push(newToken(tt.slash, nextOffset, nextOffset + 1, '/'))
210219
} else {
211-
assert(nextChar === '>')
220+
assert(
221+
nextChar === '>',
222+
`\`nextChar\` must be '>' but actually is '${nextChar}'`,
223+
)
212224

213225
const prevOffset = prevCharOffset(nodeEnd - 2)
214226

packages/eslint-mdx/src/worker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,20 @@ runAsWorker(
526526
children,
527527
}
528528

529-
const nextOffset = nextCharOffset(lastAttrOffset + 1)
529+
let nextOffset = nextCharOffset(lastAttrOffset + 1)
530+
let nextChar = text[nextOffset]
530531

531-
const nextChar = text[nextOffset]
532+
const expectedNextChar = selfClosing ? '/' : '>'
532533

533-
assert(nextChar === (selfClosing ? '/' : '>'))
534+
if (nextChar !== expectedNextChar) {
535+
nextOffset = /** @type {number} */ nextCharOffset(lastAttrOffset)
536+
nextChar = text[nextOffset]
537+
}
538+
539+
assert(
540+
nextChar === expectedNextChar,
541+
`\`nextChar\` must be '${expectedNextChar}' but actually is '${nextChar}'`,
542+
)
534543

535544
Object.assign(
536545
jsxEl.openingElement,

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,29 @@ exports[`fixtures should match all snapshots: 391.mdx 1`] = `
394394
]
395395
`;
396396
397+
exports[`fixtures should match all snapshots: 429.mdx 1`] = `
398+
[
399+
{
400+
"column": 17,
401+
"endColumn": 17,
402+
"endLine": 3,
403+
"fix": {
404+
"range": [
405+
49,
406+
49,
407+
],
408+
"text": " ",
409+
},
410+
"line": 3,
411+
"message": "Insert \`·\`",
412+
"messageId": "insert",
413+
"nodeType": null,
414+
"ruleId": "prettier/prettier",
415+
"severity": 2,
416+
},
417+
]
418+
`;
419+
397420
exports[`fixtures should match all snapshots: acorn.mdx 1`] = `
398421
[
399422
{

0 commit comments

Comments
 (0)