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

fix: match glob logic for unignore ignores #115

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions shared/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,27 @@ function minimatch(file: string, pattern: string) {
return m.match(file)
}

export function getMatchedGlobs(file: string, glob: (string | string[])[]) {
const globs = (Array.isArray(glob) ? glob : [glob]).flat()
return globs.filter(glob => minimatch(file, glob)).flat()
export function getMatchedGlobs(file: string, globs: (string | string[])[]) {
const flatGlobs = (Array.isArray(globs) ? globs : [globs]).flat()
let unmatched: string[] = []

flatGlobs.forEach((glob, i) => {
if (minimatch(file, glob)) {
if (glob.startsWith('!'))
unmatched.push(glob)
}
else {
if (glob.startsWith('!')) {
const unignoreMatched = getMatchedGlobs(file, flatGlobs.slice(0, i))
unmatched = unmatched.concat(unignoreMatched.length > 0 ? unignoreMatched : [], glob)
}
else {
unmatched.push(glob)
}
}
})

return flatGlobs.filter(glob => !unmatched.includes(glob))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the order of ignore and unignore might matter how it work. For the must accurate result, we might need to vendor ESLint's walker here: https://github.com/eslint/eslint/blob/5db226f4da9ad7d53a4505a90290b68d4036c082/lib/eslint/eslint-helpers.js#L216

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antfu I just updated the PR

I took a different approach, cos i realised that @eslint/config-array has a very convenient isFileIgnored here, we can use this to decide if a file is ignored, and that should fix the issue entirely.

I don't think we need to vendor the eslint-helper since it's not exported and there are a long chain of dependencies in eslint's implementations.

}

const META_KEYS = new Set(['name', 'index'])
Expand All @@ -40,7 +58,9 @@ export function matchFile(
configs: FlatConfigItem[],
ignoreOnlyConfigs: FlatConfigItem[],
): MatchedFile {
const globalIgnored = ignoreOnlyConfigs.flatMap(config => getMatchedGlobs(filepath, config.ignores!))
const globIgnoreBlobs = ignoreOnlyConfigs.flatMap(config => config.ignores ?? [])
const globalIgnored = getMatchedGlobs(filepath, globIgnoreBlobs)

if (globalIgnored.length) {
return {
filepath,
Expand Down