Skip to content

Commit

Permalink
Merge pull request #102 from micromatch/ISSUE-93_incorrect_extglob_ex…
Browse files Browse the repository at this point in the history
…panding

ISSUE-93: support stars in negation extglobs with expression after closing parenthesis
  • Loading branch information
mrmlnc committed Jan 2, 2022
2 parents 719d348 + ac3cb66 commit 9f241ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,14 @@ const parse = (input, options) => {
}

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

output = token.close = `)${expression})${extglobStar})`;
}

if (token.prev.type === 'bos') {
Expand Down
19 changes: 19 additions & 0 deletions test/extglobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,34 @@ describe('extglobs', () => {
it('should support stars in negation extglobs', () => {
assert(!isMatch('/file.d.ts', '/!(*.d).ts'));
assert(isMatch('/file.ts', '/!(*.d).ts'));
assert(isMatch('/file.something.ts', '/!(*.d).ts'));
assert(isMatch('/file.d.something.ts', '/!(*.d).ts'));
assert(isMatch('/file.dhello.ts', '/!(*.d).ts'));

assert(!isMatch('/file.d.ts', '**/!(*.d).ts'));
assert(isMatch('/file.ts', '**/!(*.d).ts'));
assert(isMatch('/file.something.ts', '**/!(*.d).ts'));
assert(isMatch('/file.d.something.ts', '**/!(*.d).ts'));
assert(isMatch('/file.dhello.ts', '**/!(*.d).ts'));
});

// See https://github.com/micromatch/picomatch/issues/93
it('should support stars in negation extglobs with expression after closing parenthesis', () => {
// Nested expression after closing parenthesis
assert(!isMatch('/file.d.ts', '/!(*.d).{ts,tsx}'));
assert(isMatch('/file.ts', '/!(*.d).{ts,tsx}'));
assert(isMatch('/file.something.ts', '/!(*.d).{ts,tsx}'));
assert(isMatch('/file.d.something.ts', '/!(*.d).{ts,tsx}'));
assert(isMatch('/file.dhello.ts', '/!(*.d).{ts,tsx}'));

// Extglob after closing parenthesis
assert(!isMatch('/file.d.ts', '/!(*.d).@(ts)'));
assert(isMatch('/file.ts', '/!(*.d).@(ts)'));
assert(isMatch('/file.something.ts', '/!(*.d).@(ts)'));
assert(isMatch('/file.d.something.ts', '/!(*.d).@(ts)'));
assert(isMatch('/file.dhello.ts', '/!(*.d).@(ts)'));
});

it('should support negation extglobs in patterns with slashes', () => {
assert(!isMatch('foo/abc', 'foo/!(abc)'));
assert(isMatch('foo/bar', 'foo/!(abc)'));
Expand Down

0 comments on commit 9f241ef

Please sign in to comment.