Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ const parse = (input, options) => {
value = `\\${value}`;
}

if (opts.posix === true && value === '!' && prev.value === '[') {
if (value === '!' && prev.value === '[') {
value = '^';
}

Expand Down
40 changes: 40 additions & 0 deletions test/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,44 @@ describe('brackets', () => {
assert(!isMatch('a/b', '[a]*'));
});
});
describe('negation with "!"', () => {
it('should negate a bracket expression with a leading "!"', () => {
assert(!isMatch('a', '[!abc]'));
assert(!isMatch('b', '[!abc]'));
assert(!isMatch('c', '[!abc]'));
assert(isMatch('d', '[!abc]'));
assert(isMatch('x', '[!abc]'));
});

it('should negate ranges with a leading "!"', () => {
assert(!isMatch('a', '[!a-c]'));
assert(!isMatch('c', '[!a-c]'));
assert(isMatch('d', '[!a-c]'));
});

it('should agree with "^" negation', () => {
assert.strictEqual(isMatch('a', '[!abc]'), isMatch('a', '[^abc]'));
assert.strictEqual(isMatch('d', '[!abc]'), isMatch('d', '[^abc]'));
assert.strictEqual(isMatch('a', '[!a-c]'), isMatch('a', '[^a-c]'));
assert.strictEqual(isMatch('d', '[!a-c]'), isMatch('d', '[^a-c]'));
});

it('should negate within a larger pattern', () => {
assert(!isMatch('abc', 'a[!b]c'));
assert(isMatch('axc', 'a[!b]c'));
assert(!isMatch('ad', '[!abc]d'));
assert(isMatch('xd', '[!abc]d'));
});

it('should treat "!" as a literal when it is not leading', () => {
assert(isMatch('!', '[a!]'));
assert(isMatch('a', '[a!]'));
assert(!isMatch('b', '[a!]'));
});

it('should negate a literal "!"', () => {
assert(!isMatch('!', '[!!]'));
assert(isMatch('a', '[!!]'));
});
});
});