-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve files globs matching display
- Loading branch information
Showing
8 changed files
with
98 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<script setup lang="ts"> | ||
import type { Linter } from 'eslint' | ||
defineProps<{ | ||
glob: Linter.FlatConfigFileSpec | Linter.FlatConfigFileSpec[] | ||
}>() | ||
withDefaults( | ||
defineProps<{ | ||
glob: Linter.FlatConfigFileSpec | ||
active?: boolean | null | ||
}>(), | ||
{ active: null }, | ||
) | ||
</script> | ||
|
||
<template> | ||
<div border="~ base rounded" bg-gray:5 px2 font-mono op75> | ||
<div | ||
border="~ rounded" px2 font-mono | ||
:class="active === true ? 'border-amber:50 text-amber bg-amber:5' : active === false ? 'border-base bg-gray:5 text-gray op50' : 'border-base bg-gray:5 text-gray'" | ||
> | ||
{{ glob }} | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,54 @@ | ||
import type { Linter } from 'eslint' | ||
import { minimatch } from 'minimatch' | ||
import type { FlatESLintConfigItem } from '~~/types' | ||
import type { FileConfigMatchResult, FlatESLintConfigItem } from '~~/types' | ||
|
||
function matchGlob(file: string, glob: (Linter.FlatConfigFileSpec | Linter.FlatConfigFileSpec[])[]) { | ||
function getMatchedGlobs(file: string, glob: (Linter.FlatConfigFileSpec | Linter.FlatConfigFileSpec[])[]) { | ||
const globs = (Array.isArray(glob) ? glob : [glob]).flat() | ||
return globs.some(glob => typeof glob === 'function' ? glob(file) : minimatch(file, glob)) | ||
return globs.filter(glob => typeof glob === 'function' ? glob(file) : minimatch(file, glob)).flat() | ||
} | ||
|
||
function matchGlob(file: string, glob: (Linter.FlatConfigFileSpec | Linter.FlatConfigFileSpec[])[]) { | ||
return getMatchedGlobs(file, glob).length > 0 | ||
} | ||
|
||
export function isIgnoreOnlyConfig(config: FlatESLintConfigItem) { | ||
const keys = Object.keys(config).filter(i => !['name'].includes(i)) | ||
return keys.length === 1 && keys[0] === 'ignores' | ||
} | ||
|
||
export function getMatchedConfigs(filepath: string, configs: FlatESLintConfigItem[]): number[] { | ||
export function getMatchedConfigs(filepath: string, configs: FlatESLintConfigItem[]): FileConfigMatchResult[] { | ||
const ignoreOnlyConfigs = configs.filter(isIgnoreOnlyConfig) | ||
const isIgnored = ignoreOnlyConfigs.some(config => matchGlob(filepath, config.ignores!)) | ||
if (isIgnored) | ||
return [] | ||
|
||
const isAnyIncluded = configs.some(config => matchGlob(filepath, config.files || [])) | ||
if (!isAnyIncluded) | ||
return [] | ||
|
||
return configs | ||
.map((config, index) => { | ||
const isIncluded = config.files ? matchGlob(filepath, config.files) : true | ||
const isExcluded = config.ignores ? matchGlob(filepath, config.ignores) : false | ||
if (isIncluded && !isExcluded) | ||
return index | ||
return undefined | ||
.map((config, index): FileConfigMatchResult | undefined => { | ||
if (config.ignores && matchGlob(filepath, config.ignores)) | ||
return undefined | ||
if (!config.files) { | ||
return { | ||
config, | ||
index, | ||
globs: [], | ||
} | ||
} | ||
const includeGlob = getMatchedGlobs(filepath, config.files) | ||
if (!includeGlob.length) | ||
return undefined | ||
return { | ||
config, | ||
index, | ||
globs: includeGlob, | ||
} | ||
}) | ||
.filter(index => index !== undefined) as number[] | ||
.filter(notNullish) | ||
} | ||
|
||
function notNullish<T>(value: T): value is NonNullable<T> { | ||
return value != null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters