Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed May 21, 2021
1 parent 0273e43 commit e0042e0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
15 changes: 15 additions & 0 deletions examples/extglob-negated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const picomatch = require('..');

const fixtures = [
['/file.d.ts', false],
['/file.ts', true],
['/file.d.something.ts', true],
['/file.dhello.ts', true]
];

const pattern = '/!(*.d).ts';
const isMatch = picomatch(pattern);

console.log(fixtures.map(f => [isMatch(f[0]), f[1]]));
7 changes: 6 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ const parse = (input, options) => {
}
}

if (extglobs.length && tok.type !== 'paren' && !EXTGLOB_CHARS[tok.value]) {
if (extglobs.length && tok.type !== 'paren') {
extglobs[extglobs.length - 1].inner += tok.value;
}

Expand Down Expand Up @@ -236,6 +236,7 @@ const parse = (input, options) => {

const extglobClose = token => {
let output = token.close + (opts.capture ? ')' : '');
let rest;

if (token.type === 'negate') {
let extglobStar = star;
Expand All @@ -248,6 +249,10 @@ const parse = (input, options) => {
output = token.close = `)$))${extglobStar}`;
}

if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
output = token.close = `)${rest})${extglobStar})`;
}

if (token.prev.type === 'bos') {
state.negatedExtglob = true;
}
Expand Down
1 change: 1 addition & 0 deletions test/extglobs-temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { isMatch } = require('..');

/**
* Some of tests were converted from bash 4.3, 4.4, and minimatch unit tests.
* This is called "temp" as a reminder to reorganize these test and remove duplicates.
*/

describe('extglobs', () => {
Expand Down
15 changes: 14 additions & 1 deletion test/extglobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe('extglobs', () => {
assert(isMatch('abc', 'a!(.)c'));
});

// See https://github.com/micromatch/picomatch/issues/83
it('should support stars in negation extglobs', () => {
assert(!isMatch('/file.d.ts', '/!(*.d).ts'));
assert(isMatch('/file.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.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 Expand Up @@ -271,7 +284,7 @@ describe('extglobs', () => {
assert(isMatch('ab.md', '?(a|ab|b).md'));
assert(isMatch('b.md', '?(a|ab|b).md'));

// see https://github.com/micromatch/micromatch/issues/186
// See https://github.com/micromatch/micromatch/issues/186
assert(isMatch('ab', '+(a)?(b)'));
assert(isMatch('aab', '+(a)?(b)'));
assert(isMatch('aa', '+(a)?(b)'));
Expand Down

0 comments on commit e0042e0

Please sign in to comment.