Skip to content

Commit

Permalink
fix: mdxTextExpression as JSXExpressionContainer + JSXEmptyExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 10, 2023
1 parent 6bcfe92 commit 34878f8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"fixtures",
"lib",
"CHANGELOG.md",
"/test.*",
"!/.*.js"
],
"jest": {
Expand Down
32 changes: 25 additions & 7 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
import type {
JSXClosingElement,
JSXElement,
JSXExpressionContainer,
JSXFragment,
JSXIdentifier,
JSXMemberExpression,
Expand Down Expand Up @@ -383,14 +384,31 @@ runAsWorker(
if (child.data && 'estree' in child.data && child.data.estree) {
const estree = child.data.estree as Program

assert(estree.body.length <= 1)
if (child.type === 'mdxTextExpression') {
const {
start: { offset: start },
end: { offset: end },
} = node.position

const expressionContainer: JSXExpressionContainer = {
...normalizeNode(start, end),
type: 'JSXExpressionContainer',
expression: {
...normalizeNode(start + 1, end - 1),
type: 'JSXEmptyExpression',
},
}
acc.push(expressionContainer)
} else {
assert(estree.body.length <= 1)

const expStat = estree.body[0] as ExpressionStatement
const expStat = estree.body[0] as ExpressionStatement

if (expStat) {
const expression =
expStat.expression as BaseExpression as JSXElement['children'][number]
acc.push(expression)
if (expStat) {
const expression =
expStat.expression as BaseExpression as JSXElement['children'][number]
acc.push(expression)
}
}

comments.push(...estree.comments)
Expand Down Expand Up @@ -674,7 +692,7 @@ runAsWorker(
return expression
}

const expression = handleNode(node) as Expression
const expression = handleNode(node)

if (expression) {
body.push({
Expand Down
18 changes: 0 additions & 18 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -972,24 +972,6 @@ exports[`fixtures should match all snapshots: basic.mdx 1`] = `
"ruleId": "prettier/prettier",
"severity": 2,
},
{
"column": 1,
"endColumn": 8,
"endLine": 19,
"fix": {
"range": [
275,
309,
],
"text": " />",
},
"line": 19,
"message": "Empty components are self-closing",
"messageId": "notSelfClosing",
"nodeType": "JSXOpeningElement",
"ruleId": "react/self-closing-comp",
"severity": 2,
},
]
`;
Expand Down
47 changes: 46 additions & 1 deletion test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38806,7 +38806,52 @@ exports[`parser should match all AST snapshots: basic.mdx 1`] = `
{
"end": 309,
"expression": {
"children": [],
"children": [
{
"end": 309,
"expression": {
"end": 308,
"loc": {
"end": {
"column": 39,
"line": 19,
"offset": 308,
},
"start": {
"column": 1,
"line": 19,
"offset": 270,
},
},
"range": [
270,
308,
],
"raw": "Basic>{/* This is a comment */}</Basic",
"start": 270,
"type": "JSXEmptyExpression",
},
"loc": {
"end": {
"column": 40,
"line": 19,
"offset": 309,
},
"start": {
"column": 0,
"line": 19,
"offset": 269,
},
},
"range": [
269,
309,
],
"raw": "<Basic>{/* This is a comment */}</Basic>",
"start": 269,
"type": "JSXExpressionContainer",
},
],
"closingElement": {
"end": 309,
"loc": {
Expand Down

0 comments on commit 34878f8

Please sign in to comment.