parse.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. 'use strict';
  2. const constants = require('./constants');
  3. const utils = require('./utils');
  4. /**
  5. * Constants
  6. */
  7. const {
  8. MAX_LENGTH,
  9. POSIX_REGEX_SOURCE,
  10. REGEX_NON_SPECIAL_CHARS,
  11. REGEX_SPECIAL_CHARS_BACKREF,
  12. REPLACEMENTS
  13. } = constants;
  14. /**
  15. * Helpers
  16. */
  17. const expandRange = (args, options) => {
  18. if (typeof options.expandRange === 'function') {
  19. return options.expandRange(...args, options);
  20. }
  21. args.sort();
  22. const value = `[${args.join('-')}]`;
  23. try {
  24. /* eslint-disable-next-line no-new */
  25. new RegExp(value);
  26. } catch (ex) {
  27. return args.map(v => utils.escapeRegex(v)).join('..');
  28. }
  29. return value;
  30. };
  31. /**
  32. * Create the message for a syntax error
  33. */
  34. const syntaxError = (type, char) => {
  35. return `Missing ${type}: "${char}" - use "\\\\${char}" to match literal characters`;
  36. };
  37. const splitTopLevel = input => {
  38. const parts = [];
  39. let bracket = 0;
  40. let paren = 0;
  41. let quote = 0;
  42. let value = '';
  43. let escaped = false;
  44. for (const ch of input) {
  45. if (escaped === true) {
  46. value += ch;
  47. escaped = false;
  48. continue;
  49. }
  50. if (ch === '\\') {
  51. value += ch;
  52. escaped = true;
  53. continue;
  54. }
  55. if (ch === '"') {
  56. quote = quote === 1 ? 0 : 1;
  57. value += ch;
  58. continue;
  59. }
  60. if (quote === 0) {
  61. if (ch === '[') {
  62. bracket++;
  63. } else if (ch === ']' && bracket > 0) {
  64. bracket--;
  65. } else if (bracket === 0) {
  66. if (ch === '(') {
  67. paren++;
  68. } else if (ch === ')' && paren > 0) {
  69. paren--;
  70. } else if (ch === '|' && paren === 0) {
  71. parts.push(value);
  72. value = '';
  73. continue;
  74. }
  75. }
  76. }
  77. value += ch;
  78. }
  79. parts.push(value);
  80. return parts;
  81. };
  82. const isPlainBranch = branch => {
  83. let escaped = false;
  84. for (const ch of branch) {
  85. if (escaped === true) {
  86. escaped = false;
  87. continue;
  88. }
  89. if (ch === '\\') {
  90. escaped = true;
  91. continue;
  92. }
  93. if (/[?*+@!()[\]{}]/.test(ch)) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. };
  99. const normalizeSimpleBranch = branch => {
  100. let value = branch.trim();
  101. let changed = true;
  102. while (changed === true) {
  103. changed = false;
  104. if (/^@\([^\\()[\]{}|]+\)$/.test(value)) {
  105. value = value.slice(2, -1);
  106. changed = true;
  107. }
  108. }
  109. if (!isPlainBranch(value)) {
  110. return;
  111. }
  112. return value.replace(/\\(.)/g, '$1');
  113. };
  114. const hasRepeatedCharPrefixOverlap = branches => {
  115. const values = branches.map(normalizeSimpleBranch).filter(Boolean);
  116. for (let i = 0; i < values.length; i++) {
  117. for (let j = i + 1; j < values.length; j++) {
  118. const a = values[i];
  119. const b = values[j];
  120. const char = a[0];
  121. if (!char || a !== char.repeat(a.length) || b !== char.repeat(b.length)) {
  122. continue;
  123. }
  124. if (a === b || a.startsWith(b) || b.startsWith(a)) {
  125. return true;
  126. }
  127. }
  128. }
  129. return false;
  130. };
  131. const parseRepeatedExtglob = (pattern, requireEnd = true) => {
  132. if ((pattern[0] !== '+' && pattern[0] !== '*') || pattern[1] !== '(') {
  133. return;
  134. }
  135. let bracket = 0;
  136. let paren = 0;
  137. let quote = 0;
  138. let escaped = false;
  139. for (let i = 1; i < pattern.length; i++) {
  140. const ch = pattern[i];
  141. if (escaped === true) {
  142. escaped = false;
  143. continue;
  144. }
  145. if (ch === '\\') {
  146. escaped = true;
  147. continue;
  148. }
  149. if (ch === '"') {
  150. quote = quote === 1 ? 0 : 1;
  151. continue;
  152. }
  153. if (quote === 1) {
  154. continue;
  155. }
  156. if (ch === '[') {
  157. bracket++;
  158. continue;
  159. }
  160. if (ch === ']' && bracket > 0) {
  161. bracket--;
  162. continue;
  163. }
  164. if (bracket > 0) {
  165. continue;
  166. }
  167. if (ch === '(') {
  168. paren++;
  169. continue;
  170. }
  171. if (ch === ')') {
  172. paren--;
  173. if (paren === 0) {
  174. if (requireEnd === true && i !== pattern.length - 1) {
  175. return;
  176. }
  177. return {
  178. type: pattern[0],
  179. body: pattern.slice(2, i),
  180. end: i
  181. };
  182. }
  183. }
  184. }
  185. };
  186. const getStarExtglobSequenceOutput = pattern => {
  187. let index = 0;
  188. const chars = [];
  189. while (index < pattern.length) {
  190. const match = parseRepeatedExtglob(pattern.slice(index), false);
  191. if (!match || match.type !== '*') {
  192. return;
  193. }
  194. const branches = splitTopLevel(match.body).map(branch => branch.trim());
  195. if (branches.length !== 1) {
  196. return;
  197. }
  198. const branch = normalizeSimpleBranch(branches[0]);
  199. if (!branch || branch.length !== 1) {
  200. return;
  201. }
  202. chars.push(branch);
  203. index += match.end + 1;
  204. }
  205. if (chars.length < 1) {
  206. return;
  207. }
  208. const source = chars.length === 1
  209. ? utils.escapeRegex(chars[0])
  210. : `[${chars.map(ch => utils.escapeRegex(ch)).join('')}]`;
  211. return `${source}*`;
  212. };
  213. const repeatedExtglobRecursion = pattern => {
  214. let depth = 0;
  215. let value = pattern.trim();
  216. let match = parseRepeatedExtglob(value);
  217. while (match) {
  218. depth++;
  219. value = match.body.trim();
  220. match = parseRepeatedExtglob(value);
  221. }
  222. return depth;
  223. };
  224. const analyzeRepeatedExtglob = (body, options) => {
  225. if (options.maxExtglobRecursion === false) {
  226. return { risky: false };
  227. }
  228. const max =
  229. typeof options.maxExtglobRecursion === 'number'
  230. ? options.maxExtglobRecursion
  231. : constants.DEFAULT_MAX_EXTGLOB_RECURSION;
  232. const branches = splitTopLevel(body).map(branch => branch.trim());
  233. if (branches.length > 1) {
  234. if (
  235. branches.some(branch => branch === '') ||
  236. branches.some(branch => /^[*?]+$/.test(branch)) ||
  237. hasRepeatedCharPrefixOverlap(branches)
  238. ) {
  239. return { risky: true };
  240. }
  241. }
  242. for (const branch of branches) {
  243. const safeOutput = getStarExtglobSequenceOutput(branch);
  244. if (safeOutput) {
  245. return { risky: true, safeOutput };
  246. }
  247. if (repeatedExtglobRecursion(branch) > max) {
  248. return { risky: true };
  249. }
  250. }
  251. return { risky: false };
  252. };
  253. /**
  254. * Parse the given input string.
  255. * @param {String} input
  256. * @param {Object} options
  257. * @return {Object}
  258. */
  259. const parse = (input, options) => {
  260. if (typeof input !== 'string') {
  261. throw new TypeError('Expected a string');
  262. }
  263. input = REPLACEMENTS[input] || input;
  264. const opts = { ...options };
  265. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  266. let len = input.length;
  267. if (len > max) {
  268. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  269. }
  270. const bos = { type: 'bos', value: '', output: opts.prepend || '' };
  271. const tokens = [bos];
  272. const capture = opts.capture ? '' : '?:';
  273. const win32 = utils.isWindows(options);
  274. // create constants based on platform, for windows or posix
  275. const PLATFORM_CHARS = constants.globChars(win32);
  276. const EXTGLOB_CHARS = constants.extglobChars(PLATFORM_CHARS);
  277. const {
  278. DOT_LITERAL,
  279. PLUS_LITERAL,
  280. SLASH_LITERAL,
  281. ONE_CHAR,
  282. DOTS_SLASH,
  283. NO_DOT,
  284. NO_DOT_SLASH,
  285. NO_DOTS_SLASH,
  286. QMARK,
  287. QMARK_NO_DOT,
  288. STAR,
  289. START_ANCHOR
  290. } = PLATFORM_CHARS;
  291. const globstar = opts => {
  292. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  293. };
  294. const nodot = opts.dot ? '' : NO_DOT;
  295. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  296. let star = opts.bash === true ? globstar(opts) : STAR;
  297. if (opts.capture) {
  298. star = `(${star})`;
  299. }
  300. // minimatch options support
  301. if (typeof opts.noext === 'boolean') {
  302. opts.noextglob = opts.noext;
  303. }
  304. const state = {
  305. input,
  306. index: -1,
  307. start: 0,
  308. dot: opts.dot === true,
  309. consumed: '',
  310. output: '',
  311. prefix: '',
  312. backtrack: false,
  313. negated: false,
  314. brackets: 0,
  315. braces: 0,
  316. parens: 0,
  317. quotes: 0,
  318. globstar: false,
  319. tokens
  320. };
  321. input = utils.removePrefix(input, state);
  322. len = input.length;
  323. const extglobs = [];
  324. const braces = [];
  325. const stack = [];
  326. let prev = bos;
  327. let value;
  328. /**
  329. * Tokenizing helpers
  330. */
  331. const eos = () => state.index === len - 1;
  332. const peek = state.peek = (n = 1) => input[state.index + n];
  333. const advance = state.advance = () => input[++state.index] || '';
  334. const remaining = () => input.slice(state.index + 1);
  335. const consume = (value = '', num = 0) => {
  336. state.consumed += value;
  337. state.index += num;
  338. };
  339. const append = token => {
  340. state.output += token.output != null ? token.output : token.value;
  341. consume(token.value);
  342. };
  343. const negate = () => {
  344. let count = 1;
  345. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  346. advance();
  347. state.start++;
  348. count++;
  349. }
  350. if (count % 2 === 0) {
  351. return false;
  352. }
  353. state.negated = true;
  354. state.start++;
  355. return true;
  356. };
  357. const increment = type => {
  358. state[type]++;
  359. stack.push(type);
  360. };
  361. const decrement = type => {
  362. state[type]--;
  363. stack.pop();
  364. };
  365. /**
  366. * Push tokens onto the tokens array. This helper speeds up
  367. * tokenizing by 1) helping us avoid backtracking as much as possible,
  368. * and 2) helping us avoid creating extra tokens when consecutive
  369. * characters are plain text. This improves performance and simplifies
  370. * lookbehinds.
  371. */
  372. const push = tok => {
  373. if (prev.type === 'globstar') {
  374. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  375. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  376. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  377. state.output = state.output.slice(0, -prev.output.length);
  378. prev.type = 'star';
  379. prev.value = '*';
  380. prev.output = star;
  381. state.output += prev.output;
  382. }
  383. }
  384. if (extglobs.length && tok.type !== 'paren') {
  385. extglobs[extglobs.length - 1].inner += tok.value;
  386. }
  387. if (tok.value || tok.output) append(tok);
  388. if (prev && prev.type === 'text' && tok.type === 'text') {
  389. prev.value += tok.value;
  390. prev.output = (prev.output || '') + tok.value;
  391. return;
  392. }
  393. tok.prev = prev;
  394. tokens.push(tok);
  395. prev = tok;
  396. };
  397. const extglobOpen = (type, value) => {
  398. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  399. token.prev = prev;
  400. token.parens = state.parens;
  401. token.output = state.output;
  402. token.startIndex = state.index;
  403. token.tokensIndex = tokens.length;
  404. const output = (opts.capture ? '(' : '') + token.open;
  405. increment('parens');
  406. push({ type, value, output: state.output ? '' : ONE_CHAR });
  407. push({ type: 'paren', extglob: true, value: advance(), output });
  408. extglobs.push(token);
  409. };
  410. const extglobClose = token => {
  411. const literal = input.slice(token.startIndex, state.index + 1);
  412. const body = input.slice(token.startIndex + 2, state.index);
  413. const analysis = analyzeRepeatedExtglob(body, opts);
  414. if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
  415. const safeOutput = analysis.safeOutput
  416. ? (token.output ? '' : ONE_CHAR) + (opts.capture ? `(${analysis.safeOutput})` : analysis.safeOutput)
  417. : undefined;
  418. const open = tokens[token.tokensIndex];
  419. open.type = 'text';
  420. open.value = literal;
  421. open.output = safeOutput || utils.escapeRegex(literal);
  422. for (let i = token.tokensIndex + 1; i < tokens.length; i++) {
  423. tokens[i].value = '';
  424. tokens[i].output = '';
  425. delete tokens[i].suffix;
  426. }
  427. state.output = token.output + open.output;
  428. state.backtrack = true;
  429. push({ type: 'paren', extglob: true, value, output: '' });
  430. decrement('parens');
  431. return;
  432. }
  433. let output = token.close + (opts.capture ? ')' : '');
  434. let rest;
  435. if (token.type === 'negate') {
  436. let extglobStar = star;
  437. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  438. extglobStar = globstar(opts);
  439. }
  440. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  441. output = token.close = `)$))${extglobStar}`;
  442. }
  443. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  444. // Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
  445. // In this case, we need to parse the string and use it in the output of the original pattern.
  446. // Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
  447. //
  448. // Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
  449. const expression = parse(rest, { ...options, fastpaths: false }).output;
  450. output = token.close = `)${expression})${extglobStar})`;
  451. }
  452. if (token.prev.type === 'bos') {
  453. state.negatedExtglob = true;
  454. }
  455. }
  456. push({ type: 'paren', extglob: true, value, output });
  457. decrement('parens');
  458. };
  459. /**
  460. * Fast paths
  461. */
  462. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  463. let backslashes = false;
  464. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  465. if (first === '\\') {
  466. backslashes = true;
  467. return m;
  468. }
  469. if (first === '?') {
  470. if (esc) {
  471. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  472. }
  473. if (index === 0) {
  474. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  475. }
  476. return QMARK.repeat(chars.length);
  477. }
  478. if (first === '.') {
  479. return DOT_LITERAL.repeat(chars.length);
  480. }
  481. if (first === '*') {
  482. if (esc) {
  483. return esc + first + (rest ? star : '');
  484. }
  485. return star;
  486. }
  487. return esc ? m : `\\${m}`;
  488. });
  489. if (backslashes === true) {
  490. if (opts.unescape === true) {
  491. output = output.replace(/\\/g, '');
  492. } else {
  493. output = output.replace(/\\+/g, m => {
  494. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  495. });
  496. }
  497. }
  498. if (output === input && opts.contains === true) {
  499. state.output = input;
  500. return state;
  501. }
  502. state.output = utils.wrapOutput(output, state, options);
  503. return state;
  504. }
  505. /**
  506. * Tokenize input until we reach end-of-string
  507. */
  508. while (!eos()) {
  509. value = advance();
  510. if (value === '\u0000') {
  511. continue;
  512. }
  513. /**
  514. * Escaped characters
  515. */
  516. if (value === '\\') {
  517. const next = peek();
  518. if (next === '/' && opts.bash !== true) {
  519. continue;
  520. }
  521. if (next === '.' || next === ';') {
  522. continue;
  523. }
  524. if (!next) {
  525. value += '\\';
  526. push({ type: 'text', value });
  527. continue;
  528. }
  529. // collapse slashes to reduce potential for exploits
  530. const match = /^\\+/.exec(remaining());
  531. let slashes = 0;
  532. if (match && match[0].length > 2) {
  533. slashes = match[0].length;
  534. state.index += slashes;
  535. if (slashes % 2 !== 0) {
  536. value += '\\';
  537. }
  538. }
  539. if (opts.unescape === true) {
  540. value = advance();
  541. } else {
  542. value += advance();
  543. }
  544. if (state.brackets === 0) {
  545. push({ type: 'text', value });
  546. continue;
  547. }
  548. }
  549. /**
  550. * If we're inside a regex character class, continue
  551. * until we reach the closing bracket.
  552. */
  553. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  554. if (opts.posix !== false && value === ':') {
  555. const inner = prev.value.slice(1);
  556. if (inner.includes('[')) {
  557. prev.posix = true;
  558. if (inner.includes(':')) {
  559. const idx = prev.value.lastIndexOf('[');
  560. const pre = prev.value.slice(0, idx);
  561. const rest = prev.value.slice(idx + 2);
  562. const posix = POSIX_REGEX_SOURCE[rest];
  563. if (posix) {
  564. prev.value = pre + posix;
  565. state.backtrack = true;
  566. advance();
  567. if (!bos.output && tokens.indexOf(prev) === 1) {
  568. bos.output = ONE_CHAR;
  569. }
  570. continue;
  571. }
  572. }
  573. }
  574. }
  575. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  576. value = `\\${value}`;
  577. }
  578. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  579. value = `\\${value}`;
  580. }
  581. if (opts.posix === true && value === '!' && prev.value === '[') {
  582. value = '^';
  583. }
  584. prev.value += value;
  585. append({ value });
  586. continue;
  587. }
  588. /**
  589. * If we're inside a quoted string, continue
  590. * until we reach the closing double quote.
  591. */
  592. if (state.quotes === 1 && value !== '"') {
  593. value = utils.escapeRegex(value);
  594. prev.value += value;
  595. append({ value });
  596. continue;
  597. }
  598. /**
  599. * Double quotes
  600. */
  601. if (value === '"') {
  602. state.quotes = state.quotes === 1 ? 0 : 1;
  603. if (opts.keepQuotes === true) {
  604. push({ type: 'text', value });
  605. }
  606. continue;
  607. }
  608. /**
  609. * Parentheses
  610. */
  611. if (value === '(') {
  612. increment('parens');
  613. push({ type: 'paren', value });
  614. continue;
  615. }
  616. if (value === ')') {
  617. if (state.parens === 0 && opts.strictBrackets === true) {
  618. throw new SyntaxError(syntaxError('opening', '('));
  619. }
  620. const extglob = extglobs[extglobs.length - 1];
  621. if (extglob && state.parens === extglob.parens + 1) {
  622. extglobClose(extglobs.pop());
  623. continue;
  624. }
  625. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  626. decrement('parens');
  627. continue;
  628. }
  629. /**
  630. * Square brackets
  631. */
  632. if (value === '[') {
  633. if (opts.nobracket === true || !remaining().includes(']')) {
  634. if (opts.nobracket !== true && opts.strictBrackets === true) {
  635. throw new SyntaxError(syntaxError('closing', ']'));
  636. }
  637. value = `\\${value}`;
  638. } else {
  639. increment('brackets');
  640. }
  641. push({ type: 'bracket', value });
  642. continue;
  643. }
  644. if (value === ']') {
  645. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  646. push({ type: 'text', value, output: `\\${value}` });
  647. continue;
  648. }
  649. if (state.brackets === 0) {
  650. if (opts.strictBrackets === true) {
  651. throw new SyntaxError(syntaxError('opening', '['));
  652. }
  653. push({ type: 'text', value, output: `\\${value}` });
  654. continue;
  655. }
  656. decrement('brackets');
  657. const prevValue = prev.value.slice(1);
  658. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  659. value = `/${value}`;
  660. }
  661. prev.value += value;
  662. append({ value });
  663. // when literal brackets are explicitly disabled
  664. // assume we should match with a regex character class
  665. if (opts.literalBrackets === false || utils.hasRegexChars(prevValue)) {
  666. continue;
  667. }
  668. const escaped = utils.escapeRegex(prev.value);
  669. state.output = state.output.slice(0, -prev.value.length);
  670. // when literal brackets are explicitly enabled
  671. // assume we should escape the brackets to match literal characters
  672. if (opts.literalBrackets === true) {
  673. state.output += escaped;
  674. prev.value = escaped;
  675. continue;
  676. }
  677. // when the user specifies nothing, try to match both
  678. prev.value = `(${capture}${escaped}|${prev.value})`;
  679. state.output += prev.value;
  680. continue;
  681. }
  682. /**
  683. * Braces
  684. */
  685. if (value === '{' && opts.nobrace !== true) {
  686. increment('braces');
  687. const open = {
  688. type: 'brace',
  689. value,
  690. output: '(',
  691. outputIndex: state.output.length,
  692. tokensIndex: state.tokens.length
  693. };
  694. braces.push(open);
  695. push(open);
  696. continue;
  697. }
  698. if (value === '}') {
  699. const brace = braces[braces.length - 1];
  700. if (opts.nobrace === true || !brace) {
  701. push({ type: 'text', value, output: value });
  702. continue;
  703. }
  704. let output = ')';
  705. if (brace.dots === true) {
  706. const arr = tokens.slice();
  707. const range = [];
  708. for (let i = arr.length - 1; i >= 0; i--) {
  709. tokens.pop();
  710. if (arr[i].type === 'brace') {
  711. break;
  712. }
  713. if (arr[i].type !== 'dots') {
  714. range.unshift(arr[i].value);
  715. }
  716. }
  717. output = expandRange(range, opts);
  718. state.backtrack = true;
  719. }
  720. if (brace.comma !== true && brace.dots !== true) {
  721. const out = state.output.slice(0, brace.outputIndex);
  722. const toks = state.tokens.slice(brace.tokensIndex);
  723. brace.value = brace.output = '\\{';
  724. value = output = '\\}';
  725. state.output = out;
  726. for (const t of toks) {
  727. state.output += (t.output || t.value);
  728. }
  729. }
  730. push({ type: 'brace', value, output });
  731. decrement('braces');
  732. braces.pop();
  733. continue;
  734. }
  735. /**
  736. * Pipes
  737. */
  738. if (value === '|') {
  739. if (extglobs.length > 0) {
  740. extglobs[extglobs.length - 1].conditions++;
  741. }
  742. push({ type: 'text', value });
  743. continue;
  744. }
  745. /**
  746. * Commas
  747. */
  748. if (value === ',') {
  749. let output = value;
  750. const brace = braces[braces.length - 1];
  751. if (brace && stack[stack.length - 1] === 'braces') {
  752. brace.comma = true;
  753. output = '|';
  754. }
  755. push({ type: 'comma', value, output });
  756. continue;
  757. }
  758. /**
  759. * Slashes
  760. */
  761. if (value === '/') {
  762. // if the beginning of the glob is "./", advance the start
  763. // to the current index, and don't add the "./" characters
  764. // to the state. This greatly simplifies lookbehinds when
  765. // checking for BOS characters like "!" and "." (not "./")
  766. if (prev.type === 'dot' && state.index === state.start + 1) {
  767. state.start = state.index + 1;
  768. state.consumed = '';
  769. state.output = '';
  770. tokens.pop();
  771. prev = bos; // reset "prev" to the first token
  772. continue;
  773. }
  774. push({ type: 'slash', value, output: SLASH_LITERAL });
  775. continue;
  776. }
  777. /**
  778. * Dots
  779. */
  780. if (value === '.') {
  781. if (state.braces > 0 && prev.type === 'dot') {
  782. if (prev.value === '.') prev.output = DOT_LITERAL;
  783. const brace = braces[braces.length - 1];
  784. prev.type = 'dots';
  785. prev.output += value;
  786. prev.value += value;
  787. brace.dots = true;
  788. continue;
  789. }
  790. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  791. push({ type: 'text', value, output: DOT_LITERAL });
  792. continue;
  793. }
  794. push({ type: 'dot', value, output: DOT_LITERAL });
  795. continue;
  796. }
  797. /**
  798. * Question marks
  799. */
  800. if (value === '?') {
  801. const isGroup = prev && prev.value === '(';
  802. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  803. extglobOpen('qmark', value);
  804. continue;
  805. }
  806. if (prev && prev.type === 'paren') {
  807. const next = peek();
  808. let output = value;
  809. if (next === '<' && !utils.supportsLookbehinds()) {
  810. throw new Error('Node.js v10 or higher is required for regex lookbehinds');
  811. }
  812. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  813. output = `\\${value}`;
  814. }
  815. push({ type: 'text', value, output });
  816. continue;
  817. }
  818. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  819. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  820. continue;
  821. }
  822. push({ type: 'qmark', value, output: QMARK });
  823. continue;
  824. }
  825. /**
  826. * Exclamation
  827. */
  828. if (value === '!') {
  829. if (opts.noextglob !== true && peek() === '(') {
  830. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  831. extglobOpen('negate', value);
  832. continue;
  833. }
  834. }
  835. if (opts.nonegate !== true && state.index === 0) {
  836. negate();
  837. continue;
  838. }
  839. }
  840. /**
  841. * Plus
  842. */
  843. if (value === '+') {
  844. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  845. extglobOpen('plus', value);
  846. continue;
  847. }
  848. if ((prev && prev.value === '(') || opts.regex === false) {
  849. push({ type: 'plus', value, output: PLUS_LITERAL });
  850. continue;
  851. }
  852. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  853. push({ type: 'plus', value });
  854. continue;
  855. }
  856. push({ type: 'plus', value: PLUS_LITERAL });
  857. continue;
  858. }
  859. /**
  860. * Plain text
  861. */
  862. if (value === '@') {
  863. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  864. push({ type: 'at', extglob: true, value, output: '' });
  865. continue;
  866. }
  867. push({ type: 'text', value });
  868. continue;
  869. }
  870. /**
  871. * Plain text
  872. */
  873. if (value !== '*') {
  874. if (value === '$' || value === '^') {
  875. value = `\\${value}`;
  876. }
  877. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  878. if (match) {
  879. value += match[0];
  880. state.index += match[0].length;
  881. }
  882. push({ type: 'text', value });
  883. continue;
  884. }
  885. /**
  886. * Stars
  887. */
  888. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  889. prev.type = 'star';
  890. prev.star = true;
  891. prev.value += value;
  892. prev.output = star;
  893. state.backtrack = true;
  894. state.globstar = true;
  895. consume(value);
  896. continue;
  897. }
  898. let rest = remaining();
  899. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  900. extglobOpen('star', value);
  901. continue;
  902. }
  903. if (prev.type === 'star') {
  904. if (opts.noglobstar === true) {
  905. consume(value);
  906. continue;
  907. }
  908. const prior = prev.prev;
  909. const before = prior.prev;
  910. const isStart = prior.type === 'slash' || prior.type === 'bos';
  911. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  912. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  913. push({ type: 'star', value, output: '' });
  914. continue;
  915. }
  916. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  917. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  918. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  919. push({ type: 'star', value, output: '' });
  920. continue;
  921. }
  922. // strip consecutive `/**/`
  923. while (rest.slice(0, 3) === '/**') {
  924. const after = input[state.index + 4];
  925. if (after && after !== '/') {
  926. break;
  927. }
  928. rest = rest.slice(3);
  929. consume('/**', 3);
  930. }
  931. if (prior.type === 'bos' && eos()) {
  932. prev.type = 'globstar';
  933. prev.value += value;
  934. prev.output = globstar(opts);
  935. state.output = prev.output;
  936. state.globstar = true;
  937. consume(value);
  938. continue;
  939. }
  940. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  941. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  942. prior.output = `(?:${prior.output}`;
  943. prev.type = 'globstar';
  944. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  945. prev.value += value;
  946. state.globstar = true;
  947. state.output += prior.output + prev.output;
  948. consume(value);
  949. continue;
  950. }
  951. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  952. const end = rest[1] !== void 0 ? '|$' : '';
  953. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  954. prior.output = `(?:${prior.output}`;
  955. prev.type = 'globstar';
  956. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  957. prev.value += value;
  958. state.output += prior.output + prev.output;
  959. state.globstar = true;
  960. consume(value + advance());
  961. push({ type: 'slash', value: '/', output: '' });
  962. continue;
  963. }
  964. if (prior.type === 'bos' && rest[0] === '/') {
  965. prev.type = 'globstar';
  966. prev.value += value;
  967. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  968. state.output = prev.output;
  969. state.globstar = true;
  970. consume(value + advance());
  971. push({ type: 'slash', value: '/', output: '' });
  972. continue;
  973. }
  974. // remove single star from output
  975. state.output = state.output.slice(0, -prev.output.length);
  976. // reset previous token to globstar
  977. prev.type = 'globstar';
  978. prev.output = globstar(opts);
  979. prev.value += value;
  980. // reset output with globstar
  981. state.output += prev.output;
  982. state.globstar = true;
  983. consume(value);
  984. continue;
  985. }
  986. const token = { type: 'star', value, output: star };
  987. if (opts.bash === true) {
  988. token.output = '.*?';
  989. if (prev.type === 'bos' || prev.type === 'slash') {
  990. token.output = nodot + token.output;
  991. }
  992. push(token);
  993. continue;
  994. }
  995. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  996. token.output = value;
  997. push(token);
  998. continue;
  999. }
  1000. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  1001. if (prev.type === 'dot') {
  1002. state.output += NO_DOT_SLASH;
  1003. prev.output += NO_DOT_SLASH;
  1004. } else if (opts.dot === true) {
  1005. state.output += NO_DOTS_SLASH;
  1006. prev.output += NO_DOTS_SLASH;
  1007. } else {
  1008. state.output += nodot;
  1009. prev.output += nodot;
  1010. }
  1011. if (peek() !== '*') {
  1012. state.output += ONE_CHAR;
  1013. prev.output += ONE_CHAR;
  1014. }
  1015. }
  1016. push(token);
  1017. }
  1018. while (state.brackets > 0) {
  1019. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  1020. state.output = utils.escapeLast(state.output, '[');
  1021. decrement('brackets');
  1022. }
  1023. while (state.parens > 0) {
  1024. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  1025. state.output = utils.escapeLast(state.output, '(');
  1026. decrement('parens');
  1027. }
  1028. while (state.braces > 0) {
  1029. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  1030. state.output = utils.escapeLast(state.output, '{');
  1031. decrement('braces');
  1032. }
  1033. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  1034. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  1035. }
  1036. // rebuild the output if we had to backtrack at any point
  1037. if (state.backtrack === true) {
  1038. state.output = '';
  1039. for (const token of state.tokens) {
  1040. state.output += token.output != null ? token.output : token.value;
  1041. if (token.suffix) {
  1042. state.output += token.suffix;
  1043. }
  1044. }
  1045. }
  1046. return state;
  1047. };
  1048. /**
  1049. * Fast paths for creating regular expressions for common glob patterns.
  1050. * This can significantly speed up processing and has very little downside
  1051. * impact when none of the fast paths match.
  1052. */
  1053. parse.fastpaths = (input, options) => {
  1054. const opts = { ...options };
  1055. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  1056. const len = input.length;
  1057. if (len > max) {
  1058. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1059. }
  1060. input = REPLACEMENTS[input] || input;
  1061. const win32 = utils.isWindows(options);
  1062. // create constants based on platform, for windows or posix
  1063. const {
  1064. DOT_LITERAL,
  1065. SLASH_LITERAL,
  1066. ONE_CHAR,
  1067. DOTS_SLASH,
  1068. NO_DOT,
  1069. NO_DOTS,
  1070. NO_DOTS_SLASH,
  1071. STAR,
  1072. START_ANCHOR
  1073. } = constants.globChars(win32);
  1074. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  1075. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  1076. const capture = opts.capture ? '' : '?:';
  1077. const state = { negated: false, prefix: '' };
  1078. let star = opts.bash === true ? '.*?' : STAR;
  1079. if (opts.capture) {
  1080. star = `(${star})`;
  1081. }
  1082. const globstar = opts => {
  1083. if (opts.noglobstar === true) return star;
  1084. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1085. };
  1086. const create = str => {
  1087. switch (str) {
  1088. case '*':
  1089. return `${nodot}${ONE_CHAR}${star}`;
  1090. case '.*':
  1091. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  1092. case '*.*':
  1093. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1094. case '*/*':
  1095. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  1096. case '**':
  1097. return nodot + globstar(opts);
  1098. case '**/*':
  1099. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  1100. case '**/*.*':
  1101. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1102. case '**/.*':
  1103. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  1104. default: {
  1105. const match = /^(.*?)\.(\w+)$/.exec(str);
  1106. if (!match) return;
  1107. const source = create(match[1]);
  1108. if (!source) return;
  1109. return source + DOT_LITERAL + match[2];
  1110. }
  1111. }
  1112. };
  1113. const output = utils.removePrefix(input, state);
  1114. let source = create(output);
  1115. if (source && opts.strictSlashes !== true) {
  1116. source += `${SLASH_LITERAL}?`;
  1117. }
  1118. return source;
  1119. };
  1120. module.exports = parse;