Skip to content

Commit

Permalink
Add magicalBraces option
Browse files Browse the repository at this point in the history
This tells hasMagic to treat brace expansion as magical.

Fix: #496

PR-URL: #499
Credit: @isaacs
Close: #499
Reviewed-by: @isaacs
  • Loading branch information
isaacs committed Mar 1, 2023
1 parent 02a1da4 commit 08e7348
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,14 @@ if they're not consumed immediately.
Returns `true` if the provided pattern contains any "magic" glob
characters, given the options provided.

Note that brace expansion is not considered "magic", as that just
turns one string into an array of strings. So a pattern like
`'x{a,b}y'` would return `false`, because `'xay'` and `'xby'`
both do not contain any magic glob characters, and it's treated
the same as if you had called it on `['xay', 'xby']`.
Brace expansion is not considered "magic" unless the
`magicalBraces` option is set, as brace expansion just turns one
string into an array of strings. So a pattern like `'x{a,b}y'`
would return `false`, because `'xay'` and `'xby'` both do not
contain any magic glob characters, and it's treated the same as
if you had called it on `['xay', 'xby']`. When
`magicalBraces:true` is in the options, brace expansion _is_
treated as a pattern having magic.

## Class `Glob`

Expand Down Expand Up @@ -244,6 +247,12 @@ share the previously loaded cache.
matches. Note that an explicit dot in a portion of the pattern
will always match dot files.

- `magicalBraces` Treat brace expansion like `{a,b}` as a "magic"
pattern. Has no effect if {@link nobrace} is set.

Only has effect on the {@link hasMagic} function, no effect on
glob pattern matching itself.

- `mark` Add a `/` character to directory matches. Note that this
requires additional stat calls.

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
relative paths. An unset `absolute` setting will still return
absolute or relative paths based on whether the pattern is
absolute.
- Add `magicalBraces` option to treat brace expansion as "magic"
in the `hasMagic` function.

## 9.0

Expand Down
12 changes: 11 additions & 1 deletion src/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export interface GlobOptions {
*/
ignore?: string | string[] | Ignore

/**
* Treat brace expansion like `{a,b}` as a "magic" pattern. Has no
* effect if {@link nobrace} is set.
*
* Only has effect on the {@link hasMagic} function.
*/
magicalBraces?: boolean

/**
* Add a `/` character to directory matches. Note that this requires
* additional stat calls in some cases.
Expand Down Expand Up @@ -263,7 +271,8 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
dot: boolean
follow: boolean
ignore?: Ignore
mark: boolean
magicalBraces: boolean
mark?: boolean
matchBase: boolean
nobrace: boolean
nocase: boolean
Expand Down Expand Up @@ -314,6 +323,7 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
}
this.cwd = opts.cwd || ''
this.root = opts.root
this.magicalBraces = !!opts.magicalBraces
this.nobrace = !!opts.nobrace
this.noext = !!opts.noext
this.realpath = !!opts.realpath
Expand Down
18 changes: 14 additions & 4 deletions src/has-magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { Glob, GlobOptions } from './glob.js'

/**
* Return true if the patterns provided contain any magic
* glob characters.
* glob characters, given the options provided.
*
* Brace expansion is not considered "magic" unless the `magicalBraces` option
* is set, as brace expansion just turns one string into an array of strings.
* So a pattern like `'x{a,b}y'` would return `false`, because `'xay'` and
* `'xby'` both do not contain any magic glob characters, and it's treated the
* same as if you had called it on `['xay', 'xby']`. When
* `magicalBraces:true` is in the options, brace expansion _is_ treated as a
* pattern having magic.
*/
export const hasMagic = (
pattern: string | string[],
Expand All @@ -11,8 +19,10 @@ export const hasMagic = (
if (!Array.isArray(pattern)) {
pattern = [pattern]
}
const length = pattern.length
const g = new Glob(pattern, options)
return g.patterns.length === 0
? false
: g.patterns.some(p => p.hasMagic())
if (g.magicalBraces && g.patterns.length !== length) {
return true
}
return g.patterns.some(p => p.hasMagic())
}
8 changes: 8 additions & 0 deletions test/has-magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ t.test('detect magic in glob patterns', async t => {
'no magic in a/b/+(x|y) noext'
)
t.notOk(glob.hasMagic('{a,b}'), 'no magic in {a,b}')
t.ok(
glob.hasMagic('{a,b}', { magicalBraces: true }),
'magical braces are magic in {a,b}'
)
t.notOk(
glob.hasMagic('{a,b}', { nobrace: true }),
'no magic in {a,b} nobrace:true'
)
t.notOk(
glob.hasMagic('{a,b}', { nobrace: true, magicalBraces: true }),
'magical braces not magic in {a,b} nobrace:true'
)
})

0 comments on commit 08e7348

Please sign in to comment.