Skip to content

Commit

Permalink
fix: use config array built in ignore check
Browse files Browse the repository at this point in the history
  • Loading branch information
yidingww committed Dec 26, 2024
1 parent fc2ea9a commit 0420d93
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
5 changes: 3 additions & 2 deletions app/pages/configs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useRoute } from '#app/composables/router'
import { debouncedWatch } from '@vueuse/core'
import Fuse from 'fuse.js'
import { computed, defineComponent, h, nextTick, onMounted, ref, shallowRef, watch, watchEffect } from 'vue'
import { isIgnoreOnlyConfig, matchFile } from '~~/shared/configs'
import { buildConfigArray, isIgnoreOnlyConfig, matchFile } from '~~/shared/configs'
import { getRuleLevel } from '~~/shared/rules'
import { payload } from '~/composables/payload'
import { configsOpenState, filtersConfigs as filters, stateStorage } from '~/composables/state'
Expand All @@ -24,6 +24,7 @@ function collapseAll() {
const filteredConfigs = shallowRef<FlatConfigItem[]>([])
const fileMatchResult = shallowRef<MatchedFile | null>(null)
const configArray = buildConfigArray(payload.value.configs, payload.value.meta.basePath)
watchEffect(() => {
let configs = payload.value.configs
Expand All @@ -32,7 +33,7 @@ watchEffect(() => {
fileMatchResult.value = matchFile(
filters.filepath,
payload.value.configs,
payload.value.configsIgnoreOnly,
configArray,
)
if (fileMatchResult.value.configs.length) {
configs = Array.from(new Set([
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineNuxtConfig({

experimental: {
typedPages: true,
clientNodeCompat: true,
},

features: {
Expand Down
47 changes: 32 additions & 15 deletions shared/configs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FlatConfigItem, MatchedFile } from './types'
import { ConfigArray } from '@eslint/config-array'
import { Minimatch } from 'minimatch'

const minimatchOpts = { dot: true }
Expand Down Expand Up @@ -38,31 +39,47 @@ export function isGeneralConfig(config: FlatConfigItem) {
export function matchFile(
filepath: string,
configs: FlatConfigItem[],
ignoreOnlyConfigs: FlatConfigItem[],
configArray: ConfigArray,
): MatchedFile {
const globalIgnored = ignoreOnlyConfigs.flatMap(config => getMatchedGlobs(filepath, config.ignores!))
if (globalIgnored.length) {
return {
filepath,
globs: globalIgnored,
configs: [],
}
}

const result: MatchedFile = {
filepath,
globs: [],
configs: [],
}

configs.forEach((config, index) => {
const positive = getMatchedGlobs(filepath, config.files || [])
const negative = getMatchedGlobs(filepath, config.ignores || [])
if (!negative.length && positive.length)
if (configArray && !configArray.isFileIgnored(filepath) && positive.length > 0) {
result.configs.push(index)
result.globs.push(
...positive,
...negative,
)
// push positive globs only when there are configs matched
result.globs.push(...positive)
}
// push negative globs except for unignore globs
result.globs.push(...negative.filter(glob => !glob.startsWith('!')))
})

return result
}

const NOOP_SCHEMA = {
merge: 'replace',
validate() {},
}

const FLAT_CONFIG_NOOP_SCHEMA = {
settings: NOOP_SCHEMA,
linterOptions: NOOP_SCHEMA,
language: NOOP_SCHEMA,
languageOptions: NOOP_SCHEMA,
processor: NOOP_SCHEMA,
plugins: NOOP_SCHEMA,
rules: NOOP_SCHEMA,
}

export function buildConfigArray(configs: FlatConfigItem[], basePath: string) {
return new ConfigArray(configs.map(({ index: _, ...c }) => c), {
basePath,
schema: FLAT_CONFIG_NOOP_SCHEMA,
}).normalizeSync()
}
45 changes: 5 additions & 40 deletions src/configs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { FlatConfigItem, MatchedFile, Payload, RuleInfo } from '../shared/types'
import { basename, dirname, relative, resolve } from 'node:path'
import process from 'node:process'
import { ConfigArray } from '@eslint/config-array'
import { configArrayFindFiles } from '@voxpelli/config-array-find-files'
import { bundleRequire } from 'bundle-require'
import { findUp } from 'find-up'
import { resolve as resolveModule } from 'mlly'
import c from 'picocolors'
import { isIgnoreOnlyConfig, matchFile } from '../shared/configs'
import { buildConfigArray, matchFile } from '../shared/configs'
import { configFilenames, legacyConfigFilenames, MARK_CHECK, MARK_INFO } from './constants'
import { ConfigPathError, ConfigPathLegacyError } from './errors'

Expand Down Expand Up @@ -235,56 +234,22 @@ export async function readConfig(
}
}

const noopSchema = {
merge: 'replace',
validate() {},
}

const flatConfigNoopSchema = {
settings: noopSchema,
linterOptions: noopSchema,
language: noopSchema,
languageOptions: noopSchema,
processor: noopSchema,
plugins: noopSchema,
rules: noopSchema,
}

export async function globMatchedFiles(
basePath: string,
configs: FlatConfigItem[],
): Promise<MatchedFile[]> {
console.log(MARK_INFO, 'Globing matched files')

const configArray = new ConfigArray(configs, {
basePath,
schema: flatConfigNoopSchema,
})

await configArray.normalize()

const files = await configArrayFindFiles({
const configArray = buildConfigArray(configs, basePath)
const files = (await configArrayFindFiles({
basePath,
configs: configArray,
})

files.sort()

const ignoreOnlyConfigs = configs.filter(isIgnoreOnlyConfig)
// const functionalGlobMap = new Map<any, string>()
// function stringifyGlob(glob: string) {
// if (typeof glob === 'function') {
// if (!functionalGlobMap.has(glob))
// functionalGlobMap.set(glob, `<function#${functionalGlobMap.size + 1}>`)
// return functionalGlobMap.get(glob)!
// }
// return glob
// }
})).toSorted()

return files
.map((filepath) => {
filepath = relative(basePath, filepath)
const result = matchFile(filepath, configs, ignoreOnlyConfigs)
const result = matchFile(filepath, configs, configArray)
if (!result.configs.length)
return undefined
return result
Expand Down

0 comments on commit 0420d93

Please sign in to comment.