Skip to content

Commit

Permalink
fix: pass consistent acorn parser for micromark-extension-mdxjs (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Sep 2, 2022
1 parent d321cf2 commit 2ae592a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-bikes-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-mdx": patch
---

fix: pass consistent acorn parser for micromark-extension-mdxjs
35 changes: 20 additions & 15 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TokenType>
Expand All @@ -60,6 +64,7 @@ const explorer = cosmiconfig('remark', {
export const processorCache = new Map<string, FrozenProcessor>()

const getRemarkMdxOptions = (tokens: Token[]): Options => ({
acorn: acornParser,
acornOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
Expand Down Expand Up @@ -181,6 +186,20 @@ runAsWorker(
WorkerOptions): Promise<WorkerResult> => {
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<typeof import('acorn')>('acorn')
acornJsx = await loadEsmModule<{ default: typeof import('acorn-jsx') }>(
'acorn-jsx',
)
acornParser = acorn.Parser.extend(acornJsx.default())
}

const processor = await getRemarkProcessor(
physicalFilename,
isMdx,
Expand All @@ -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<typeof import('acorn')>('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,
},
Expand Down

0 comments on commit 2ae592a

Please sign in to comment.