@openrewrite/rewrite 8.96.3 → 8.96.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/java/tree.d.ts +11 -11
- package/dist/java/tree.d.ts.map +1 -1
- package/dist/java/visitor.d.ts.map +1 -1
- package/dist/java/visitor.js +30 -0
- package/dist/java/visitor.js.map +1 -1
- package/dist/javascript/format/format.d.ts +2 -0
- package/dist/javascript/format/format.d.ts.map +1 -1
- package/dist/javascript/format/format.js +7 -1
- package/dist/javascript/format/format.js.map +1 -1
- package/dist/javascript/parser-utils.d.ts.map +1 -1
- package/dist/javascript/parser-utils.js +15 -7
- package/dist/javascript/parser-utils.js.map +1 -1
- package/dist/javascript/templating/precedence.js +1 -1
- package/dist/javascript/templating/precedence.js.map +1 -1
- package/dist/javascript/tree.d.ts +12 -12
- package/dist/javascript/tree.d.ts.map +1 -1
- package/dist/javascript/visitor.d.ts.map +1 -1
- package/dist/javascript/visitor.js +75 -0
- package/dist/javascript/visitor.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/node_modules/picomatch/LICENSE +21 -0
- package/node_modules/picomatch/README.md +743 -0
- package/node_modules/picomatch/index.js +17 -0
- package/node_modules/picomatch/lib/constants.js +184 -0
- package/node_modules/picomatch/lib/parse.js +1506 -0
- package/node_modules/picomatch/lib/picomatch.js +361 -0
- package/node_modules/picomatch/lib/scan.js +403 -0
- package/node_modules/picomatch/lib/utils.js +72 -0
- package/node_modules/picomatch/package.json +83 -0
- package/node_modules/picomatch/posix.js +3 -0
- package/package.json +88 -90
- package/src/java/tree.ts +11 -11
- package/src/java/visitor.ts +36 -0
- package/src/javascript/format/format.ts +7 -1
- package/src/javascript/parser-utils.ts +15 -7
- package/src/javascript/templating/precedence.ts +1 -1
- package/src/javascript/tree.ts +12 -12
- package/src/javascript/visitor.ts +90 -0
|
@@ -0,0 +1,1506 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const constants = require('./constants');
|
|
4
|
+
const utils = require('./utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Constants
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
MAX_LENGTH,
|
|
12
|
+
POSIX_REGEX_SOURCE,
|
|
13
|
+
REGEX_NON_SPECIAL_CHARS,
|
|
14
|
+
REGEX_SPECIAL_CHARS_BACKREF,
|
|
15
|
+
REPLACEMENTS
|
|
16
|
+
} = constants;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Helpers
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const expandRange = (args, options) => {
|
|
23
|
+
if (typeof options.expandRange === 'function') {
|
|
24
|
+
return options.expandRange(...args, options);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
args.sort();
|
|
28
|
+
const value = `[${args.join('-')}]`;
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
/* eslint-disable-next-line no-new */
|
|
32
|
+
new RegExp(value);
|
|
33
|
+
} catch (ex) {
|
|
34
|
+
return args.map(v => utils.escapeRegex(v)).join('..');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return value;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create the message for a syntax error
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
const syntaxError = (type, char) => {
|
|
45
|
+
return `Missing ${type}: "${char}" - use "\\\\${char}" to match literal characters`;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const splitTopLevel = input => {
|
|
49
|
+
const parts = [];
|
|
50
|
+
let bracket = 0;
|
|
51
|
+
let paren = 0;
|
|
52
|
+
let quote = 0;
|
|
53
|
+
let value = '';
|
|
54
|
+
let escaped = false;
|
|
55
|
+
|
|
56
|
+
for (const ch of input) {
|
|
57
|
+
if (escaped === true) {
|
|
58
|
+
value += ch;
|
|
59
|
+
escaped = false;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (ch === '\\') {
|
|
64
|
+
value += ch;
|
|
65
|
+
escaped = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (ch === '"') {
|
|
70
|
+
quote = quote === 1 ? 0 : 1;
|
|
71
|
+
value += ch;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (quote === 0) {
|
|
76
|
+
if (ch === '[') {
|
|
77
|
+
bracket++;
|
|
78
|
+
} else if (ch === ']' && bracket > 0) {
|
|
79
|
+
bracket--;
|
|
80
|
+
} else if (bracket === 0) {
|
|
81
|
+
if (ch === '(') {
|
|
82
|
+
paren++;
|
|
83
|
+
} else if (ch === ')' && paren > 0) {
|
|
84
|
+
paren--;
|
|
85
|
+
} else if (ch === '|' && paren === 0) {
|
|
86
|
+
parts.push(value);
|
|
87
|
+
value = '';
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
value += ch;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
parts.push(value);
|
|
97
|
+
return parts;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const isPlainBranch = branch => {
|
|
101
|
+
let escaped = false;
|
|
102
|
+
|
|
103
|
+
for (const ch of branch) {
|
|
104
|
+
if (escaped === true) {
|
|
105
|
+
escaped = false;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (ch === '\\') {
|
|
110
|
+
escaped = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (/[?*+@!()[\]{}]/.test(ch)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return true;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const normalizeSimpleBranch = branch => {
|
|
123
|
+
let value = branch.trim();
|
|
124
|
+
let changed = true;
|
|
125
|
+
|
|
126
|
+
while (changed === true) {
|
|
127
|
+
changed = false;
|
|
128
|
+
|
|
129
|
+
if (/^@\([^\\()[\]{}|]+\)$/.test(value)) {
|
|
130
|
+
value = value.slice(2, -1);
|
|
131
|
+
changed = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!isPlainBranch(value)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return value.replace(/\\(.)/g, '$1');
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const hasRepeatedCharPrefixOverlap = branches => {
|
|
143
|
+
const values = branches.map(normalizeSimpleBranch).filter(Boolean);
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < values.length; i++) {
|
|
146
|
+
for (let j = i + 1; j < values.length; j++) {
|
|
147
|
+
const a = values[i];
|
|
148
|
+
const b = values[j];
|
|
149
|
+
const char = a[0];
|
|
150
|
+
|
|
151
|
+
if (!char || a !== char.repeat(a.length) || b !== char.repeat(b.length)) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (a === b || a.startsWith(b) || b.startsWith(a)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return false;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const parseRepeatedExtglob = (pattern, requireEnd = true, types = '+*') => {
|
|
165
|
+
if (!types.includes(pattern[0]) || pattern[1] !== '(') {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let bracket = 0;
|
|
170
|
+
let paren = 0;
|
|
171
|
+
let quote = 0;
|
|
172
|
+
let escaped = false;
|
|
173
|
+
|
|
174
|
+
for (let i = 1; i < pattern.length; i++) {
|
|
175
|
+
const ch = pattern[i];
|
|
176
|
+
|
|
177
|
+
if (escaped === true) {
|
|
178
|
+
escaped = false;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (ch === '\\') {
|
|
183
|
+
escaped = true;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (ch === '"') {
|
|
188
|
+
quote = quote === 1 ? 0 : 1;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (quote === 1) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (ch === '[') {
|
|
197
|
+
bracket++;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (ch === ']' && bracket > 0) {
|
|
202
|
+
bracket--;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (bracket > 0) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (ch === '(') {
|
|
211
|
+
paren++;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (ch === ')') {
|
|
216
|
+
paren--;
|
|
217
|
+
|
|
218
|
+
if (paren === 0) {
|
|
219
|
+
if (requireEnd === true && i !== pattern.length - 1) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
type: pattern[0],
|
|
225
|
+
body: pattern.slice(2, i),
|
|
226
|
+
end: i
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const buildCharClassStar = chars => {
|
|
234
|
+
const source = chars.length === 1
|
|
235
|
+
? utils.escapeRegex(chars[0])
|
|
236
|
+
: `[${chars.map(ch => utils.escapeRegex(ch)).join('')}]`;
|
|
237
|
+
|
|
238
|
+
return `${source}*`;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const getStarExtglobSequenceChars = pattern => {
|
|
242
|
+
let index = 0;
|
|
243
|
+
const chars = [];
|
|
244
|
+
|
|
245
|
+
while (index < pattern.length) {
|
|
246
|
+
const match = parseRepeatedExtglob(pattern.slice(index), false);
|
|
247
|
+
|
|
248
|
+
if (!match || match.type !== '*') {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const branches = splitTopLevel(match.body).map(branch => branch.trim());
|
|
253
|
+
if (branches.length !== 1) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const branch = normalizeSimpleBranch(branches[0]);
|
|
258
|
+
if (!branch || branch.length !== 1) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
chars.push(branch);
|
|
263
|
+
index += match.end + 1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (chars.length < 1) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return chars;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// Whether a branch has a `*` of its own: not escaped, quoted, bracketed, nested in parens, or opening a `*(...)`.
|
|
274
|
+
const hasTopLevelStar = branch => {
|
|
275
|
+
let bracket = 0;
|
|
276
|
+
let paren = 0;
|
|
277
|
+
let quote = 0;
|
|
278
|
+
let escaped = false;
|
|
279
|
+
|
|
280
|
+
for (let i = 0; i < branch.length; i++) {
|
|
281
|
+
const ch = branch[i];
|
|
282
|
+
|
|
283
|
+
if (escaped === true) {
|
|
284
|
+
escaped = false;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (ch === '\\') {
|
|
289
|
+
escaped = true;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (ch === '"') {
|
|
294
|
+
quote = quote === 1 ? 0 : 1;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (quote === 1) {
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (ch === '[') {
|
|
303
|
+
bracket++;
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (ch === ']' && bracket > 0) {
|
|
308
|
+
bracket--;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (bracket > 0) {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (ch === '(') {
|
|
317
|
+
paren++;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (ch === ')') {
|
|
322
|
+
paren--;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (ch === '*' && paren === 0 && branch[i + 1] !== '(') {
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return false;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const repeatedExtglobRecursion = pattern => {
|
|
335
|
+
let depth = 0;
|
|
336
|
+
let value = pattern.trim();
|
|
337
|
+
let match = parseRepeatedExtglob(value);
|
|
338
|
+
|
|
339
|
+
while (match) {
|
|
340
|
+
depth++;
|
|
341
|
+
value = match.body.trim();
|
|
342
|
+
match = parseRepeatedExtglob(value);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return depth;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const analyzeRepeatedExtglob = (body, options) => {
|
|
349
|
+
if (options.maxExtglobRecursion === false) {
|
|
350
|
+
return { risky: false };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const max =
|
|
354
|
+
typeof options.maxExtglobRecursion === 'number'
|
|
355
|
+
? options.maxExtglobRecursion
|
|
356
|
+
: constants.DEFAULT_MAX_EXTGLOB_RECURSION;
|
|
357
|
+
|
|
358
|
+
let branches = splitTopLevel(body).map(branch => branch.trim());
|
|
359
|
+
let at;
|
|
360
|
+
|
|
361
|
+
// A lone `@(...)` branch is its own alternatives: `+(@(a|aa))` repeats `a|aa` as `+(a|aa)` does.
|
|
362
|
+
while (branches.length === 1 && (at = parseRepeatedExtglob(branches[0], true, '@'))) {
|
|
363
|
+
branches = splitTopLevel(at.body).map(branch => branch.trim());
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (branches.length > 1) {
|
|
367
|
+
if (
|
|
368
|
+
branches.some(branch => branch === '') ||
|
|
369
|
+
branches.some(branch => /^[*?]+$/.test(branch)) ||
|
|
370
|
+
hasRepeatedCharPrefixOverlap(branches)
|
|
371
|
+
) {
|
|
372
|
+
return { risky: true };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// A repeated extglob is "risky" (prone to catastrophic backtracking) when a
|
|
377
|
+
// branch is itself a `*(...)` sequence, since that nests an unbounded quantifier
|
|
378
|
+
// inside the outer `+(...)`/`*(...)`. When *every* branch reduces to single
|
|
379
|
+
// characters we can emit one flat, ReDoS-safe character class that preserves the
|
|
380
|
+
// meaning of ALL branches (e.g. `+(*(a)|*(b))` -> `[ab]*`), rather than dropping
|
|
381
|
+
// every branch but the first.
|
|
382
|
+
const safeChars = [];
|
|
383
|
+
let sawStarSequence = false;
|
|
384
|
+
let combinable = true;
|
|
385
|
+
|
|
386
|
+
for (const branch of branches) {
|
|
387
|
+
const chars = getStarExtglobSequenceChars(branch);
|
|
388
|
+
if (chars) {
|
|
389
|
+
sawStarSequence = true;
|
|
390
|
+
safeChars.push(...chars);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const literal = normalizeSimpleBranch(branch);
|
|
395
|
+
if (literal && literal.length === 1) {
|
|
396
|
+
safeChars.push(literal);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
combinable = false;
|
|
401
|
+
|
|
402
|
+
if (repeatedExtglobRecursion(branch) > max) {
|
|
403
|
+
return { risky: true };
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (sawStarSequence) {
|
|
408
|
+
return combinable
|
|
409
|
+
? { risky: true, safeOutput: buildCharClassStar([...new Set(safeChars)]) }
|
|
410
|
+
: { risky: true };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Repeating one branch with a `*` of its own matches nothing the branch does not match once, because the star
|
|
414
|
+
// absorbs every repetition after the first: `+(*a)` matches what `*a` does. Compiled once, the branch has no
|
|
415
|
+
// quantifier nested in another to backtrack over (CVE-2026-112366). A `/` would stop a star from absorbing.
|
|
416
|
+
if (branches.length === 1 && !branches[0].includes('/') && hasTopLevelStar(branches[0])) {
|
|
417
|
+
return { risky: false, once: true };
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return { risky: false };
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Parse the given input string.
|
|
425
|
+
* @param {String} input
|
|
426
|
+
* @param {Object} options
|
|
427
|
+
* @return {Object}
|
|
428
|
+
*/
|
|
429
|
+
|
|
430
|
+
const parse = (input, options) => {
|
|
431
|
+
if (typeof input !== 'string') {
|
|
432
|
+
throw new TypeError('Expected a string');
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
input = REPLACEMENTS[input] || input;
|
|
436
|
+
|
|
437
|
+
const opts = { ...options };
|
|
438
|
+
const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
|
|
439
|
+
|
|
440
|
+
let len = input.length;
|
|
441
|
+
if (len > max) {
|
|
442
|
+
throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const bos = { type: 'bos', value: '', output: opts.prepend || '' };
|
|
446
|
+
const tokens = [bos];
|
|
447
|
+
|
|
448
|
+
const capture = opts.capture ? '' : '?:';
|
|
449
|
+
|
|
450
|
+
// create constants based on platform, for windows or posix
|
|
451
|
+
const PLATFORM_CHARS = constants.globChars(opts.windows);
|
|
452
|
+
const EXTGLOB_CHARS = constants.extglobChars(PLATFORM_CHARS);
|
|
453
|
+
|
|
454
|
+
const {
|
|
455
|
+
DOT_LITERAL,
|
|
456
|
+
PLUS_LITERAL,
|
|
457
|
+
SLASH_LITERAL,
|
|
458
|
+
ONE_CHAR,
|
|
459
|
+
DOTS_SLASH,
|
|
460
|
+
NO_DOT,
|
|
461
|
+
NO_DOT_SLASH,
|
|
462
|
+
NO_DOTS_SLASH,
|
|
463
|
+
QMARK,
|
|
464
|
+
QMARK_NO_DOT,
|
|
465
|
+
STAR,
|
|
466
|
+
START_ANCHOR
|
|
467
|
+
} = PLATFORM_CHARS;
|
|
468
|
+
|
|
469
|
+
const globstar = opts => {
|
|
470
|
+
return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const nodot = opts.dot ? '' : NO_DOT;
|
|
474
|
+
const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
|
|
475
|
+
let star = opts.bash === true ? globstar(opts) : STAR;
|
|
476
|
+
|
|
477
|
+
if (opts.capture) {
|
|
478
|
+
star = `(${star})`;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// minimatch options support
|
|
482
|
+
if (typeof opts.noext === 'boolean') {
|
|
483
|
+
opts.noextglob = opts.noext;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const state = {
|
|
487
|
+
input,
|
|
488
|
+
index: -1,
|
|
489
|
+
start: 0,
|
|
490
|
+
dot: opts.dot === true,
|
|
491
|
+
consumed: '',
|
|
492
|
+
output: '',
|
|
493
|
+
prefix: '',
|
|
494
|
+
backtrack: false,
|
|
495
|
+
negated: false,
|
|
496
|
+
brackets: 0,
|
|
497
|
+
braces: 0,
|
|
498
|
+
parens: 0,
|
|
499
|
+
quotes: 0,
|
|
500
|
+
globstar: false,
|
|
501
|
+
tokens
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
input = utils.removePrefix(input, state);
|
|
505
|
+
len = input.length;
|
|
506
|
+
|
|
507
|
+
const extglobs = [];
|
|
508
|
+
const braces = [];
|
|
509
|
+
const stack = [];
|
|
510
|
+
let prev = bos;
|
|
511
|
+
let value;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Tokenizing helpers
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
const eos = () => state.index === len - 1;
|
|
518
|
+
const peek = state.peek = (n = 1) => input[state.index + n];
|
|
519
|
+
const advance = state.advance = () => input[++state.index] || '';
|
|
520
|
+
const remaining = () => input.slice(state.index + 1);
|
|
521
|
+
const consume = (value = '', num = 0) => {
|
|
522
|
+
state.consumed += value;
|
|
523
|
+
state.index += num;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
const append = token => {
|
|
527
|
+
state.output += token.output != null ? token.output : token.value;
|
|
528
|
+
consume(token.value);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const negate = () => {
|
|
532
|
+
let count = 1;
|
|
533
|
+
|
|
534
|
+
while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
|
|
535
|
+
advance();
|
|
536
|
+
state.start++;
|
|
537
|
+
count++;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (count % 2 === 0) {
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
state.negated = true;
|
|
545
|
+
state.start++;
|
|
546
|
+
return true;
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const increment = type => {
|
|
550
|
+
state[type]++;
|
|
551
|
+
stack.push(type);
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
const decrement = type => {
|
|
555
|
+
state[type]--;
|
|
556
|
+
stack.pop();
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Push tokens onto the tokens array. This helper speeds up
|
|
561
|
+
* tokenizing by 1) helping us avoid backtracking as much as possible,
|
|
562
|
+
* and 2) helping us avoid creating extra tokens when consecutive
|
|
563
|
+
* characters are plain text. This improves performance and simplifies
|
|
564
|
+
* lookbehinds.
|
|
565
|
+
*/
|
|
566
|
+
|
|
567
|
+
const push = tok => {
|
|
568
|
+
if (prev.type === 'globstar') {
|
|
569
|
+
const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
|
|
570
|
+
const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
|
|
571
|
+
|
|
572
|
+
if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
|
|
573
|
+
state.output = state.output.slice(0, -prev.output.length);
|
|
574
|
+
prev.type = 'star';
|
|
575
|
+
prev.value = '*';
|
|
576
|
+
prev.output = star;
|
|
577
|
+
state.output += prev.output;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (extglobs.length && tok.type !== 'paren') {
|
|
582
|
+
extglobs[extglobs.length - 1].inner += tok.value;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if (tok.value || tok.output) append(tok);
|
|
586
|
+
if (prev && prev.type === 'text' && tok.type === 'text') {
|
|
587
|
+
prev.output = (prev.output || prev.value) + tok.value;
|
|
588
|
+
prev.value += tok.value;
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
tok.prev = prev;
|
|
593
|
+
tokens.push(tok);
|
|
594
|
+
prev = tok;
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
const extglobOpen = (type, value) => {
|
|
598
|
+
const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
|
|
599
|
+
|
|
600
|
+
token.prev = prev;
|
|
601
|
+
token.parens = state.parens;
|
|
602
|
+
token.output = state.output;
|
|
603
|
+
token.startIndex = state.index;
|
|
604
|
+
token.tokensIndex = tokens.length;
|
|
605
|
+
const output = (opts.capture ? '(' : '') + token.open;
|
|
606
|
+
|
|
607
|
+
increment('parens');
|
|
608
|
+
push({ type, value, output: state.output ? '' : ONE_CHAR });
|
|
609
|
+
push({ type: 'paren', extglob: true, value: advance(), output });
|
|
610
|
+
extglobs.push(token);
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const extglobClose = token => {
|
|
614
|
+
const literal = input.slice(token.startIndex, state.index + 1);
|
|
615
|
+
const body = input.slice(token.startIndex + 2, state.index);
|
|
616
|
+
const analysis = analyzeRepeatedExtglob(body, opts);
|
|
617
|
+
|
|
618
|
+
if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
|
|
619
|
+
const safeOutput = analysis.safeOutput
|
|
620
|
+
? (token.output ? '' : ONE_CHAR) + (opts.capture ? `(${analysis.safeOutput})` : analysis.safeOutput)
|
|
621
|
+
: undefined;
|
|
622
|
+
const open = tokens[token.tokensIndex];
|
|
623
|
+
|
|
624
|
+
open.type = 'text';
|
|
625
|
+
open.value = literal;
|
|
626
|
+
open.output = safeOutput || utils.escapeRegex(literal);
|
|
627
|
+
|
|
628
|
+
for (let i = token.tokensIndex + 1; i < tokens.length; i++) {
|
|
629
|
+
tokens[i].value = '';
|
|
630
|
+
tokens[i].output = '';
|
|
631
|
+
delete tokens[i].suffix;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
state.output = token.output + open.output;
|
|
635
|
+
state.backtrack = true;
|
|
636
|
+
|
|
637
|
+
push({ type: 'paren', extglob: true, value, output: '' });
|
|
638
|
+
decrement('parens');
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
let close = token.close;
|
|
643
|
+
|
|
644
|
+
// A branch that matches the same repeated or once: `+(X)` closes as `@(X)` does, `*(X)` as `?(X)` does.
|
|
645
|
+
if (analysis.once === true && (token.type === 'plus' || token.type === 'star')) {
|
|
646
|
+
close = token.type === 'plus' ? ')' : ')?';
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
let output = close + (opts.capture ? ')' : '');
|
|
650
|
+
let rest;
|
|
651
|
+
|
|
652
|
+
if (token.type === 'negate') {
|
|
653
|
+
let extglobStar = star;
|
|
654
|
+
|
|
655
|
+
if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
|
|
656
|
+
extglobStar = globstar(opts);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
|
|
660
|
+
output = token.close = `)$))${extglobStar}`;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
|
|
664
|
+
// Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
|
|
665
|
+
// In this case, we need to parse the string and use it in the output of the original pattern.
|
|
666
|
+
// Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
|
|
667
|
+
//
|
|
668
|
+
// Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
|
|
669
|
+
const expression = parse(rest, { ...options, fastpaths: false }).output;
|
|
670
|
+
|
|
671
|
+
output = token.close = `)${expression})${extglobStar})`;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
if (token.prev.type === 'bos') {
|
|
675
|
+
state.negatedExtglob = true;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
push({ type: 'paren', extglob: true, value, output });
|
|
680
|
+
decrement('parens');
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Fast paths
|
|
685
|
+
*/
|
|
686
|
+
|
|
687
|
+
if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
|
|
688
|
+
let backslashes = false;
|
|
689
|
+
|
|
690
|
+
let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
|
|
691
|
+
if (first === '\\') {
|
|
692
|
+
backslashes = true;
|
|
693
|
+
return m;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (first === '?') {
|
|
697
|
+
if (esc) {
|
|
698
|
+
return esc + first + (rest ? QMARK.repeat(rest.length) : '');
|
|
699
|
+
}
|
|
700
|
+
if (index === 0) {
|
|
701
|
+
return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
|
|
702
|
+
}
|
|
703
|
+
return QMARK.repeat(chars.length);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
if (first === '.') {
|
|
707
|
+
return DOT_LITERAL.repeat(chars.length);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
if (first === '*') {
|
|
711
|
+
if (esc) {
|
|
712
|
+
return esc + first + (rest ? star : '');
|
|
713
|
+
}
|
|
714
|
+
return star;
|
|
715
|
+
}
|
|
716
|
+
return esc ? m : `\\${m}`;
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
if (backslashes === true) {
|
|
720
|
+
if (opts.unescape === true) {
|
|
721
|
+
output = output.replace(/\\/g, '');
|
|
722
|
+
} else {
|
|
723
|
+
output = output.replace(/\\+/g, m => {
|
|
724
|
+
return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
if (output === input && opts.contains === true) {
|
|
730
|
+
state.output = input;
|
|
731
|
+
return state;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
state.output = utils.wrapOutput(output, state, options);
|
|
735
|
+
return state;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Tokenize input until we reach end-of-string
|
|
740
|
+
*/
|
|
741
|
+
|
|
742
|
+
while (!eos()) {
|
|
743
|
+
value = advance();
|
|
744
|
+
|
|
745
|
+
if (value === '\u0000') {
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Escaped characters
|
|
751
|
+
*/
|
|
752
|
+
|
|
753
|
+
if (value === '\\') {
|
|
754
|
+
const next = peek();
|
|
755
|
+
|
|
756
|
+
if (next === '/' && opts.bash !== true) {
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
if (next === '.' || next === ';') {
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
if (!next) {
|
|
765
|
+
value += '\\';
|
|
766
|
+
push({ type: 'text', value });
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// collapse slashes to reduce potential for exploits
|
|
771
|
+
const match = /^\\+/.exec(remaining());
|
|
772
|
+
let slashes = 0;
|
|
773
|
+
|
|
774
|
+
if (match && match[0].length > 2) {
|
|
775
|
+
slashes = match[0].length;
|
|
776
|
+
state.index += slashes;
|
|
777
|
+
if (slashes % 2 !== 0) {
|
|
778
|
+
value += '\\';
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (opts.unescape === true) {
|
|
783
|
+
value = advance();
|
|
784
|
+
} else {
|
|
785
|
+
value += advance();
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
if (state.brackets === 0) {
|
|
789
|
+
push({ type: 'text', value });
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* If we're inside a regex character class, continue
|
|
796
|
+
* until we reach the closing bracket.
|
|
797
|
+
*/
|
|
798
|
+
|
|
799
|
+
if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
|
|
800
|
+
if (opts.posix !== false && value === ':') {
|
|
801
|
+
const inner = prev.value.slice(1);
|
|
802
|
+
if (inner.includes('[')) {
|
|
803
|
+
prev.posix = true;
|
|
804
|
+
|
|
805
|
+
if (inner.includes(':')) {
|
|
806
|
+
const idx = prev.value.lastIndexOf('[');
|
|
807
|
+
const pre = prev.value.slice(0, idx);
|
|
808
|
+
const rest = prev.value.slice(idx + 2);
|
|
809
|
+
const posix = POSIX_REGEX_SOURCE[rest];
|
|
810
|
+
if (posix) {
|
|
811
|
+
prev.value = pre + posix;
|
|
812
|
+
state.backtrack = true;
|
|
813
|
+
advance();
|
|
814
|
+
|
|
815
|
+
if (!bos.output && tokens.indexOf(prev) === 1) {
|
|
816
|
+
bos.output = ONE_CHAR;
|
|
817
|
+
}
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
|
|
825
|
+
value = `\\${value}`;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
|
|
829
|
+
value = `\\${value}`;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
if (opts.posix === true && value === '!' && prev.value === '[') {
|
|
833
|
+
value = '^';
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
prev.value += value;
|
|
837
|
+
append({ value });
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* If we're inside a quoted string, continue
|
|
843
|
+
* until we reach the closing double quote.
|
|
844
|
+
*/
|
|
845
|
+
|
|
846
|
+
if (state.quotes === 1 && value !== '"') {
|
|
847
|
+
value = utils.escapeRegex(value);
|
|
848
|
+
prev.value += value;
|
|
849
|
+
append({ value });
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* Double quotes
|
|
855
|
+
*/
|
|
856
|
+
|
|
857
|
+
if (value === '"') {
|
|
858
|
+
state.quotes = state.quotes === 1 ? 0 : 1;
|
|
859
|
+
if (opts.keepQuotes === true) {
|
|
860
|
+
push({ type: 'text', value });
|
|
861
|
+
}
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Parentheses
|
|
867
|
+
*/
|
|
868
|
+
|
|
869
|
+
if (value === '(') {
|
|
870
|
+
increment('parens');
|
|
871
|
+
push({ type: 'paren', value });
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (value === ')') {
|
|
876
|
+
if (state.parens === 0 && opts.strictBrackets === true) {
|
|
877
|
+
throw new SyntaxError(syntaxError('opening', '('));
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
const extglob = extglobs[extglobs.length - 1];
|
|
881
|
+
if (extglob && state.parens === extglob.parens + 1) {
|
|
882
|
+
extglobClose(extglobs.pop());
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
|
|
887
|
+
decrement('parens');
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Square brackets
|
|
893
|
+
*/
|
|
894
|
+
|
|
895
|
+
if (value === '[') {
|
|
896
|
+
if (opts.nobracket === true || !remaining().includes(']')) {
|
|
897
|
+
if (opts.nobracket !== true && opts.strictBrackets === true) {
|
|
898
|
+
throw new SyntaxError(syntaxError('closing', ']'));
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
value = `\\${value}`;
|
|
902
|
+
} else {
|
|
903
|
+
increment('brackets');
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
push({ type: 'bracket', value });
|
|
907
|
+
continue;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
if (value === ']') {
|
|
911
|
+
if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
|
|
912
|
+
push({ type: 'text', value, output: `\\${value}` });
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
if (state.brackets === 0) {
|
|
917
|
+
if (opts.strictBrackets === true) {
|
|
918
|
+
throw new SyntaxError(syntaxError('opening', '['));
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
push({ type: 'text', value, output: `\\${value}` });
|
|
922
|
+
continue;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
decrement('brackets');
|
|
926
|
+
|
|
927
|
+
const prevValue = prev.value.slice(1);
|
|
928
|
+
if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
|
|
929
|
+
value = `/${value}`;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
prev.value += value;
|
|
933
|
+
append({ value });
|
|
934
|
+
|
|
935
|
+
// when literal brackets are explicitly disabled
|
|
936
|
+
// assume we should match with a regex character class
|
|
937
|
+
if (opts.literalBrackets === false || utils.hasRegexChars(prevValue)) {
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
const escaped = utils.escapeRegex(prev.value);
|
|
942
|
+
state.output = state.output.slice(0, -prev.value.length);
|
|
943
|
+
|
|
944
|
+
// when literal brackets are explicitly enabled
|
|
945
|
+
// assume we should escape the brackets to match literal characters
|
|
946
|
+
if (opts.literalBrackets === true) {
|
|
947
|
+
state.output += escaped;
|
|
948
|
+
prev.value = escaped;
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// when the user specifies nothing, try to match both
|
|
953
|
+
prev.value = `(${capture}${escaped}|${prev.value})`;
|
|
954
|
+
state.output += prev.value;
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* Braces
|
|
960
|
+
*/
|
|
961
|
+
|
|
962
|
+
if (value === '{' && opts.nobrace !== true) {
|
|
963
|
+
increment('braces');
|
|
964
|
+
|
|
965
|
+
const open = {
|
|
966
|
+
type: 'brace',
|
|
967
|
+
value,
|
|
968
|
+
output: '(',
|
|
969
|
+
outputIndex: state.output.length,
|
|
970
|
+
tokensIndex: state.tokens.length
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
braces.push(open);
|
|
974
|
+
push(open);
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
if (value === '}') {
|
|
979
|
+
const brace = braces[braces.length - 1];
|
|
980
|
+
|
|
981
|
+
if (opts.nobrace === true || !brace) {
|
|
982
|
+
push({ type: 'text', value, output: value });
|
|
983
|
+
continue;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
let output = ')';
|
|
987
|
+
|
|
988
|
+
if (brace.dots === true) {
|
|
989
|
+
const arr = tokens.slice();
|
|
990
|
+
const range = [];
|
|
991
|
+
|
|
992
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
993
|
+
tokens.pop();
|
|
994
|
+
if (arr[i].type === 'brace') {
|
|
995
|
+
break;
|
|
996
|
+
}
|
|
997
|
+
if (arr[i].type !== 'dots') {
|
|
998
|
+
range.unshift(arr[i].value);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
output = expandRange(range, opts);
|
|
1003
|
+
state.backtrack = true;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
if (brace.comma !== true && brace.dots !== true) {
|
|
1007
|
+
const out = state.output.slice(0, brace.outputIndex);
|
|
1008
|
+
const toks = state.tokens.slice(brace.tokensIndex);
|
|
1009
|
+
brace.value = brace.output = '\\{';
|
|
1010
|
+
value = output = '\\}';
|
|
1011
|
+
state.output = out;
|
|
1012
|
+
for (const t of toks) {
|
|
1013
|
+
state.output += (t.output || t.value);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
push({ type: 'brace', value, output });
|
|
1018
|
+
decrement('braces');
|
|
1019
|
+
braces.pop();
|
|
1020
|
+
continue;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* Pipes
|
|
1025
|
+
*/
|
|
1026
|
+
|
|
1027
|
+
if (value === '|') {
|
|
1028
|
+
if (extglobs.length > 0) {
|
|
1029
|
+
extglobs[extglobs.length - 1].conditions++;
|
|
1030
|
+
}
|
|
1031
|
+
push({ type: 'text', value });
|
|
1032
|
+
continue;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Commas
|
|
1037
|
+
*/
|
|
1038
|
+
|
|
1039
|
+
if (value === ',') {
|
|
1040
|
+
let output = value;
|
|
1041
|
+
|
|
1042
|
+
const brace = braces[braces.length - 1];
|
|
1043
|
+
if (brace && stack[stack.length - 1] === 'braces') {
|
|
1044
|
+
brace.comma = true;
|
|
1045
|
+
output = '|';
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
push({ type: 'comma', value, output });
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/**
|
|
1053
|
+
* Slashes
|
|
1054
|
+
*/
|
|
1055
|
+
|
|
1056
|
+
if (value === '/') {
|
|
1057
|
+
// if the beginning of the glob is "./", advance the start
|
|
1058
|
+
// to the current index, and don't add the "./" characters
|
|
1059
|
+
// to the state. This greatly simplifies lookbehinds when
|
|
1060
|
+
// checking for BOS characters like "!" and "." (not "./")
|
|
1061
|
+
if (prev.type === 'dot' && state.index === state.start + 1) {
|
|
1062
|
+
state.start = state.index + 1;
|
|
1063
|
+
state.consumed = '';
|
|
1064
|
+
state.output = '';
|
|
1065
|
+
tokens.pop();
|
|
1066
|
+
prev = bos; // reset "prev" to the first token
|
|
1067
|
+
continue;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
push({ type: 'slash', value, output: SLASH_LITERAL });
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Dots
|
|
1076
|
+
*/
|
|
1077
|
+
|
|
1078
|
+
if (value === '.') {
|
|
1079
|
+
if (state.braces > 0 && prev.type === 'dot') {
|
|
1080
|
+
if (prev.value === '.') prev.output = DOT_LITERAL;
|
|
1081
|
+
const brace = braces[braces.length - 1];
|
|
1082
|
+
prev.type = 'dots';
|
|
1083
|
+
prev.output += value;
|
|
1084
|
+
prev.value += value;
|
|
1085
|
+
brace.dots = true;
|
|
1086
|
+
continue;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
|
|
1090
|
+
push({ type: 'text', value, output: DOT_LITERAL });
|
|
1091
|
+
continue;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
push({ type: 'dot', value, output: DOT_LITERAL });
|
|
1095
|
+
continue;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* Question marks
|
|
1100
|
+
*/
|
|
1101
|
+
|
|
1102
|
+
if (value === '?') {
|
|
1103
|
+
const isGroup = prev && prev.value === '(';
|
|
1104
|
+
if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
|
|
1105
|
+
extglobOpen('qmark', value);
|
|
1106
|
+
continue;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
if (prev && prev.type === 'paren') {
|
|
1110
|
+
const next = peek();
|
|
1111
|
+
let output = value;
|
|
1112
|
+
|
|
1113
|
+
if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
|
|
1114
|
+
output = `\\${value}`;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
push({ type: 'text', value, output });
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
|
|
1122
|
+
push({ type: 'qmark', value, output: QMARK_NO_DOT });
|
|
1123
|
+
continue;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
push({ type: 'qmark', value, output: QMARK });
|
|
1127
|
+
continue;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* Exclamation
|
|
1132
|
+
*/
|
|
1133
|
+
|
|
1134
|
+
if (value === '!') {
|
|
1135
|
+
if (opts.noextglob !== true && peek() === '(') {
|
|
1136
|
+
if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
|
|
1137
|
+
extglobOpen('negate', value);
|
|
1138
|
+
continue;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
if (opts.nonegate !== true && state.index === 0) {
|
|
1143
|
+
negate();
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Plus
|
|
1150
|
+
*/
|
|
1151
|
+
|
|
1152
|
+
if (value === '+') {
|
|
1153
|
+
if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
|
|
1154
|
+
extglobOpen('plus', value);
|
|
1155
|
+
continue;
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
if ((prev && prev.value === '(') || opts.regex === false) {
|
|
1159
|
+
push({ type: 'plus', value, output: PLUS_LITERAL });
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
|
|
1164
|
+
push({ type: 'plus', value });
|
|
1165
|
+
continue;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
push({ type: 'plus', value: PLUS_LITERAL });
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* Plain text
|
|
1174
|
+
*/
|
|
1175
|
+
|
|
1176
|
+
if (value === '@') {
|
|
1177
|
+
if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
|
|
1178
|
+
push({ type: 'at', extglob: true, value, output: '' });
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
push({ type: 'text', value });
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Plain text
|
|
1188
|
+
*/
|
|
1189
|
+
|
|
1190
|
+
if (value !== '*') {
|
|
1191
|
+
if (value === '$' || value === '^') {
|
|
1192
|
+
value = `\\${value}`;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
|
|
1196
|
+
if (match) {
|
|
1197
|
+
value += match[0];
|
|
1198
|
+
state.index += match[0].length;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
push({ type: 'text', value });
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* Stars
|
|
1207
|
+
*/
|
|
1208
|
+
|
|
1209
|
+
if (prev && (prev.type === 'globstar' || prev.star === true)) {
|
|
1210
|
+
prev.type = 'star';
|
|
1211
|
+
prev.star = true;
|
|
1212
|
+
prev.value += value;
|
|
1213
|
+
prev.output = star;
|
|
1214
|
+
state.backtrack = true;
|
|
1215
|
+
state.globstar = true;
|
|
1216
|
+
consume(value);
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
let rest = remaining();
|
|
1221
|
+
if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
|
|
1222
|
+
extglobOpen('star', value);
|
|
1223
|
+
continue;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
if (prev.type === 'star') {
|
|
1227
|
+
if (opts.noglobstar === true) {
|
|
1228
|
+
consume(value);
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
const prior = prev.prev;
|
|
1233
|
+
const before = prior.prev;
|
|
1234
|
+
const isStart = prior.type === 'slash' || prior.type === 'bos';
|
|
1235
|
+
const afterStar = before && (before.type === 'star' || before.type === 'globstar');
|
|
1236
|
+
|
|
1237
|
+
if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
|
|
1238
|
+
push({ type: 'star', value, output: '' });
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
|
|
1243
|
+
const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
|
|
1244
|
+
if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
|
|
1245
|
+
push({ type: 'star', value, output: '' });
|
|
1246
|
+
continue;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// strip consecutive `/**/`
|
|
1250
|
+
while (rest.slice(0, 3) === '/**') {
|
|
1251
|
+
const after = input[state.index + 4];
|
|
1252
|
+
if (after && after !== '/') {
|
|
1253
|
+
break;
|
|
1254
|
+
}
|
|
1255
|
+
rest = rest.slice(3);
|
|
1256
|
+
consume('/**', 3);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
// A globstar followed only by balanced closing parens is at the logical end of patterns like
|
|
1260
|
+
// `test(/utils/**)` and `test?(/utils/**)`. Treat it as EOS so the trailing `/**` can match its
|
|
1261
|
+
// parent path, except in negated extglobs where that would change the exclusion semantics.
|
|
1262
|
+
const isEnd = eos() || (
|
|
1263
|
+
state.parens > 0
|
|
1264
|
+
&& rest === ')'.repeat(state.parens)
|
|
1265
|
+
&& !extglobs.some(extglob => extglob.type === 'negate')
|
|
1266
|
+
);
|
|
1267
|
+
|
|
1268
|
+
if (prior.type === 'bos' && eos()) {
|
|
1269
|
+
prev.type = 'globstar';
|
|
1270
|
+
prev.value += value;
|
|
1271
|
+
prev.output = globstar(opts);
|
|
1272
|
+
state.output = prev.output;
|
|
1273
|
+
state.globstar = true;
|
|
1274
|
+
consume(value);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && isEnd) {
|
|
1279
|
+
state.output = state.output.slice(0, -(prior.output + prev.output).length);
|
|
1280
|
+
prior.output = `(?:${prior.output}`;
|
|
1281
|
+
|
|
1282
|
+
prev.type = 'globstar';
|
|
1283
|
+
prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
|
|
1284
|
+
prev.value += value;
|
|
1285
|
+
state.globstar = true;
|
|
1286
|
+
state.output += prior.output + prev.output;
|
|
1287
|
+
consume(value);
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
|
|
1292
|
+
const end = rest[1] !== void 0 ? '|$' : '';
|
|
1293
|
+
|
|
1294
|
+
state.output = state.output.slice(0, -(prior.output + prev.output).length);
|
|
1295
|
+
prior.output = `(?:${prior.output}`;
|
|
1296
|
+
|
|
1297
|
+
prev.type = 'globstar';
|
|
1298
|
+
prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
|
|
1299
|
+
prev.value += value;
|
|
1300
|
+
|
|
1301
|
+
state.output += prior.output + prev.output;
|
|
1302
|
+
state.globstar = true;
|
|
1303
|
+
|
|
1304
|
+
consume(value + advance());
|
|
1305
|
+
|
|
1306
|
+
push({ type: 'slash', value: '/', output: '' });
|
|
1307
|
+
continue;
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
if (prior.type === 'bos' && rest[0] === '/') {
|
|
1311
|
+
prev.type = 'globstar';
|
|
1312
|
+
prev.value += value;
|
|
1313
|
+
prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
|
|
1314
|
+
state.output = prev.output;
|
|
1315
|
+
state.globstar = true;
|
|
1316
|
+
consume(value + advance());
|
|
1317
|
+
push({ type: 'slash', value: '/', output: '' });
|
|
1318
|
+
continue;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// remove single star from output
|
|
1322
|
+
state.output = state.output.slice(0, -prev.output.length);
|
|
1323
|
+
|
|
1324
|
+
// reset previous token to globstar
|
|
1325
|
+
prev.type = 'globstar';
|
|
1326
|
+
prev.output = globstar(opts);
|
|
1327
|
+
prev.value += value;
|
|
1328
|
+
|
|
1329
|
+
// reset output with globstar
|
|
1330
|
+
state.output += prev.output;
|
|
1331
|
+
state.globstar = true;
|
|
1332
|
+
consume(value);
|
|
1333
|
+
continue;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
const token = { type: 'star', value, output: star };
|
|
1337
|
+
|
|
1338
|
+
if (opts.bash === true) {
|
|
1339
|
+
token.output = '.*?';
|
|
1340
|
+
if (prev.type === 'bos' || prev.type === 'slash') {
|
|
1341
|
+
token.output = nodot + token.output;
|
|
1342
|
+
}
|
|
1343
|
+
push(token);
|
|
1344
|
+
continue;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
|
|
1348
|
+
token.output = value;
|
|
1349
|
+
push(token);
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
|
|
1354
|
+
if (prev.type === 'dot') {
|
|
1355
|
+
state.output += NO_DOT_SLASH;
|
|
1356
|
+
prev.output += NO_DOT_SLASH;
|
|
1357
|
+
|
|
1358
|
+
} else if (opts.dot === true) {
|
|
1359
|
+
state.output += NO_DOTS_SLASH;
|
|
1360
|
+
prev.output += NO_DOTS_SLASH;
|
|
1361
|
+
|
|
1362
|
+
} else {
|
|
1363
|
+
state.output += nodot;
|
|
1364
|
+
prev.output += nodot;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
if (peek() !== '*') {
|
|
1368
|
+
state.output += ONE_CHAR;
|
|
1369
|
+
prev.output += ONE_CHAR;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
push(token);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
while (state.brackets > 0) {
|
|
1377
|
+
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
|
|
1378
|
+
state.output = utils.escapeLast(state.output, '[');
|
|
1379
|
+
decrement('brackets');
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
while (state.parens > 0) {
|
|
1383
|
+
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
|
|
1384
|
+
state.output = utils.escapeLast(state.output, '(');
|
|
1385
|
+
decrement('parens');
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
while (state.braces > 0) {
|
|
1389
|
+
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
|
|
1390
|
+
state.output = utils.escapeLast(state.output, '{');
|
|
1391
|
+
decrement('braces');
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
|
|
1395
|
+
push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// rebuild the output if we had to backtrack at any point
|
|
1399
|
+
if (state.backtrack === true) {
|
|
1400
|
+
state.output = '';
|
|
1401
|
+
|
|
1402
|
+
for (const token of state.tokens) {
|
|
1403
|
+
state.output += token.output != null ? token.output : token.value;
|
|
1404
|
+
|
|
1405
|
+
if (token.suffix) {
|
|
1406
|
+
state.output += token.suffix;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
return state;
|
|
1412
|
+
};
|
|
1413
|
+
|
|
1414
|
+
/**
|
|
1415
|
+
* Fast paths for creating regular expressions for common glob patterns.
|
|
1416
|
+
* This can significantly speed up processing and has very little downside
|
|
1417
|
+
* impact when none of the fast paths match.
|
|
1418
|
+
*/
|
|
1419
|
+
|
|
1420
|
+
parse.fastpaths = (input, options) => {
|
|
1421
|
+
const opts = { ...options };
|
|
1422
|
+
const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
|
|
1423
|
+
const len = input.length;
|
|
1424
|
+
if (len > max) {
|
|
1425
|
+
throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
input = REPLACEMENTS[input] || input;
|
|
1429
|
+
|
|
1430
|
+
// create constants based on platform, for windows or posix
|
|
1431
|
+
const {
|
|
1432
|
+
DOT_LITERAL,
|
|
1433
|
+
SLASH_LITERAL,
|
|
1434
|
+
ONE_CHAR,
|
|
1435
|
+
DOTS_SLASH,
|
|
1436
|
+
NO_DOT,
|
|
1437
|
+
NO_DOTS,
|
|
1438
|
+
NO_DOTS_SLASH,
|
|
1439
|
+
STAR,
|
|
1440
|
+
START_ANCHOR
|
|
1441
|
+
} = constants.globChars(opts.windows);
|
|
1442
|
+
|
|
1443
|
+
const nodot = opts.dot ? NO_DOTS : NO_DOT;
|
|
1444
|
+
const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
|
|
1445
|
+
const capture = opts.capture ? '' : '?:';
|
|
1446
|
+
const state = { negated: false, prefix: '' };
|
|
1447
|
+
let star = opts.bash === true ? '.*?' : STAR;
|
|
1448
|
+
|
|
1449
|
+
if (opts.capture) {
|
|
1450
|
+
star = `(${star})`;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
const globstar = opts => {
|
|
1454
|
+
if (opts.noglobstar === true) return star;
|
|
1455
|
+
return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
const create = str => {
|
|
1459
|
+
switch (str) {
|
|
1460
|
+
case '*':
|
|
1461
|
+
return `${nodot}${ONE_CHAR}${star}`;
|
|
1462
|
+
|
|
1463
|
+
case '.*':
|
|
1464
|
+
return `${DOT_LITERAL}${ONE_CHAR}${star}`;
|
|
1465
|
+
|
|
1466
|
+
case '*.*':
|
|
1467
|
+
return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
|
|
1468
|
+
|
|
1469
|
+
case '*/*':
|
|
1470
|
+
return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
|
|
1471
|
+
|
|
1472
|
+
case '**':
|
|
1473
|
+
return nodot + globstar(opts);
|
|
1474
|
+
|
|
1475
|
+
case '**/*':
|
|
1476
|
+
return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
|
|
1477
|
+
|
|
1478
|
+
case '**/*.*':
|
|
1479
|
+
return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
|
|
1480
|
+
|
|
1481
|
+
case '**/.*':
|
|
1482
|
+
return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
|
|
1483
|
+
|
|
1484
|
+
default: {
|
|
1485
|
+
const match = /^(.*?)\.(\w+)$/.exec(str);
|
|
1486
|
+
if (!match) return;
|
|
1487
|
+
|
|
1488
|
+
const source = create(match[1]);
|
|
1489
|
+
if (!source) return;
|
|
1490
|
+
|
|
1491
|
+
return source + DOT_LITERAL + match[2];
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
|
|
1496
|
+
const output = utils.removePrefix(input, state);
|
|
1497
|
+
let source = create(output);
|
|
1498
|
+
|
|
1499
|
+
if (source && opts.strictSlashes !== true) {
|
|
1500
|
+
source += `${SLASH_LITERAL}?`;
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
return source;
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
module.exports = parse;
|