Skip to content

Commit e15b920

Browse files
authored
Merge pull request #65 from mrmlnc/fix_parens
ISSUE-58: Correctly handle a part after parentheses
2 parents 66e1b20 + 2ca064f commit e15b920

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

lib/scan.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,24 @@ const scan = (input, options) => {
247247
}
248248

249249
if (opts.noparen !== true && code === CHAR_LEFT_PARENTHESES) {
250-
while (eos() !== true && (code = advance())) {
251-
if (code === CHAR_BACKWARD_SLASH) {
252-
backslashes = token.backslashes = true;
253-
code = advance();
254-
continue;
255-
}
256-
257-
if (code === CHAR_RIGHT_PARENTHESES) {
258-
isGlob = token.isGlob = true;
259-
finished = true;
250+
isGlob = token.isGlob = true;
260251

261-
if (scanToEnd === true) {
252+
if (scanToEnd === true) {
253+
while (eos() !== true && (code = advance())) {
254+
if (code === CHAR_LEFT_PARENTHESES) {
255+
backslashes = token.backslashes = true;
256+
code = advance();
262257
continue;
263258
}
264-
break;
259+
260+
if (code === CHAR_RIGHT_PARENTHESES) {
261+
finished = true;
262+
break;
263+
}
265264
}
265+
continue;
266266
}
267+
break;
267268
}
268269

269270
if (isGlob === true) {

test/api.scan.js

+60
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ const both = (...args) => {
88
return [base, glob];
99
};
1010

11+
/**
12+
* @param {String} pattern
13+
* @param {String[]} parts
14+
*/
15+
function assertParts(pattern, parts) {
16+
const info = scan(pattern, { parts: true });
17+
18+
assert.deepStrictEqual(info.parts, parts);
19+
}
20+
1121
/**
1222
* Most of the unit tests in this file were from https://github.com/es128/glob-parent
1323
* and https://github.com/jonschlinkert/glob-base. Both libraries use a completely
@@ -252,6 +262,56 @@ describe('picomatch', () => {
252262
negated: false
253263
});
254264
});
265+
266+
it('should return parts of the pattern', () => {
267+
// Right now it returns []
268+
// assertParts('', ['']);
269+
// assertParts('*', ['*']);
270+
// assertParts('.*', ['.*']);
271+
// assertParts('**', ['**']);
272+
// assertParts('foo', ['foo']);
273+
// assertParts('foo*', ['foo*']);
274+
// assertParts('/', ['', '']);
275+
// assertParts('/*', ['', '*']);
276+
// assertParts('./', ['']);
277+
// assertParts('{1..9}', ['{1..9}']);
278+
// assertParts('c!(.)z', ['c!(.)z']);
279+
// assertParts('(b|a).(a)', ['(b|a).(a)']);
280+
// assertParts('+(a|b\\[)*', ['+(a|b\\[)*']);
281+
// assertParts('@(a|b).md', ['@(a|b).md']);
282+
// assertParts('(a/b)', ['(a/b)']);
283+
// assertParts('(a\\b)', ['(a\\b)']);
284+
// assertParts('foo\\[a\\/]', ['foo\\[a\\/]']);
285+
// assertParts('foo[/]bar', ['foo[/]bar']);
286+
// assertParts('/dev\\/@(tcp|udp)\\/*\\/*', ['', '/dev\\/@(tcp|udp)\\/*\\/*']);
287+
288+
// Right now it returns ['*']
289+
// assertParts('*/', ['*', '']);
290+
291+
// Right now it returns ['!(!(bar)', 'baz)']
292+
// assertParts('!(!(bar)/baz)', ['!(!(bar)/baz)']);
293+
294+
assertParts('./foo', ['foo']);
295+
assertParts('../foo', ['..', 'foo']);
296+
297+
assertParts('foo/bar', ['foo', 'bar']);
298+
assertParts('foo/*', ['foo', '*']);
299+
assertParts('foo/**', ['foo', '**']);
300+
assertParts('foo/**/*', ['foo', '**', '*']);
301+
assertParts('フォルダ/**/*', ['フォルダ', '**', '*']);
302+
303+
assertParts('foo/!(abc)', ['foo', '!(abc)']);
304+
assertParts('c/!(z)/v', ['c', '!(z)', 'v']);
305+
assertParts('c/@(z)/v', ['c', '@(z)', 'v']);
306+
assertParts('foo/(bar|baz)', ['foo', '(bar|baz)']);
307+
assertParts('foo/(bar|baz)*', ['foo', '(bar|baz)*']);
308+
assertParts('**/*(W*, *)*', ['**', '*(W*, *)*']);
309+
assertParts('a/**@(/x|/z)/*.md', ['a', '**@(/x|/z)', '*.md']);
310+
assertParts('foo/(bar|baz)/*.js', ['foo', '(bar|baz)', '*.js']);
311+
312+
assertParts('XXX/*/*/12/*/*/m/*/*', ['XXX', '*', '*', '12', '*', '*', 'm', '*', '*']);
313+
assertParts('foo/\\"**\\"/bar', ['foo', '\\"**\\"', 'bar']);
314+
});
255315
});
256316

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

0 commit comments

Comments
 (0)