Skip to content

Commit

Permalink
ensure brackets are matched
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Apr 10, 2019
1 parent abcf341 commit 3d461a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const parse = (input, options = {}) => {
let stack = [ast];
let block = ast;
let prev = ast;
let brackets = 0;
let length = input.length;
let index = 0;
let depth = 0;
Expand Down Expand Up @@ -108,25 +109,31 @@ const parse = (input, options = {}) => {
*/

if (value === CHAR_LEFT_SQUARE_BRACKET) {
brackets++;

let closed = true;
let next;

while (index < length && (next = advance())) {
value += next;

if (next === CHAR_LEFT_SQUARE_BRACKET) {
brackets++;
continue;
}

if (next === CHAR_BACKSLASH) {
value += advance();
continue;
}

if (next === CHAR_RIGHT_SQUARE_BRACKET) {
closed = true;
break;
}
}
brackets--;

if (closed !== true) {
value = '\\' + value;
if (brackets === 0) {
break;
}
}
}

push({ type: 'text', value });
Expand Down
2 changes: 1 addition & 1 deletion test/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe('bash tests', () => {
equal('a/**/c/{d,e}/f*.{md,txt}', ['a/**/c/d/f*.md', 'a/**/c/d/f*.txt', 'a/**/c/e/f*.md', 'a/**/c/e/f*.txt']);
});

it('should expand with extglobs.', () => {
it('should expand with brackets.', () => {
equal('a/b/{d,e,[1-5]}/*.js', ['a/b/d/*.js', 'a/b/e/*.js', 'a/b/[1-5]/*.js']);
});
});
Expand Down

0 comments on commit 3d461a1

Please sign in to comment.