Skip to content

Commit c62c541

Browse files
authored
feat: respect .remarkignore at the same time (#561)
close #502
1 parent 68b2028 commit c62c541

File tree

6 files changed

+120
-95
lines changed

6 files changed

+120
-95
lines changed

Diff for: .changeset/great-snakes-nail.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
"eslint-mdx": minor
32
"eslint-plugin-mdx": minor
43
---
54

Diff for: .changeset/lovely-spiders-fetch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-mdx": minor
3+
---
4+
5+
feat: respect `.remarkignore` at the same time

Diff for: package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
"@types/react": "^19.1.0",
4242
"@types/unist": "^3.0.3",
4343
"@unts/patch-package": "^8.1.1",
44-
"@vitest/coverage-istanbul": "^3.1.1",
4544
"concurrently": "^9.1.2",
46-
"eslint": "^9.23.0",
45+
"eslint": "^9.24.0",
4746
"eslint-plugin-react": "^7.37.5",
4847
"globals": "^16.0.0",
4948
"jest": "^30.0.0-alpha.7",
@@ -55,7 +54,7 @@
5554
"simple-git-hooks": "^2.12.1",
5655
"ts-node": "^10.9.2",
5756
"type-coverage": "^2.29.7",
58-
"typescript": "^5.8.2",
57+
"typescript": "^5.8.3",
5958
"yarn-berry-deduplicate": "^6.1.1"
6059
},
6160
"resolutions": {

Diff for: packages/eslint-mdx/src/types.ts

+32
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,35 @@ export interface PackageJson {
7272
name: string
7373
version: string
7474
}
75+
76+
/**
77+
* temporary workaround for missing `Ignore` from `unified-engine`
78+
*
79+
* See also {@link https://github.com/unifiedjs/unified-engine/pull/79}
80+
*
81+
* {@link https://app.unpkg.com/unified-engine@11.2.2/files/lib/ignore.d.ts}
82+
*/
83+
84+
export declare class Ignore {
85+
constructor(options: Options)
86+
check(filePath: string, callback: Callback): void
87+
}
88+
89+
export type IgnoreClass = typeof Ignore
90+
91+
export type Callback = (error?: Error, result?: boolean) => void
92+
93+
export interface Options {
94+
/**
95+
* Base.
96+
*/
97+
cwd: string
98+
/**
99+
* Whether to detect ignore files.
100+
*/
101+
detectIgnore: boolean | undefined
102+
/**
103+
* Basename of ignore files.
104+
*/
105+
ignoreName?: string
106+
}

Diff for: packages/eslint-mdx/src/worker.ts

+43-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
import { restoreTokens } from './tokens.ts'
5454
import type {
5555
Arrayable,
56+
IgnoreClass,
5657
MDXCode,
5758
MDXHeading,
5859
NormalPosition,
@@ -73,18 +74,51 @@ export const processorCache = new Map<
7374
Processor<Root, undefined, undefined, Root, string>
7475
>()
7576

76-
let configLoad: (filePath: string) => Promise<ConfigResult>
77+
const configLoadCache = new Map<
78+
string,
79+
(filePath: string) => Promise<ConfigResult>
80+
>()
81+
82+
let Ignore: IgnoreClass
83+
84+
const ignoreCheckCache = new Map<
85+
string,
86+
(filePath: string) => Promise<boolean>
87+
>()
88+
89+
const getRemarkConfig = async (filePath: string, cwd = process.cwd()) => {
90+
let configLoad = configLoadCache.get(cwd)
7791

78-
const getRemarkConfig = async (filePath: string) => {
7992
if (!configLoad) {
8093
const config = new Configuration({
81-
cwd: process.cwd(),
94+
cwd,
8295
packageField: 'remarkConfig',
8396
pluginPrefix: 'remark',
8497
rcName: '.remarkrc',
8598
detectConfig: true,
8699
})
87100
configLoad = promisify(config.load.bind(config))
101+
configLoadCache.set(cwd, configLoad)
102+
}
103+
104+
if (!Ignore) {
105+
;({ Ignore } = (await import(
106+
pathToFileURL(
107+
path.resolve(cjsRequire.resolve('unified-engine'), '../lib/ignore.js'),
108+
).href
109+
)) as { Ignore: IgnoreClass })
110+
}
111+
112+
let ignoreCheck = ignoreCheckCache.get(cwd)
113+
114+
if (!ignoreCheck) {
115+
const ignore = new Ignore({
116+
cwd,
117+
ignoreName: '.remarkignore',
118+
detectIgnore: true,
119+
})
120+
ignoreCheck = promisify(ignore.check.bind(ignore))
121+
ignoreCheckCache.set(cwd, ignoreCheck)
88122
}
89123

90124
return configLoad(filePath)
@@ -216,6 +250,12 @@ runAsWorker(
216250
}
217251

218252
if (process) {
253+
if (await ignoreCheckCache.get(cwd)(filePath)) {
254+
return {
255+
messages: [],
256+
}
257+
}
258+
219259
const file = new VFile(fileOptions)
220260
try {
221261
await processor.process(file)

0 commit comments

Comments
 (0)