Skip to content

Commit

Permalink
Merge pull request #65 from mrmlnc/fix_parens
Browse files Browse the repository at this point in the history
ISSUE-58: Correctly handle a part after parentheses
  • Loading branch information
mrmlnc committed Mar 21, 2020
2 parents 66e1b20 + 2ca064f commit e15b920
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/scan.js
Expand Up @@ -247,23 +247,24 @@ const scan = (input, options) => {
}

if (opts.noparen !== true && code === CHAR_LEFT_PARENTHESES) {
while (eos() !== true && (code = advance())) {
if (code === CHAR_BACKWARD_SLASH) {
backslashes = token.backslashes = true;
code = advance();
continue;
}

if (code === CHAR_RIGHT_PARENTHESES) {
isGlob = token.isGlob = true;
finished = true;
isGlob = token.isGlob = true;

if (scanToEnd === true) {
if (scanToEnd === true) {
while (eos() !== true && (code = advance())) {
if (code === CHAR_LEFT_PARENTHESES) {
backslashes = token.backslashes = true;
code = advance();
continue;
}
break;

if (code === CHAR_RIGHT_PARENTHESES) {
finished = true;
break;
}
}
continue;
}
break;
}

if (isGlob === true) {
Expand Down
60 changes: 60 additions & 0 deletions test/api.scan.js
Expand Up @@ -8,6 +8,16 @@ const both = (...args) => {
return [base, glob];
};

/**
* @param {String} pattern
* @param {String[]} parts
*/
function assertParts(pattern, parts) {
const info = scan(pattern, { parts: true });

assert.deepStrictEqual(info.parts, parts);
}

/**
* Most of the unit tests in this file were from https://github.com/es128/glob-parent
* and https://github.com/jonschlinkert/glob-base. Both libraries use a completely
Expand Down Expand Up @@ -252,6 +262,56 @@ describe('picomatch', () => {
negated: false
});
});

it('should return parts of the pattern', () => {
// Right now it returns []
// assertParts('', ['']);
// assertParts('*', ['*']);
// assertParts('.*', ['.*']);
// assertParts('**', ['**']);
// assertParts('foo', ['foo']);
// assertParts('foo*', ['foo*']);
// assertParts('/', ['', '']);
// assertParts('/*', ['', '*']);
// assertParts('./', ['']);
// assertParts('{1..9}', ['{1..9}']);
// assertParts('c!(.)z', ['c!(.)z']);
// assertParts('(b|a).(a)', ['(b|a).(a)']);
// assertParts('+(a|b\\[)*', ['+(a|b\\[)*']);
// assertParts('@(a|b).md', ['@(a|b).md']);
// assertParts('(a/b)', ['(a/b)']);
// assertParts('(a\\b)', ['(a\\b)']);
// assertParts('foo\\[a\\/]', ['foo\\[a\\/]']);
// assertParts('foo[/]bar', ['foo[/]bar']);
// assertParts('/dev\\/@(tcp|udp)\\/*\\/*', ['', '/dev\\/@(tcp|udp)\\/*\\/*']);

// Right now it returns ['*']
// assertParts('*/', ['*', '']);

// Right now it returns ['!(!(bar)', 'baz)']
// assertParts('!(!(bar)/baz)', ['!(!(bar)/baz)']);

assertParts('./foo', ['foo']);
assertParts('../foo', ['..', 'foo']);

assertParts('foo/bar', ['foo', 'bar']);
assertParts('foo/*', ['foo', '*']);
assertParts('foo/**', ['foo', '**']);
assertParts('foo/**/*', ['foo', '**', '*']);
assertParts('フォルダ/**/*', ['フォルダ', '**', '*']);

assertParts('foo/!(abc)', ['foo', '!(abc)']);
assertParts('c/!(z)/v', ['c', '!(z)', 'v']);
assertParts('c/@(z)/v', ['c', '@(z)', 'v']);
assertParts('foo/(bar|baz)', ['foo', '(bar|baz)']);
assertParts('foo/(bar|baz)*', ['foo', '(bar|baz)*']);
assertParts('**/*(W*, *)*', ['**', '*(W*, *)*']);
assertParts('a/**@(/x|/z)/*.md', ['a', '**@(/x|/z)', '*.md']);
assertParts('foo/(bar|baz)/*.js', ['foo', '(bar|baz)', '*.js']);

assertParts('XXX/*/*/12/*/*/m/*/*', ['XXX', '*', '*', '12', '*', '*', 'm', '*', '*']);
assertParts('foo/\\"**\\"/bar', ['foo', '\\"**\\"', 'bar']);
});
});

describe('.base (glob2base test patterns)', () => {
Expand Down

0 comments on commit e15b920

Please sign in to comment.