Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, resolve } from 'node:path'
import { slash, toArray } from '@antfu/utils'
import { isPackageExists } from 'local-pkg'
import { detectTypeImports } from './type-imports/detect'
import { escapeSpecialChars } from './utils'
import { escapeGlobRootPrefix } from './utils'

export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'excludeNames' | 'transformer' | 'globs' | 'globsExclude' | 'directives' | 'types' | 'version'> = {
dirs: 'src/components',
Expand Down Expand Up @@ -65,9 +65,8 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
prefix = '!'
i = i.slice(1)
}
return resolved.deep
? prefix + escapeSpecialChars(slash(join(i, `**/*.${extsGlob}`)))
: prefix + escapeSpecialChars(slash(join(i, `*.${extsGlob}`)))
const joined = slash(join(i, resolved.deep ? `**/*.${extsGlob}` : `*.${extsGlob}`))
return prefix + escapeGlobRootPrefix(joined, slash(root))
})

if (!resolved.extensions.length)
Expand Down
18 changes: 16 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,22 @@ export function isExclude(name: string, exclude?: FilterPattern): boolean {
return false
}

const ESCAPE_PARENTHESES_REGEX = /[()]/g
const ESCAPE_SPECIAL_CHARS_REGEX = /[()[\]]/g

export function escapeSpecialChars(str: string): string {
return str.replace(ESCAPE_PARENTHESES_REGEX, '\\$&')
return str.replace(ESCAPE_SPECIAL_CHARS_REGEX, '\\$&')
}

/**
* Escape glob metacharacters in the `root` prefix of an already-joined path,
* leaving the rest untouched.
*
* `root` is a real filesystem path, so `()` and `[]` in it are always literal.
* What follows comes from the user's `dirs` option and may be deliberate glob
* syntax such as `dirs: ['src/[ab]']`, which must keep behaving as a glob.
*/
export function escapeGlobRootPrefix(glob: string, root: string): string {
if (!root || !glob.startsWith(root))
return escapeSpecialChars(glob)
return escapeSpecialChars(root) + glob.slice(root.length)
}
43 changes: 42 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ResolvedOptions } from '../src'
import { describe, expect, it } from 'vitest'
import { escapeSpecialChars, getNameFromFilePath } from '../src/core/utils'
import { escapeGlobRootPrefix, escapeSpecialChars, getNameFromFilePath, matchGlobs } from '../src/core/utils'

describe('getNameFromFilePath', () => {
const options: Partial<ResolvedOptions> = {
Expand All @@ -25,4 +25,45 @@ describe('escapeSpecialChars', () => {
it('should escape parentheses', () => {
expect(escapeSpecialChars('component()')).toBe('component\\(\\)')
})

it('should escape square brackets so they are not treated as character classes', () => {
expect(escapeSpecialChars('/proj/[Foo]/src')).toBe('/proj/\\[Foo\\]/src')
})
})

describe('matchGlobs', () => {
it('matches a path whose resolved base contains square brackets', () => {
const filepath = '/Github/Project/[Foo]/ui/src/component/Button.vue'
const glob = escapeSpecialChars('/Github/Project/[Foo]/ui/src/component/**/*.vue')
expect(matchGlobs(filepath, [glob])).toBe(true)
})

it('matches a path whose directory name is a picomatch character-class range', () => {
const filepath = '/Github/Project/[a-z]/ui/src/component/Button.vue'
const glob = escapeSpecialChars('/Github/Project/[a-z]/ui/src/component/**/*.vue')
expect(matchGlobs(filepath, [glob])).toBe(true)
})
})

describe('escapeGlobRootPrefix', () => {
it('escapes metacharacters in the root prefix', () => {
expect(escapeGlobRootPrefix('/proj/[Foo]/src/**/*.vue', '/proj/[Foo]'))
.toBe('/proj/\\[Foo\\]/src/**/*.vue')
})

it('leaves user-supplied glob syntax after the root untouched', () => {
// `dirs: ['src/[ab]']` under a plain root must still behave as a character
// class, so only the root prefix may be escaped.
const glob = escapeGlobRootPrefix('/proj/src/[ab]/**/*.vue', '/proj')
expect(glob).toBe('/proj/src/[ab]/**/*.vue')
expect(matchGlobs('/proj/src/a/Button.vue', [glob])).toBe(true)
expect(matchGlobs('/proj/src/b/Button.vue', [glob])).toBe(true)
expect(matchGlobs('/proj/src/c/Button.vue', [glob])).toBe(false)
})

it('handles both at once: metacharacters in the root and a glob after it', () => {
const glob = escapeGlobRootPrefix('/proj/Code(T)/src/[ab]/**/*.vue', '/proj/Code(T)')
expect(matchGlobs('/proj/Code(T)/src/a/Button.vue', [glob])).toBe(true)
expect(matchGlobs('/proj/Code(T)/src/c/Button.vue', [glob])).toBe(false)
})
})