Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-93: support stars in negation extglobs with expression after closing parenthesis #102

Merged
merged 1 commit into from
Jan 2, 2022
Merged
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
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