Skip to content

Commit

Permalink
fix: support only string globs
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 11, 2024
1 parent a40fc38 commit c99f85e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
3 changes: 1 addition & 2 deletions app/components/ConfigItem.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { computed, defineModel, ref, watchEffect } from 'vue'
import type { Linter } from 'eslint'
import { filtersRules, isGridView } from '~/composables/state'
import { stringifyUnquoted } from '~/composables/strings'
import { useRouter } from '#app/composables/router'
Expand All @@ -12,7 +11,7 @@ const props = defineProps<{
index: number
filters?: FiltersConfigsPage
active?: boolean
matchedGlobs?: Linter.FlatConfigFileSpec[]
matchedGlobs?: string[]
}>()
const emit = defineEmits<{
Expand Down
3 changes: 1 addition & 2 deletions app/components/GlobItem.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { computed, defineComponent } from 'vue'
import type { Linter } from 'eslint'
import { Dropdown as VDropdown } from 'floating-vue'
import { useHighlightedGlob } from '../composables/shiki'
import { filtersConfigs } from '~/composables/state'
Expand All @@ -9,7 +8,7 @@ import { payload } from '~/composables/payload'
const props = withDefaults(
defineProps<{
glob: Linter.FlatConfigFileSpec
glob: string
popup?: 'files' | 'configs'
active?: boolean | null
}>(),
Expand Down
7 changes: 3 additions & 4 deletions app/composables/payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
import type { Linter } from 'eslint'
import { $fetch } from 'ofetch'
import { isGeneralConfig, isIgnoreOnlyConfig } from '~~/shared/configs'
import { getRuleLevel, getRuleOptions } from '~~/shared/rules'
Expand Down Expand Up @@ -91,7 +90,7 @@ export function getRuleStates(name: string): RuleConfigStates | undefined {

export function resolvePayload(payload: Payload): ResolvedPayload {
const ruleToState = new Map<string, RuleConfigStates>()
const globToConfigs = new Map<Linter.FlatConfigFileSpec, FlatConfigItem[]>()
const globToConfigs = new Map<string, FlatConfigItem[]>()

payload.configs.forEach((config, index) => {
// Rule Level
Expand Down Expand Up @@ -148,8 +147,8 @@ function resolveFiles(payload: Payload): ResolvedPayload['filesResolved'] {
.map(i => i.index)

const files: string[] = []
const globToFiles = new Map<Linter.FlatConfigFileSpec, Set<string>>()
const fileToGlobs = new Map<string, Set<Linter.FlatConfigFileSpec>>()
const globToFiles = new Map<string, Set<string>>()
const fileToGlobs = new Map<string, Set<string>>()
const fileToConfigs = new Map<string, Set<number>>()
const configToFiles = new Map<number, Set<string>>()
const filesGroupMap = new Map<string, FilesGroup>()
Expand Down
5 changes: 2 additions & 3 deletions shared/configs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Linter } from 'eslint'
import { minimatch } from 'minimatch'
import type { FlatConfigItem, MatchedFile } from './types'

export function getMatchedGlobs(file: string, glob: (Linter.FlatConfigFileSpec | Linter.FlatConfigFileSpec[])[]) {
export function getMatchedGlobs(file: string, glob: (string | string[])[]) {
const globs = (Array.isArray(glob) ? glob : [glob]).flat()
return globs.filter(glob => typeof glob === 'function' ? glob(file) : minimatch(file, glob)).flat()
return globs.filter(glob => minimatch(file, glob)).flat()
}

const META_KEYS = new Set(['name', 'index'])
Expand Down
14 changes: 8 additions & 6 deletions shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Linter } from 'eslint'
import type { RuleMetaData } from '@typescript-eslint/utils/ts-eslint'

export interface FlatConfigItem extends Linter.FlatConfig {
export interface FlatConfigItem extends Omit<Linter.FlatConfig, 'files' | 'ignores'> {
name?: string
index: number
files?: (string | string[])[]
ignores?: string[]
}

export type RuleLevel = 'off' | 'warn' | 'error'
Expand All @@ -20,17 +22,17 @@ export interface ResolvedPayload extends Payload {
configsGeneral: FlatConfigItem[]

ruleToState: Map<string, RuleConfigStates>
globToConfigs: Map<Linter.FlatConfigFileSpec, FlatConfigItem[]>
globToConfigs: Map<string, FlatConfigItem[]>

/**
* Resolved data from files
* Undefined if users disabled glob matching
*/
filesResolved?: {
list: string[]
globToFiles: Map<Linter.FlatConfigFileSpec, Set<string>>
globToFiles: Map<string, Set<string>>
configToFiles: Map<number, Set<string>>
fileToGlobs: Map<string, Set<Linter.FlatConfigFileSpec>>
fileToGlobs: Map<string, Set<string>>
fileToConfigs: Map<string, FlatConfigItem[]>
groups: FilesGroup[]
}
Expand All @@ -44,7 +46,7 @@ export interface MatchedFile {
/**
* Matched globs, includes both positive and negative globs
*/
globs: Linter.FlatConfigFileSpec[]
globs: string[]
/**
* Matched configs indexes
*/
Expand All @@ -60,7 +62,7 @@ export interface FilesGroup {
id: string
files: string[]
configs: FlatConfigItem[]
globs: Set<Linter.FlatConfigFileSpec>
globs: Set<string>
}

export interface PayloadMeta {
Expand Down
2 changes: 1 addition & 1 deletion src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export async function globMatchedFiles(

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

0 comments on commit c99f85e

Please sign in to comment.