Skip to content

Commit 2ae592a

Browse files
authored
fix: pass consistent acorn parser for micromark-extension-mdxjs (#424)
1 parent d321cf2 commit 2ae592a

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

.changeset/dull-bikes-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-mdx": patch
3+
---
4+
5+
fix: pass consistent acorn parser for micromark-extension-mdxjs

packages/eslint-mdx/src/worker.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ import type {
4646
} from './types'
4747

4848
let acorn: typeof import('acorn')
49+
let acornJsx: {
50+
default: typeof import('acorn-jsx')
51+
}
52+
let acornParser: typeof import('acorn').Parser
4953

5054
let tokTypes: typeof _tokTypes
5155
let jsxTokTypes: Record<string, TokenType>
@@ -60,6 +64,7 @@ const explorer = cosmiconfig('remark', {
6064
export const processorCache = new Map<string, FrozenProcessor>()
6165

6266
const getRemarkMdxOptions = (tokens: Token[]): Options => ({
67+
acorn: acornParser,
6368
acornOptions: {
6469
ecmaVersion: 'latest',
6570
sourceType: 'module',
@@ -181,6 +186,20 @@ runAsWorker(
181186
WorkerOptions): Promise<WorkerResult> => {
182187
sharedTokens.length = 0
183188

189+
/**
190+
* unified plugins are using ESM version of acorn,
191+
* so we have to use the same version as well,
192+
* otherwise the TokenType will be different class
193+
* @see also https://github.com/acornjs/acorn-jsx/issues/133
194+
*/
195+
if (!acorn) {
196+
acorn = await loadEsmModule<typeof import('acorn')>('acorn')
197+
acornJsx = await loadEsmModule<{ default: typeof import('acorn-jsx') }>(
198+
'acorn-jsx',
199+
)
200+
acornParser = acorn.Parser.extend(acornJsx.default())
201+
}
202+
184203
const processor = await getRemarkProcessor(
185204
physicalFilename,
186205
isMdx,
@@ -205,27 +224,13 @@ runAsWorker(
205224
}
206225
}
207226

208-
/**
209-
* unified plugins are using ESM version of acorn,
210-
* so we have to use the same version as well,
211-
* otherwise the TokenType will be different class
212-
* @see also https://github.com/acornjs/acorn-jsx/issues/133
213-
*/
214-
if (!acorn) {
215-
acorn = await loadEsmModule<typeof import('acorn')>('acorn')
216-
}
217-
218227
if (!tokTypes) {
219228
tokTypes = acorn.tokTypes
220229
}
221230

222231
if (!jsxTokTypes) {
223232
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
224-
jsxTokTypes = (
225-
await loadEsmModule<{ default: typeof import('acorn-jsx') }>(
226-
'acorn-jsx',
227-
)
228-
).default(
233+
jsxTokTypes = acornJsx.default(
229234
{
230235
allowNamespacedObjects: true,
231236
},

0 commit comments

Comments
 (0)