diff --git a/.changeset/dull-bikes-act.md b/.changeset/dull-bikes-act.md new file mode 100644 index 00000000..cc05cea3 --- /dev/null +++ b/.changeset/dull-bikes-act.md @@ -0,0 +1,5 @@ +--- +"eslint-mdx": patch +--- + +fix: pass consistent acorn parser for micromark-extension-mdxjs diff --git a/packages/eslint-mdx/src/worker.ts b/packages/eslint-mdx/src/worker.ts index 5e2b0f2c..9a3472a6 100644 --- a/packages/eslint-mdx/src/worker.ts +++ b/packages/eslint-mdx/src/worker.ts @@ -46,6 +46,10 @@ import type { } from './types' let acorn: typeof import('acorn') +let acornJsx: { + default: typeof import('acorn-jsx') +} +let acornParser: typeof import('acorn').Parser let tokTypes: typeof _tokTypes let jsxTokTypes: Record @@ -60,6 +64,7 @@ const explorer = cosmiconfig('remark', { export const processorCache = new Map() const getRemarkMdxOptions = (tokens: Token[]): Options => ({ + acorn: acornParser, acornOptions: { ecmaVersion: 'latest', sourceType: 'module', @@ -181,6 +186,20 @@ runAsWorker( WorkerOptions): Promise => { sharedTokens.length = 0 + /** + * unified plugins are using ESM version of acorn, + * so we have to use the same version as well, + * otherwise the TokenType will be different class + * @see also https://github.com/acornjs/acorn-jsx/issues/133 + */ + if (!acorn) { + acorn = await loadEsmModule('acorn') + acornJsx = await loadEsmModule<{ default: typeof import('acorn-jsx') }>( + 'acorn-jsx', + ) + acornParser = acorn.Parser.extend(acornJsx.default()) + } + const processor = await getRemarkProcessor( physicalFilename, isMdx, @@ -205,27 +224,13 @@ runAsWorker( } } - /** - * unified plugins are using ESM version of acorn, - * so we have to use the same version as well, - * otherwise the TokenType will be different class - * @see also https://github.com/acornjs/acorn-jsx/issues/133 - */ - if (!acorn) { - acorn = await loadEsmModule('acorn') - } - if (!tokTypes) { tokTypes = acorn.tokTypes } if (!jsxTokTypes) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access - jsxTokTypes = ( - await loadEsmModule<{ default: typeof import('acorn-jsx') }>( - 'acorn-jsx', - ) - ).default( + jsxTokTypes = acornJsx.default( { allowNamespacedObjects: true, },