Skip to content

Commit 58db688

Browse files
committed
fix: use unified performSyncWork for parsing with remark config
close #576
1 parent 27da917 commit 58db688

File tree

8 files changed

+175
-19
lines changed

8 files changed

+175
-19
lines changed

.changeset/sour-icons-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-mdx": patch
3+
---
4+
5+
fix: use unified `performSyncWork` for parsing with remark config

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"path-serializer": "^0.5.0",
5252
"prettier": "^3.5.3",
5353
"react": "^19.1.0",
54+
"remark-math": "^6.0.0",
5455
"rimraf": "^6.0.1",
5556
"simple-git-hooks": "^2.13.0",
5657
"ts-node": "^10.9.2",
@@ -102,7 +103,8 @@
102103
"prettier": "@1stg/prettier-config",
103104
"remarkConfig": {
104105
"plugins": [
105-
"@1stg/preset"
106+
"@1stg/preset",
107+
"remark-math"
106108
]
107109
},
108110
"typeCoverage": {

packages/eslint-plugin-mdx/src/from-markdown.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/eslint-plugin-mdx/src/processors/markdown.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/** {@link https://github.com/eslint/markdown/blob/f17311eb250fe3f41a24fd52be9c6713d12b7d67/src/processor.js} */
22

3+
import fs from 'node:fs'
4+
import path from 'node:path'
5+
36
import type { AST, Linter, Rule } from 'eslint'
7+
import { performSyncWork } from 'eslint-mdx'
48
import type { Node, Parent, Nodes, Root, RootContentMap } from 'mdast'
59
import type { MdxFlowExpression, MdxTextExpression } from 'mdast-util-mdx'
610

7-
import { fromMarkdown } from '../from-markdown.js'
811
import { meta } from '../meta.js'
912

1013
import type { CodeBlock, RangeMap } from './types.js'
@@ -262,22 +265,43 @@ function fileNameFromMeta(block: CodeBlock) {
262265
?.groups.filename.replaceAll(/\s+/gu, '_')
263266
}
264267

268+
/**
269+
* Given a filepath, get the nearest path that is a regular file. The filepath
270+
* provided by eslint may be a virtual filepath rather than a file on disk. This
271+
* attempts to transform a virtual path into an on-disk path
272+
*/
273+
function getOnDiskFilepath(filepath: string): string {
274+
let fallback: boolean | undefined
275+
try {
276+
/* istanbul ignore next -- `throwIfNoEntry` compatibility */
277+
if (!fs.statSync(filepath, { throwIfNoEntry: false })) {
278+
fallback = true
279+
}
280+
} catch (err) {
281+
/* istanbul ignore next -- https://github.com/eslint/eslint/issues/11989 */
282+
if ((err as NodeJS.ErrnoException).code === 'ENOTDIR') {
283+
fallback = true
284+
}
285+
}
286+
return fallback ? getOnDiskFilepath(path.dirname(filepath)) : filepath
287+
}
288+
265289
/**
266290
* Extracts lintable code blocks from Markdown text.
267291
*
268292
* @param sourceText The text of the file.
269293
* @param filename The filename of the file
270294
* @returns Source code blocks to lint.
271295
*/
272-
273296
function preprocess(sourceText: string, filename: string) {
274297
// istanbul ignore next
275298
const text = sourceText.startsWith(BOM) ? sourceText.slice(1) : sourceText
276-
const ast = fromMarkdown(
277-
text,
299+
const { root } = performSyncWork({
300+
filePath: getOnDiskFilepath(filename),
301+
code: text,
278302
// FIXME: how to read `extensions` and `markdownExtensions` parser options?
279-
filename.endsWith('.mdx'),
280-
)
303+
isMdx: filename.endsWith('.mdx'),
304+
})
281305
const blocks: CodeBlock[] = []
282306

283307
blocksCache.set(filename, blocks)
@@ -300,7 +324,7 @@ function preprocess(sourceText: string, filename: string) {
300324
}
301325
}
302326

303-
traverse(ast, {
327+
traverse(root, {
304328
'*'() {
305329
allComments = []
306330
},

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ exports[`fixtures should match all snapshots: 488.mdx 1`] = `
12241224
]
12251225
`;
12261226
1227+
exports[`fixtures should match all snapshots: 576.mdx 1`] = `[]`;
1228+
12271229
exports[`fixtures should match all snapshots: acorn.mdx 1`] = `
12281230
[
12291231
{

test/__snapshots__/parser.test.ts.snap

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`parser should match all AST snapshots: 287.mdx 1`] = `
44
{
@@ -36769,6 +36769,55 @@ exports[`parser should match all AST snapshots: 488.mdx 1`] = `
3676936769
}
3677036770
`;
3677136771

36772+
exports[`parser should match all AST snapshots: 576.mdx 1`] = `
36773+
{
36774+
"body": [],
36775+
"comments": [],
36776+
"end": 159,
36777+
"loc": {
36778+
"end": {
36779+
"column": 1,
36780+
"line": 6,
36781+
"offset": 159,
36782+
},
36783+
"start": {
36784+
"column": 1,
36785+
"line": 1,
36786+
"offset": 0,
36787+
},
36788+
},
36789+
"range": [
36790+
0,
36791+
159,
36792+
],
36793+
"sourceType": undefined,
36794+
"start": 0,
36795+
"tokens": [
36796+
{
36797+
"end": 143,
36798+
"loc": {
36799+
"end": {
36800+
"column": 30,
36801+
"line": 5,
36802+
},
36803+
"start": {
36804+
"column": 0,
36805+
"line": 5,
36806+
},
36807+
},
36808+
"range": [
36809+
113,
36810+
143,
36811+
],
36812+
"start": 113,
36813+
"type": "JSXText",
36814+
"value": "the aggregate amount would be ",
36815+
},
36816+
],
36817+
"type": "Program",
36818+
}
36819+
`;
36820+
3677236821
exports[`parser should match all AST snapshots: acorn.mdx 1`] = `"Unexpected character \`\\\` (U+005C) in name, expected a name character such as letters, digits, \`$\`, or \`_\`; whitespace before attributes; or the end of the tag"`;
3677336822

3677436823
exports[`parser should match all AST snapshots: adjacent.mdx 1`] = `

test/fixtures/576.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$$
2+
\forall \epsilon > 0, \exists \delta > 0 \text{ s.t. } |x - a| < \delta \Rightarrow |f(x) - b| < \epsilon
3+
$$
4+
5+
the aggregate amount would be $100Mm*10^{18}$

yarn.lock

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3433,6 +3433,7 @@ __metadata:
34333433
path-serializer: "npm:^0.5.0"
34343434
prettier: "npm:^3.5.3"
34353435
react: "npm:^19.1.0"
3436+
remark-math: "npm:^6.0.0"
34363437
rimraf: "npm:^6.0.1"
34373438
simple-git-hooks: "npm:^2.13.0"
34383439
ts-node: "npm:^10.9.2"
@@ -4571,6 +4572,13 @@ __metadata:
45714572
languageName: node
45724573
linkType: hard
45734574

4575+
"@types/katex@npm:^0.16.0":
4576+
version: 0.16.7
4577+
resolution: "@types/katex@npm:0.16.7"
4578+
checksum: 10c0/68dcb9f68a90513ec78ca0196a142e15c2a2c270b1520d752bafd47a99207115085a64087b50140359017d7e9c870b3c68e7e4d36668c9e348a9ef0c48919b5a
4579+
languageName: node
4580+
linkType: hard
4581+
45744582
"@types/keyv@npm:^3.1.4":
45754583
version: 3.1.4
45764584
resolution: "@types/keyv@npm:3.1.4"
@@ -6325,6 +6333,13 @@ __metadata:
63256333
languageName: node
63266334
linkType: hard
63276335

6336+
"commander@npm:^8.3.0":
6337+
version: 8.3.0
6338+
resolution: "commander@npm:8.3.0"
6339+
checksum: 10c0/8b043bb8322ea1c39664a1598a95e0495bfe4ca2fad0d84a92d7d1d8d213e2a155b441d2470c8e08de7c4a28cf2bc6e169211c49e1b21d9f7edc6ae4d9356060
6340+
languageName: node
6341+
linkType: hard
6342+
63286343
"commander@npm:^9.4.1":
63296344
version: 9.5.0
63306345
resolution: "commander@npm:9.5.0"
@@ -10157,6 +10172,17 @@ __metadata:
1015710172
languageName: node
1015810173
linkType: hard
1015910174

10175+
"katex@npm:^0.16.0":
10176+
version: 0.16.22
10177+
resolution: "katex@npm:0.16.22"
10178+
dependencies:
10179+
commander: "npm:^8.3.0"
10180+
bin:
10181+
katex: cli.js
10182+
checksum: 10c0/07b8b1f07ae53171b5f1ea0cf6f18841d2055825c8b11cd81cfe039afcd3af2cfc84ad033531ee3875088329105195b039c267e0dd4b0c237807e3c3b2009913
10183+
languageName: node
10184+
linkType: hard
10185+
1016010186
"keyv@npm:^4.0.0, keyv@npm:^4.5.4":
1016110187
version: 4.5.4
1016210188
resolution: "keyv@npm:4.5.4"
@@ -10694,6 +10720,21 @@ __metadata:
1069410720
languageName: node
1069510721
linkType: hard
1069610722

10723+
"mdast-util-math@npm:^3.0.0":
10724+
version: 3.0.0
10725+
resolution: "mdast-util-math@npm:3.0.0"
10726+
dependencies:
10727+
"@types/hast": "npm:^3.0.0"
10728+
"@types/mdast": "npm:^4.0.0"
10729+
devlop: "npm:^1.0.0"
10730+
longest-streak: "npm:^3.0.0"
10731+
mdast-util-from-markdown: "npm:^2.0.0"
10732+
mdast-util-to-markdown: "npm:^2.1.0"
10733+
unist-util-remove-position: "npm:^5.0.0"
10734+
checksum: 10c0/d4e839e38719f26872ed78aac18339805a892f1b56585a9cb8668f34e221b4f0660b9dfe49ec96dbbe79fd1b63b648608a64046d8286bcd2f9d576e80b48a0a1
10735+
languageName: node
10736+
linkType: hard
10737+
1069710738
"mdast-util-mdx-expression@npm:^2.0.0":
1069810739
version: 2.0.1
1069910740
resolution: "mdast-util-mdx-expression@npm:2.0.1"
@@ -10782,7 +10823,7 @@ __metadata:
1078210823
languageName: node
1078310824
linkType: hard
1078410825

10785-
"mdast-util-to-markdown@npm:^2.0.0":
10826+
"mdast-util-to-markdown@npm:^2.0.0, mdast-util-to-markdown@npm:^2.1.0":
1078610827
version: 2.1.2
1078710828
resolution: "mdast-util-to-markdown@npm:2.1.2"
1078810829
dependencies:
@@ -11026,6 +11067,21 @@ __metadata:
1102611067
languageName: node
1102711068
linkType: hard
1102811069

11070+
"micromark-extension-math@npm:^3.0.0":
11071+
version: 3.1.0
11072+
resolution: "micromark-extension-math@npm:3.1.0"
11073+
dependencies:
11074+
"@types/katex": "npm:^0.16.0"
11075+
devlop: "npm:^1.0.0"
11076+
katex: "npm:^0.16.0"
11077+
micromark-factory-space: "npm:^2.0.0"
11078+
micromark-util-character: "npm:^2.0.0"
11079+
micromark-util-symbol: "npm:^2.0.0"
11080+
micromark-util-types: "npm:^2.0.0"
11081+
checksum: 10c0/56e6f2185a4613f9d47e7e98cf8605851c990957d9229c942b005e286c8087b61dc9149448d38b2f8be6d42cc6a64aad7e1f2778ddd86fbbb1a2f48a3ca1872f
11082+
languageName: node
11083+
linkType: hard
11084+
1102911085
"micromark-extension-mdx-expression@npm:^3.0.0":
1103011086
version: 3.0.1
1103111087
resolution: "micromark-extension-mdx-expression@npm:3.0.1"
@@ -13496,6 +13552,18 @@ __metadata:
1349613552
languageName: node
1349713553
linkType: hard
1349813554

13555+
"remark-math@npm:^6.0.0":
13556+
version: 6.0.0
13557+
resolution: "remark-math@npm:6.0.0"
13558+
dependencies:
13559+
"@types/mdast": "npm:^4.0.0"
13560+
mdast-util-math: "npm:^3.0.0"
13561+
micromark-extension-math: "npm:^3.0.0"
13562+
unified: "npm:^11.0.0"
13563+
checksum: 10c0/859613c4db194bb6b3c9c063661dc52b8ceda9c5cf3256b42f73d93eb8f38a6d634eb5f976fe094425f6f1035aaf329eb49ada314feb3b2b1073326b6d3aaa02
13564+
languageName: node
13565+
linkType: hard
13566+
1349913567
"remark-mdx@npm:^3.1.0":
1350013568
version: 3.1.0
1350113569
resolution: "remark-mdx@npm:3.1.0"
@@ -15272,6 +15340,16 @@ __metadata:
1527215340
languageName: node
1527315341
linkType: hard
1527415342

15343+
"unist-util-remove-position@npm:^5.0.0":
15344+
version: 5.0.0
15345+
resolution: "unist-util-remove-position@npm:5.0.0"
15346+
dependencies:
15347+
"@types/unist": "npm:^3.0.0"
15348+
unist-util-visit: "npm:^5.0.0"
15349+
checksum: 10c0/e8c76da4399446b3da2d1c84a97c607b37d03d1d92561e14838cbe4fdcb485bfc06c06cfadbb808ccb72105a80643976d0660d1fe222ca372203075be9d71105
15350+
languageName: node
15351+
linkType: hard
15352+
1527515353
"unist-util-stringify-position@npm:^4.0.0":
1527615354
version: 4.0.0
1527715355
resolution: "unist-util-stringify-position@npm:4.0.0"

0 commit comments

Comments
 (0)