Skip to content

Commit

Permalink
fix: parse error on void boolean attributes (#431)
Browse files Browse the repository at this point in the history
close #429
  • Loading branch information
JounQin committed Sep 14, 2022
1 parent e3ebd9c commit bebc564
Show file tree
Hide file tree
Showing 6 changed files with 716 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-squids-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-mdx": patch
---

fix: parse error on void boolean attributes
22 changes: 17 additions & 5 deletions packages/eslint-mdx/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const restoreTokens = (
const nodeStart = nodePos.start.offset
const nodeEnd = nodePos.end.offset

const lastCharOffset = prevCharOffset(nodeEnd - 2)

assert(nodeStart != null)
assert(nodeEnd != null)

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

const selfClosing = text[lastCharOffset] === '/'

let nodeNameStart = nodeStart + 1

if (nodeName) {
Expand Down Expand Up @@ -200,15 +204,23 @@ export const restoreTokens = (
assert(text[lastAttrOffset] === attrQuote)
}

const nextOffset = nextCharOffset(lastAttrOffset + 1)
let nextOffset = nextCharOffset(lastAttrOffset + 1)
let nextChar = text[nextOffset]

const nextChar = text[nextOffset]
const expectedNextChar = selfClosing ? '/' : '>'

// self closing tag
if (nextChar === '/') {
if (nextChar !== expectedNextChar) {
nextOffset = /** @type {number} */ nextCharOffset(lastAttrOffset)
nextChar = text[nextOffset]
}

if (selfClosing) {
tokens.push(newToken(tt.slash, nextOffset, nextOffset + 1, '/'))
} else {
assert(nextChar === '>')
assert(
nextChar === '>',
`\`nextChar\` must be '>' but actually is '${nextChar}'`,
)

const prevOffset = prevCharOffset(nodeEnd - 2)

Expand Down
15 changes: 12 additions & 3 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,20 @@ runAsWorker(
children,
}

const nextOffset = nextCharOffset(lastAttrOffset + 1)
let nextOffset = nextCharOffset(lastAttrOffset + 1)
let nextChar = text[nextOffset]

const nextChar = text[nextOffset]
const expectedNextChar = selfClosing ? '/' : '>'

assert(nextChar === (selfClosing ? '/' : '>'))
if (nextChar !== expectedNextChar) {
nextOffset = /** @type {number} */ nextCharOffset(lastAttrOffset)
nextChar = text[nextOffset]
}

assert(
nextChar === expectedNextChar,
`\`nextChar\` must be '${expectedNextChar}' but actually is '${nextChar}'`,
)

Object.assign(
jsxEl.openingElement,
Expand Down
23 changes: 23 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,29 @@ exports[`fixtures should match all snapshots: 391.mdx 1`] = `
]
`;
exports[`fixtures should match all snapshots: 429.mdx 1`] = `
[
{
"column": 17,
"endColumn": 17,
"endLine": 3,
"fix": {
"range": [
49,
49,
],
"text": " ",
},
"line": 3,
"message": "Insert \`·\`",
"messageId": "insert",
"nodeType": null,
"ruleId": "prettier/prettier",
"severity": 2,
},
]
`;
exports[`fixtures should match all snapshots: acorn.mdx 1`] = `
[
{
Expand Down
Loading

0 comments on commit bebc564

Please sign in to comment.