Skip to content

Commit 9f241ef

Browse files
authored
Merge pull request #102 from micromatch/ISSUE-93_incorrect_extglob_expanding
ISSUE-93: support stars in negation extglobs with expression after closing parenthesis
2 parents 719d348 + ac3cb66 commit 9f241ef

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/parse.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,14 @@ const parse = (input, options) => {
250250
}
251251

252252
if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
253-
output = token.close = `)${rest})${extglobStar})`;
253+
// Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
254+
// In this case, we need to parse the string and use it in the output of the original pattern.
255+
// Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
256+
//
257+
// Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
258+
const expression = parse(rest, { ...options, fastpaths: false }).output;
259+
260+
output = token.close = `)${expression})${extglobStar})`;
254261
}
255262

256263
if (token.prev.type === 'bos') {

test/extglobs.js

+19
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,34 @@ describe('extglobs', () => {
5858
it('should support stars in negation extglobs', () => {
5959
assert(!isMatch('/file.d.ts', '/!(*.d).ts'));
6060
assert(isMatch('/file.ts', '/!(*.d).ts'));
61+
assert(isMatch('/file.something.ts', '/!(*.d).ts'));
6162
assert(isMatch('/file.d.something.ts', '/!(*.d).ts'));
6263
assert(isMatch('/file.dhello.ts', '/!(*.d).ts'));
6364

6465
assert(!isMatch('/file.d.ts', '**/!(*.d).ts'));
6566
assert(isMatch('/file.ts', '**/!(*.d).ts'));
67+
assert(isMatch('/file.something.ts', '**/!(*.d).ts'));
6668
assert(isMatch('/file.d.something.ts', '**/!(*.d).ts'));
6769
assert(isMatch('/file.dhello.ts', '**/!(*.d).ts'));
6870
});
6971

72+
// See https://github.com/micromatch/picomatch/issues/93
73+
it('should support stars in negation extglobs with expression after closing parenthesis', () => {
74+
// Nested expression after closing parenthesis
75+
assert(!isMatch('/file.d.ts', '/!(*.d).{ts,tsx}'));
76+
assert(isMatch('/file.ts', '/!(*.d).{ts,tsx}'));
77+
assert(isMatch('/file.something.ts', '/!(*.d).{ts,tsx}'));
78+
assert(isMatch('/file.d.something.ts', '/!(*.d).{ts,tsx}'));
79+
assert(isMatch('/file.dhello.ts', '/!(*.d).{ts,tsx}'));
80+
81+
// Extglob after closing parenthesis
82+
assert(!isMatch('/file.d.ts', '/!(*.d).@(ts)'));
83+
assert(isMatch('/file.ts', '/!(*.d).@(ts)'));
84+
assert(isMatch('/file.something.ts', '/!(*.d).@(ts)'));
85+
assert(isMatch('/file.d.something.ts', '/!(*.d).@(ts)'));
86+
assert(isMatch('/file.dhello.ts', '/!(*.d).@(ts)'));
87+
});
88+
7089
it('should support negation extglobs in patterns with slashes', () => {
7190
assert(!isMatch('foo/abc', 'foo/!(abc)'));
7291
assert(isMatch('foo/bar', 'foo/!(abc)'));

0 commit comments

Comments
 (0)