Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect .remarkignore at the same time #561

Merged
merged 8 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .changeset/great-snakes-nail.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
"eslint-mdx": minor
"eslint-plugin-mdx": minor
---

Expand Down
5 changes: 5 additions & 0 deletions .changeset/lovely-spiders-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-mdx": minor
---

feat: respect `.remarkignore` at the same time
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
"@types/react": "^19.1.0",
"@types/unist": "^3.0.3",
"@unts/patch-package": "^8.1.1",
"@vitest/coverage-istanbul": "^3.1.1",
"concurrently": "^9.1.2",
"eslint": "^9.23.0",
"eslint": "^9.24.0",
"eslint-plugin-react": "^7.37.5",
"globals": "^16.0.0",
"jest": "^30.0.0-alpha.7",
Expand All @@ -55,7 +54,7 @@
"simple-git-hooks": "^2.12.1",
"ts-node": "^10.9.2",
"type-coverage": "^2.29.7",
"typescript": "^5.8.2",
"typescript": "^5.8.3",
"yarn-berry-deduplicate": "^6.1.1"
},
"resolutions": {
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-mdx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@ export interface PackageJson {
name: string
version: string
}

/**
* temporary workaround for missing `Ignore` from `unified-engine`
*
* See also {@link https://github.com/unifiedjs/unified-engine/pull/79}
*
* {@link https://app.unpkg.com/[email protected]/files/lib/ignore.d.ts}
*/

export declare class Ignore {
constructor(options: Options)
check(filePath: string, callback: Callback): void
}

export type IgnoreClass = typeof Ignore

export type Callback = (error?: Error, result?: boolean) => void

export interface Options {
/**
* Base.
*/
cwd: string
/**
* Whether to detect ignore files.
*/
detectIgnore: boolean | undefined
/**
* Basename of ignore files.
*/
ignoreName?: string
}
46 changes: 43 additions & 3 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
import { restoreTokens } from './tokens.ts'
import type {
Arrayable,
IgnoreClass,
MDXCode,
MDXHeading,
NormalPosition,
Expand All @@ -73,18 +74,51 @@ export const processorCache = new Map<
Processor<Root, undefined, undefined, Root, string>
>()

let configLoad: (filePath: string) => Promise<ConfigResult>
const configLoadCache = new Map<
string,
(filePath: string) => Promise<ConfigResult>
>()

let Ignore: IgnoreClass

const ignoreCheckCache = new Map<
string,
(filePath: string) => Promise<boolean>
>()

const getRemarkConfig = async (filePath: string, cwd = process.cwd()) => {
let configLoad = configLoadCache.get(cwd)

const getRemarkConfig = async (filePath: string) => {
if (!configLoad) {
const config = new Configuration({
cwd: process.cwd(),
cwd,
packageField: 'remarkConfig',
pluginPrefix: 'remark',
rcName: '.remarkrc',
detectConfig: true,
})
configLoad = promisify(config.load.bind(config))
configLoadCache.set(cwd, configLoad)
}

if (!Ignore) {
;({ Ignore } = (await import(
pathToFileURL(
path.resolve(cjsRequire.resolve('unified-engine'), '../lib/ignore.js'),
).href
)) as { Ignore: IgnoreClass })
}

let ignoreCheck = ignoreCheckCache.get(cwd)

if (!ignoreCheck) {
const ignore = new Ignore({
cwd,
ignoreName: '.remarkignore',
detectIgnore: true,
})
ignoreCheck = promisify(ignore.check.bind(ignore))
ignoreCheckCache.set(cwd, ignoreCheck)
}

return configLoad(filePath)
Expand Down Expand Up @@ -216,6 +250,12 @@ runAsWorker(
}

if (process) {
if (await ignoreCheckCache.get(cwd)(filePath)) {
return {
messages: [],
}
}

const file = new VFile(fileOptions)
try {
await processor.process(file)
Expand Down
Loading
Loading