From 34878f8eb35ec9e930c0df8ba4fd26947d560cae Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 10 Dec 2023 17:59:21 +0800 Subject: [PATCH] fix: mdxTextExpression as JSXExpressionContainer + JSXEmptyExpression --- package.json | 1 + packages/eslint-mdx/src/worker.ts | 32 ++++++++++++---- test/__snapshots__/fixtures.test.ts.snap | 18 --------- test/__snapshots__/parser.test.ts.snap | 47 +++++++++++++++++++++++- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 2ee8bb5..02d3b54 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "fixtures", "lib", "CHANGELOG.md", + "/test.*", "!/.*.js" ], "jest": { diff --git a/packages/eslint-mdx/src/worker.ts b/packages/eslint-mdx/src/worker.ts index 329aed0..d274353 100644 --- a/packages/eslint-mdx/src/worker.ts +++ b/packages/eslint-mdx/src/worker.ts @@ -18,6 +18,7 @@ import type { import type { JSXClosingElement, JSXElement, + JSXExpressionContainer, JSXFragment, JSXIdentifier, JSXMemberExpression, @@ -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) @@ -674,7 +692,7 @@ runAsWorker( return expression } - const expression = handleNode(node) as Expression + const expression = handleNode(node) if (expression) { body.push({ diff --git a/test/__snapshots__/fixtures.test.ts.snap b/test/__snapshots__/fixtures.test.ts.snap index aef064e..9796a1a 100644 --- a/test/__snapshots__/fixtures.test.ts.snap +++ b/test/__snapshots__/fixtures.test.ts.snap @@ -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, - }, ] `; diff --git a/test/__snapshots__/parser.test.ts.snap b/test/__snapshots__/parser.test.ts.snap index e4f8164..b8aac28 100644 --- a/test/__snapshots__/parser.test.ts.snap +++ b/test/__snapshots__/parser.test.ts.snap @@ -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 */}{/* This is a comment */}", + "start": 269, + "type": "JSXExpressionContainer", + }, + ], "closingElement": { "end": 309, "loc": {