Skip to content

Commit 08e7348

Browse files
committed
Add magicalBraces option
This tells hasMagic to treat brace expansion as magical. Fix: #496 PR-URL: #499 Credit: @isaacs Close: #499 Reviewed-by: @isaacs
1 parent 02a1da4 commit 08e7348

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,14 @@ if they're not consumed immediately.
129129
Returns `true` if the provided pattern contains any "magic" glob
130130
characters, given the options provided.
131131

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

138141
## Class `Glob`
139142

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

250+
- `magicalBraces` Treat brace expansion like `{a,b}` as a "magic"
251+
pattern. Has no effect if {@link nobrace} is set.
252+
253+
Only has effect on the {@link hasMagic} function, no effect on
254+
glob pattern matching itself.
255+
247256
- `mark` Add a `/` character to directory matches. Note that this
248257
requires additional stat calls.
249258

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
relative paths. An unset `absolute` setting will still return
77
absolute or relative paths based on whether the pattern is
88
absolute.
9+
- Add `magicalBraces` option to treat brace expansion as "magic"
10+
in the `hasMagic` function.
911

1012
## 9.0
1113

src/glob.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export interface GlobOptions {
9393
*/
9494
ignore?: string | string[] | Ignore
9595

96+
/**
97+
* Treat brace expansion like `{a,b}` as a "magic" pattern. Has no
98+
* effect if {@link nobrace} is set.
99+
*
100+
* Only has effect on the {@link hasMagic} function.
101+
*/
102+
magicalBraces?: boolean
103+
96104
/**
97105
* Add a `/` character to directory matches. Note that this requires
98106
* additional stat calls in some cases.
@@ -263,7 +271,8 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
263271
dot: boolean
264272
follow: boolean
265273
ignore?: Ignore
266-
mark: boolean
274+
magicalBraces: boolean
275+
mark?: boolean
267276
matchBase: boolean
268277
nobrace: boolean
269278
nocase: boolean
@@ -314,6 +323,7 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
314323
}
315324
this.cwd = opts.cwd || ''
316325
this.root = opts.root
326+
this.magicalBraces = !!opts.magicalBraces
317327
this.nobrace = !!opts.nobrace
318328
this.noext = !!opts.noext
319329
this.realpath = !!opts.realpath

src/has-magic.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import { Glob, GlobOptions } from './glob.js'
22

33
/**
44
* Return true if the patterns provided contain any magic
5-
* glob characters.
5+
* glob characters, given the options provided.
6+
*
7+
* Brace expansion is not considered "magic" unless the `magicalBraces` option
8+
* is set, as brace expansion just turns one string into an array of strings.
9+
* So a pattern like `'x{a,b}y'` would return `false`, because `'xay'` and
10+
* `'xby'` both do not contain any magic glob characters, and it's treated the
11+
* same as if you had called it on `['xay', 'xby']`. When
12+
* `magicalBraces:true` is in the options, brace expansion _is_ treated as a
13+
* pattern having magic.
614
*/
715
export const hasMagic = (
816
pattern: string | string[],
@@ -11,8 +19,10 @@ export const hasMagic = (
1119
if (!Array.isArray(pattern)) {
1220
pattern = [pattern]
1321
}
22+
const length = pattern.length
1423
const g = new Glob(pattern, options)
15-
return g.patterns.length === 0
16-
? false
17-
: g.patterns.some(p => p.hasMagic())
24+
if (g.magicalBraces && g.patterns.length !== length) {
25+
return true
26+
}
27+
return g.patterns.some(p => p.hasMagic())
1828
}

test/has-magic.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ t.test('detect magic in glob patterns', async t => {
2323
'no magic in a/b/+(x|y) noext'
2424
)
2525
t.notOk(glob.hasMagic('{a,b}'), 'no magic in {a,b}')
26+
t.ok(
27+
glob.hasMagic('{a,b}', { magicalBraces: true }),
28+
'magical braces are magic in {a,b}'
29+
)
2630
t.notOk(
2731
glob.hasMagic('{a,b}', { nobrace: true }),
2832
'no magic in {a,b} nobrace:true'
2933
)
34+
t.notOk(
35+
glob.hasMagic('{a,b}', { nobrace: true, magicalBraces: true }),
36+
'magical braces not magic in {a,b} nobrace:true'
37+
)
3038
})

0 commit comments

Comments
 (0)