Skip to content

Commit

Permalink
Fix unignore statements breaking file filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
comp615 committed Dec 5, 2024
1 parent ab526d0 commit ffe2a7d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions shared/configs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FlatConfigItem, MatchedFile } from './types'
import { Minimatch } from 'minimatch'

const minimatchOpts = { dot: true }
const minimatchOpts = { dot: true, flipNegate: true }
const _matchInstances = new Map<string, Minimatch>()

function minimatch(file: string, pattern: string) {
Expand All @@ -13,8 +13,9 @@ function minimatch(file: string, pattern: string) {
return m.match(file)
}

export function getMatchedGlobs(file: string, glob: (string | string[])[]) {
function getMatchedGlobs(file: string, glob: (string | string[])[]) {
const globs = (Array.isArray(glob) ? glob : [glob]).flat()
// Strip leading `!` from globs since they don't mean invert the match condition, but rather unignore
return globs.filter(glob => minimatch(file, glob)).flat()
}

Expand All @@ -35,12 +36,24 @@ export function isGeneralConfig(config: FlatConfigItem) {
return (!config.files && !config.ignores) || isIgnoreOnlyConfig(config)
}

/**
* Given a list of matched globs, if an unignore (leading !) is the last one, then the file no longer matches the glob set
*/
function filterUnignoreGlobs(globs: string[]) {
if (!globs.length)
return globs
if (globs[globs.length - 1].startsWith('!'))
return []
return globs
}

export function matchFile(
filepath: string,
configs: FlatConfigItem[],
ignoreOnlyConfigs: FlatConfigItem[],
): MatchedFile {
const globalIgnored = ignoreOnlyConfigs.flatMap(config => getMatchedGlobs(filepath, config.ignores!))
const globalIgnored = ignoreOnlyConfigs.flatMap(config => filterUnignoreGlobs(getMatchedGlobs(filepath, config.ignores!)))

if (globalIgnored.length) {
return {
filepath,
Expand All @@ -56,7 +69,8 @@ export function matchFile(
}
configs.forEach((config, index) => {
const positive = getMatchedGlobs(filepath, config.files || [])
const negative = getMatchedGlobs(filepath, config.ignores || [])
const negative = filterUnignoreGlobs(getMatchedGlobs(filepath, config.ignores || []))

if (!negative.length && positive.length)
result.configs.push(index)
result.globs.push(
Expand Down

0 comments on commit ffe2a7d

Please sign in to comment.