GeezerVison is showing github.com — fix: handle terminal globstars in parenthesized patterns · micromatch/picomatch@e279bd7
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/parse.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1175,6 +1175,15 @@ const parse = (input, options) => { |
1175 | 1175 | consume('/**', 3); |
1176 | 1176 | } |
1177 | 1177 |
|
| 1178 | + // A globstar followed only by balanced closing parens is at the logical end of patterns like |
| 1179 | + // `test(/utils/**)` and `test?(/utils/**)`. Treat it as EOS so the trailing `/**` can match its |
| 1180 | + // parent path, except in negated extglobs where that would change the exclusion semantics. |
| 1181 | + const isEnd = eos() || ( |
| 1182 | + state.parens > 0 |
| 1183 | + && rest === ')'.repeat(state.parens) |
| 1184 | + && !extglobs.some(extglob => extglob.type === 'negate') |
| 1185 | + ); |
| 1186 | + |
1178 | 1187 | if (prior.type === 'bos' && eos()) { |
1179 | 1188 | prev.type = 'globstar'; |
1180 | 1189 | prev.value += value; |
@@ -1185,7 +1194,7 @@ const parse = (input, options) => { |
1185 | 1194 | continue; |
1186 | 1195 | } |
1187 | 1196 |
|
1188 | | - if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) { |
| 1197 | + if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && isEnd) { |
1189 | 1198 | state.output = state.output.slice(0, -(prior.output + prev.output).length); |
1190 | 1199 | prior.output = `(?:${prior.output}`; |
1191 | 1200 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/issue-related.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,4 +61,15 @@ describe('issue-related tests', () => { |
61 | 61 | assert(isMatch('a/foo.js', '**/foo.js', { dot: true })); |
62 | 62 | assert(isMatch('foo.js', '**/foo.js', { dot: true })); |
63 | 63 | }); |
| 64 | + |
| 65 | + it('picomatch issue#142 - should match trailing globstars in parens', () => { |
| 66 | + assert(isMatch('test/utils', 'test(/utils/**)')); |
| 67 | + assert(isMatch('test/utils', 'test?(/utils/**)')); |
| 68 | + assert(isMatch('test/utils/file', 'test(/utils/**)')); |
| 69 | + assert(isMatch('test/utils/file', 'test?(/utils/**)')); |
| 70 | + assert(!isMatch('test', 'test(/utils/**)')); |
| 71 | + assert(isMatch('test', 'test?(/utils/**)')); |
| 72 | + assert(!isMatch('test/utils', 'test(/utils/**)', { strictSlashes: true })); |
| 73 | + assert(!isMatch('test/utils', 'test(/utils/**)/file')); |
| 74 | + }); |
64 | 75 | }); |