parse.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. // create constants based on platform, for windows or posix
  274. const PLATFORM_CHARS = constants.globChars(opts.windows);
  275. const EXTGLOB_CHARS = constants.extglobChars(PLATFORM_CHARS);
  276. const {
  277. DOT_LITERAL,
  278. PLUS_LITERAL,
  279. SLASH_LITERAL,
  280. ONE_CHAR,
  281. DOTS_SLASH,
  282. NO_DOT,
  283. NO_DOT_SLASH,
  284. NO_DOTS_SLASH,
  285. QMARK,
  286. QMARK_NO_DOT,
  287. STAR,
  288. START_ANCHOR
  289. } = PLATFORM_CHARS;
  290. const globstar = opts => {
  291. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  292. };
  293. const nodot = opts.dot ? '' : NO_DOT;
  294. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  295. let star = opts.bash === true ? globstar(opts) : STAR;
  296. if (opts.capture) {
  297. star = `(${star})`;
  298. }
  299. // minimatch options support
  300. if (typeof opts.noext === 'boolean') {
  301. opts.noextglob = opts.noext;
  302. }
  303. const state = {
  304. input,
  305. index: -1,
  306. start: 0,
  307. dot: opts.dot === true,
  308. consumed: '',
  309. output: '',
  310. prefix: '',
  311. backtrack: false,
  312. negated: false,
  313. brackets: 0,
  314. braces: 0,
  315. parens: 0,
  316. quotes: 0,
  317. globstar: false,
  318. tokens
  319. };
  320. input = utils.removePrefix(input, state);
  321. len = input.length;
  322. const extglobs = [];
  323. const braces = [];
  324. const stack = [];
  325. let prev = bos;
  326. let value;
  327. /**
  328. * Tokenizing helpers
  329. */
  330. const eos = () => state.index === len - 1;
  331. const peek = state.peek = (n = 1) => input[state.index + n];
  332. const advance = state.advance = () => input[++state.index] || '';
  333. const remaining = () => input.slice(state.index + 1);
  334. const consume = (value = '', num = 0) => {
  335. state.consumed += value;
  336. state.index += num;
  337. };
  338. const append = token => {
  339. state.output += token.output != null ? token.output : token.value;
  340. consume(token.value);
  341. };
  342. const negate = () => {
  343. let count = 1;
  344. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  345. advance();
  346. state.start++;
  347. count++;
  348. }
  349. if (count % 2 === 0) {
  350. return false;
  351. }
  352. state.negated = true;
  353. state.start++;
  354. return true;
  355. };
  356. const increment = type => {
  357. state[type]++;
  358. stack.push(type);
  359. };
  360. const decrement = type => {
  361. state[type]--;
  362. stack.pop();
  363. };
  364. /**
  365. * Push tokens onto the tokens array. This helper speeds up
  366. * tokenizing by 1) helping us avoid backtracking as much as possible,
  367. * and 2) helping us avoid creating extra tokens when consecutive
  368. * characters are plain text. This improves performance and simplifies
  369. * lookbehinds.
  370. */
  371. const push = tok => {
  372. if (prev.type === 'globstar') {
  373. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  374. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  375. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  376. state.output = state.output.slice(0, -prev.output.length);
  377. prev.type = 'star';
  378. prev.value = '*';
  379. prev.output = star;
  380. state.output += prev.output;
  381. }
  382. }
  383. if (extglobs.length && tok.type !== 'paren') {
  384. extglobs[extglobs.length - 1].inner += tok.value;
  385. }
  386. if (tok.value || tok.output) append(tok);
  387. if (prev && prev.type === 'text' && tok.type === 'text') {
  388. prev.output = (prev.output || prev.value) + tok.value;
  389. prev.value += tok.value;
  390. return;
  391. }
  392. tok.prev = prev;
  393. tokens.push(tok);
  394. prev = tok;
  395. };
  396. const extglobOpen = (type, value) => {
  397. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  398. token.prev = prev;
  399. token.parens = state.parens;
  400. token.output = state.output;
  401. token.startIndex = state.index;
  402. token.tokensIndex = tokens.length;
  403. const output = (opts.capture ? '(' : '') + token.open;
  404. increment('parens');
  405. push({ type, value, output: state.output ? '' : ONE_CHAR });
  406. push({ type: 'paren', extglob: true, value: advance(), output });
  407. extglobs.push(token);
  408. };
  409. const extglobClose = token => {
  410. const literal = input.slice(token.startIndex, state.index + 1);
  411. const body = input.slice(token.startIndex + 2, state.index);
  412. const analysis = analyzeRepeatedExtglob(body, opts);
  413. if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
  414. const safeOutput = analysis.safeOutput
  415. ? (token.output ? '' : ONE_CHAR) + (opts.capture ? `(${analysis.safeOutput})` : analysis.safeOutput)
  416. : undefined;
  417. const open = tokens[token.tokensIndex];
  418. open.type = 'text';
  419. open.value = literal;
  420. open.output = safeOutput || utils.escapeRegex(literal);
  421. for (let i = token.tokensIndex + 1; i < tokens.length; i++) {
  422. tokens[i].value = '';
  423. tokens[i].output = '';
  424. delete tokens[i].suffix;
  425. }
  426. state.output = token.output + open.output;
  427. state.backtrack = true;
  428. push({ type: 'paren', extglob: true, value, output: '' });
  429. decrement('parens');
  430. return;
  431. }
  432. let output = token.close + (opts.capture ? ')' : '');
  433. let rest;
  434. if (token.type === 'negate') {
  435. let extglobStar = star;
  436. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  437. extglobStar = globstar(opts);
  438. }
  439. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  440. output = token.close = `)$))${extglobStar}`;
  441. }
  442. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  443. // Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
  444. // In this case, we need to parse the string and use it in the output of the original pattern.
  445. // Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
  446. //
  447. // Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
  448. const expression = parse(rest, { ...options, fastpaths: false }).output;
  449. output = token.close = `)${expression})${extglobStar})`;
  450. }
  451. if (token.prev.type === 'bos') {
  452. state.negatedExtglob = true;
  453. }
  454. }
  455. push({ type: 'paren', extglob: true, value, output });
  456. decrement('parens');
  457. };
  458. /**
  459. * Fast paths
  460. */
  461. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  462. let backslashes = false;
  463. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  464. if (first === '\\') {
  465. backslashes = true;
  466. return m;
  467. }
  468. if (first === '?') {
  469. if (esc) {
  470. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  471. }
  472. if (index === 0) {
  473. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  474. }
  475. return QMARK.repeat(chars.length);
  476. }
  477. if (first === '.') {
  478. return DOT_LITERAL.repeat(chars.length);
  479. }
  480. if (first === '*') {
  481. if (esc) {
  482. return esc + first + (rest ? star : '');
  483. }
  484. return star;
  485. }
  486. return esc ? m : `\\${m}`;
  487. });
  488. if (backslashes === true) {
  489. if (opts.unescape === true) {
  490. output = output.replace(/\\/g, '');
  491. } else {
  492. output = output.replace(/\\+/g, m => {
  493. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  494. });
  495. }
  496. }
  497. if (output === input && opts.contains === true) {
  498. state.output = input;
  499. return state;
  500. }
  501. state.output = utils.wrapOutput(output, state, options);
  502. return state;
  503. }
  504. /**
  505. * Tokenize input until we reach end-of-string
  506. */
  507. while (!eos()) {
  508. value = advance();
  509. if (value === '\u0000') {
  510. continue;
  511. }
  512. /**
  513. * Escaped characters
  514. */
  515. if (value === '\\') {
  516. const next = peek();
  517. if (next === '/' && opts.bash !== true) {
  518. continue;
  519. }
  520. if (next === '.' || next === ';') {
  521. continue;
  522. }
  523. if (!next) {
  524. value += '\\';
  525. push({ type: 'text', value });
  526. continue;
  527. }
  528. // collapse slashes to reduce potential for exploits
  529. const match = /^\\+/.exec(remaining());
  530. let slashes = 0;
  531. if (match && match[0].length > 2) {
  532. slashes = match[0].length;
  533. state.index += slashes;
  534. if (slashes % 2 !== 0) {
  535. value += '\\';
  536. }
  537. }
  538. if (opts.unescape === true) {
  539. value = advance();
  540. } else {
  541. value += advance();
  542. }
  543. if (state.brackets === 0) {
  544. push({ type: 'text', value });
  545. continue;
  546. }
  547. }
  548. /**
  549. * If we're inside a regex character class, continue
  550. * until we reach the closing bracket.
  551. */
  552. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  553. if (opts.posix !== false && value === ':') {
  554. const inner = prev.value.slice(1);
  555. if (inner.includes('[')) {
  556. prev.posix = true;
  557. if (inner.includes(':')) {
  558. const idx = prev.value.lastIndexOf('[');
  559. const pre = prev.value.slice(0, idx);
  560. const rest = prev.value.slice(idx + 2);
  561. const posix = POSIX_REGEX_SOURCE[rest];
  562. if (posix) {
  563. prev.value = pre + posix;
  564. state.backtrack = true;
  565. advance();
  566. if (!bos.output && tokens.indexOf(prev) === 1) {
  567. bos.output = ONE_CHAR;
  568. }
  569. continue;
  570. }
  571. }
  572. }
  573. }
  574. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  575. value = `\\${value}`;
  576. }
  577. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  578. value = `\\${value}`;
  579. }
  580. if (opts.posix === true && value === '!' && prev.value === '[') {
  581. value = '^';
  582. }
  583. prev.value += value;
  584. append({ value });
  585. continue;
  586. }
  587. /**
  588. * If we're inside a quoted string, continue
  589. * until we reach the closing double quote.
  590. */
  591. if (state.quotes === 1 && value !== '"') {
  592. value = utils.escapeRegex(value);
  593. prev.value += value;
  594. append({ value });
  595. continue;
  596. }
  597. /**
  598. * Double quotes
  599. */
  600. if (value === '"') {
  601. state.quotes = state.quotes === 1 ? 0 : 1;
  602. if (opts.keepQuotes === true) {
  603. push({ type: 'text', value });
  604. }
  605. continue;
  606. }
  607. /**
  608. * Parentheses
  609. */
  610. if (value === '(') {
  611. increment('parens');
  612. push({ type: 'paren', value });
  613. continue;
  614. }
  615. if (value === ')') {
  616. if (state.parens === 0 && opts.strictBrackets === true) {
  617. throw new SyntaxError(syntaxError('opening', '('));
  618. }
  619. const extglob = extglobs[extglobs.length - 1];
  620. if (extglob && state.parens === extglob.parens + 1) {
  621. extglobClose(extglobs.pop());
  622. continue;
  623. }
  624. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  625. decrement('parens');
  626. continue;
  627. }
  628. /**
  629. * Square brackets
  630. */
  631. if (value === '[') {
  632. if (opts.nobracket === true || !remaining().includes(']')) {
  633. if (opts.nobracket !== true && opts.strictBrackets === true) {
  634. throw new SyntaxError(syntaxError('closing', ']'));
  635. }
  636. value = `\\${value}`;
  637. } else {
  638. increment('brackets');
  639. }
  640. push({ type: 'bracket', value });
  641. continue;
  642. }
  643. if (value === ']') {
  644. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  645. push({ type: 'text', value, output: `\\${value}` });
  646. continue;
  647. }
  648. if (state.brackets === 0) {
  649. if (opts.strictBrackets === true) {
  650. throw new SyntaxError(syntaxError('opening', '['));
  651. }
  652. push({ type: 'text', value, output: `\\${value}` });
  653. continue;
  654. }
  655. decrement('brackets');
  656. const prevValue = prev.value.slice(1);
  657. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  658. value = `/${value}`;
  659. }
  660. prev.value += value;
  661. append({ value });
  662. // when literal brackets are explicitly disabled
  663. // assume we should match with a regex character class
  664. if (opts.literalBrackets === false || utils.hasRegexChars(prevValue)) {
  665. continue;
  666. }
  667. const escaped = utils.escapeRegex(prev.value);
  668. state.output = state.output.slice(0, -prev.value.length);
  669. // when literal brackets are explicitly enabled
  670. // assume we should escape the brackets to match literal characters
  671. if (opts.literalBrackets === true) {
  672. state.output += escaped;
  673. prev.value = escaped;
  674. continue;
  675. }
  676. // when the user specifies nothing, try to match both
  677. prev.value = `(${capture}${escaped}|${prev.value})`;
  678. state.output += prev.value;
  679. continue;
  680. }
  681. /**
  682. * Braces
  683. */
  684. if (value === '{' && opts.nobrace !== true) {
  685. increment('braces');
  686. const open = {
  687. type: 'brace',
  688. value,
  689. output: '(',
  690. outputIndex: state.output.length,
  691. tokensIndex: state.tokens.length
  692. };
  693. braces.push(open);
  694. push(open);
  695. continue;
  696. }
  697. if (value === '}') {
  698. const brace = braces[braces.length - 1];
  699. if (opts.nobrace === true || !brace) {
  700. push({ type: 'text', value, output: value });
  701. continue;
  702. }
  703. let output = ')';
  704. if (brace.dots === true) {
  705. const arr = tokens.slice();
  706. const range = [];
  707. for (let i = arr.length - 1; i >= 0; i--) {
  708. tokens.pop();
  709. if (arr[i].type === 'brace') {
  710. break;
  711. }
  712. if (arr[i].type !== 'dots') {
  713. range.unshift(arr[i].value);
  714. }
  715. }
  716. output = expandRange(range, opts);
  717. state.backtrack = true;
  718. }
  719. if (brace.comma !== true && brace.dots !== true) {
  720. const out = state.output.slice(0, brace.outputIndex);
  721. const toks = state.tokens.slice(brace.tokensIndex);
  722. brace.value = brace.output = '\\{';
  723. value = output = '\\}';
  724. state.output = out;
  725. for (const t of toks) {
  726. state.output += (t.output || t.value);
  727. }
  728. }
  729. push({ type: 'brace', value, output });
  730. decrement('braces');
  731. braces.pop();
  732. continue;
  733. }
  734. /**
  735. * Pipes
  736. */
  737. if (value === '|') {
  738. if (extglobs.length > 0) {
  739. extglobs[extglobs.length - 1].conditions++;
  740. }
  741. push({ type: 'text', value });
  742. continue;
  743. }
  744. /**
  745. * Commas
  746. */
  747. if (value === ',') {
  748. let output = value;
  749. const brace = braces[braces.length - 1];
  750. if (brace && stack[stack.length - 1] === 'braces') {
  751. brace.comma = true;
  752. output = '|';
  753. }
  754. push({ type: 'comma', value, output });
  755. continue;
  756. }
  757. /**
  758. * Slashes
  759. */
  760. if (value === '/') {
  761. // if the beginning of the glob is "./", advance the start
  762. // to the current index, and don't add the "./" characters
  763. // to the state. This greatly simplifies lookbehinds when
  764. // checking for BOS characters like "!" and "." (not "./")
  765. if (prev.type === 'dot' && state.index === state.start + 1) {
  766. state.start = state.index + 1;
  767. state.consumed = '';
  768. state.output = '';
  769. tokens.pop();
  770. prev = bos; // reset "prev" to the first token
  771. continue;
  772. }
  773. push({ type: 'slash', value, output: SLASH_LITERAL });
  774. continue;
  775. }
  776. /**
  777. * Dots
  778. */
  779. if (value === '.') {
  780. if (state.braces > 0 && prev.type === 'dot') {
  781. if (prev.value === '.') prev.output = DOT_LITERAL;
  782. const brace = braces[braces.length - 1];
  783. prev.type = 'dots';
  784. prev.output += value;
  785. prev.value += value;
  786. brace.dots = true;
  787. continue;
  788. }
  789. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  790. push({ type: 'text', value, output: DOT_LITERAL });
  791. continue;
  792. }
  793. push({ type: 'dot', value, output: DOT_LITERAL });
  794. continue;
  795. }
  796. /**
  797. * Question marks
  798. */
  799. if (value === '?') {
  800. const isGroup = prev && prev.value === '(';
  801. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  802. extglobOpen('qmark', value);
  803. continue;
  804. }
  805. if (prev && prev.type === 'paren') {
  806. const next = peek();
  807. let output = value;
  808. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  809. output = `\\${value}`;
  810. }
  811. push({ type: 'text', value, output });
  812. continue;
  813. }
  814. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  815. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  816. continue;
  817. }
  818. push({ type: 'qmark', value, output: QMARK });
  819. continue;
  820. }
  821. /**
  822. * Exclamation
  823. */
  824. if (value === '!') {
  825. if (opts.noextglob !== true && peek() === '(') {
  826. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  827. extglobOpen('negate', value);
  828. continue;
  829. }
  830. }
  831. if (opts.nonegate !== true && state.index === 0) {
  832. negate();
  833. continue;
  834. }
  835. }
  836. /**
  837. * Plus
  838. */
  839. if (value === '+') {
  840. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  841. extglobOpen('plus', value);
  842. continue;
  843. }
  844. if ((prev && prev.value === '(') || opts.regex === false) {
  845. push({ type: 'plus', value, output: PLUS_LITERAL });
  846. continue;
  847. }
  848. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  849. push({ type: 'plus', value });
  850. continue;
  851. }
  852. push({ type: 'plus', value: PLUS_LITERAL });
  853. continue;
  854. }
  855. /**
  856. * Plain text
  857. */
  858. if (value === '@') {
  859. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  860. push({ type: 'at', extglob: true, value, output: '' });
  861. continue;
  862. }
  863. push({ type: 'text', value });
  864. continue;
  865. }
  866. /**
  867. * Plain text
  868. */
  869. if (value !== '*') {
  870. if (value === '$' || value === '^') {
  871. value = `\\${value}`;
  872. }
  873. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  874. if (match) {
  875. value += match[0];
  876. state.index += match[0].length;
  877. }
  878. push({ type: 'text', value });
  879. continue;
  880. }
  881. /**
  882. * Stars
  883. */
  884. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  885. prev.type = 'star';
  886. prev.star = true;
  887. prev.value += value;
  888. prev.output = star;
  889. state.backtrack = true;
  890. state.globstar = true;
  891. consume(value);
  892. continue;
  893. }
  894. let rest = remaining();
  895. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  896. extglobOpen('star', value);
  897. continue;
  898. }
  899. if (prev.type === 'star') {
  900. if (opts.noglobstar === true) {
  901. consume(value);
  902. continue;
  903. }
  904. const prior = prev.prev;
  905. const before = prior.prev;
  906. const isStart = prior.type === 'slash' || prior.type === 'bos';
  907. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  908. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  909. push({ type: 'star', value, output: '' });
  910. continue;
  911. }
  912. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  913. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  914. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  915. push({ type: 'star', value, output: '' });
  916. continue;
  917. }
  918. // strip consecutive `/**/`
  919. while (rest.slice(0, 3) === '/**') {
  920. const after = input[state.index + 4];
  921. if (after && after !== '/') {
  922. break;
  923. }
  924. rest = rest.slice(3);
  925. consume('/**', 3);
  926. }
  927. if (prior.type === 'bos' && eos()) {
  928. prev.type = 'globstar';
  929. prev.value += value;
  930. prev.output = globstar(opts);
  931. state.output = prev.output;
  932. state.globstar = true;
  933. consume(value);
  934. continue;
  935. }
  936. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  937. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  938. prior.output = `(?:${prior.output}`;
  939. prev.type = 'globstar';
  940. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  941. prev.value += value;
  942. state.globstar = true;
  943. state.output += prior.output + prev.output;
  944. consume(value);
  945. continue;
  946. }
  947. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  948. const end = rest[1] !== void 0 ? '|$' : '';
  949. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  950. prior.output = `(?:${prior.output}`;
  951. prev.type = 'globstar';
  952. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  953. prev.value += value;
  954. state.output += prior.output + prev.output;
  955. state.globstar = true;
  956. consume(value + advance());
  957. push({ type: 'slash', value: '/', output: '' });
  958. continue;
  959. }
  960. if (prior.type === 'bos' && rest[0] === '/') {
  961. prev.type = 'globstar';
  962. prev.value += value;
  963. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  964. state.output = prev.output;
  965. state.globstar = true;
  966. consume(value + advance());
  967. push({ type: 'slash', value: '/', output: '' });
  968. continue;
  969. }
  970. // remove single star from output
  971. state.output = state.output.slice(0, -prev.output.length);
  972. // reset previous token to globstar
  973. prev.type = 'globstar';
  974. prev.output = globstar(opts);
  975. prev.value += value;
  976. // reset output with globstar
  977. state.output += prev.output;
  978. state.globstar = true;
  979. consume(value);
  980. continue;
  981. }
  982. const token = { type: 'star', value, output: star };
  983. if (opts.bash === true) {
  984. token.output = '.*?';
  985. if (prev.type === 'bos' || prev.type === 'slash') {
  986. token.output = nodot + token.output;
  987. }
  988. push(token);
  989. continue;
  990. }
  991. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  992. token.output = value;
  993. push(token);
  994. continue;
  995. }
  996. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  997. if (prev.type === 'dot') {
  998. state.output += NO_DOT_SLASH;
  999. prev.output += NO_DOT_SLASH;
  1000. } else if (opts.dot === true) {
  1001. state.output += NO_DOTS_SLASH;
  1002. prev.output += NO_DOTS_SLASH;
  1003. } else {
  1004. state.output += nodot;
  1005. prev.output += nodot;
  1006. }
  1007. if (peek() !== '*') {
  1008. state.output += ONE_CHAR;
  1009. prev.output += ONE_CHAR;
  1010. }
  1011. }
  1012. push(token);
  1013. }
  1014. while (state.brackets > 0) {
  1015. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  1016. state.output = utils.escapeLast(state.output, '[');
  1017. decrement('brackets');
  1018. }
  1019. while (state.parens > 0) {
  1020. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  1021. state.output = utils.escapeLast(state.output, '(');
  1022. decrement('parens');
  1023. }
  1024. while (state.braces > 0) {
  1025. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  1026. state.output = utils.escapeLast(state.output, '{');
  1027. decrement('braces');
  1028. }
  1029. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  1030. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  1031. }
  1032. // rebuild the output if we had to backtrack at any point
  1033. if (state.backtrack === true) {
  1034. state.output = '';
  1035. for (const token of state.tokens) {
  1036. state.output += token.output != null ? token.output : token.value;
  1037. if (token.suffix) {
  1038. state.output += token.suffix;
  1039. }
  1040. }
  1041. }
  1042. return state;
  1043. };
  1044. /**
  1045. * Fast paths for creating regular expressions for common glob patterns.
  1046. * This can significantly speed up processing and has very little downside
  1047. * impact when none of the fast paths match.
  1048. */
  1049. parse.fastpaths = (input, options) => {
  1050. const opts = { ...options };
  1051. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  1052. const len = input.length;
  1053. if (len > max) {
  1054. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1055. }
  1056. input = REPLACEMENTS[input] || input;
  1057. // create constants based on platform, for windows or posix
  1058. const {
  1059. DOT_LITERAL,
  1060. SLASH_LITERAL,
  1061. ONE_CHAR,
  1062. DOTS_SLASH,
  1063. NO_DOT,
  1064. NO_DOTS,
  1065. NO_DOTS_SLASH,
  1066. STAR,
  1067. START_ANCHOR
  1068. } = constants.globChars(opts.windows);
  1069. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  1070. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  1071. const capture = opts.capture ? '' : '?:';
  1072. const state = { negated: false, prefix: '' };
  1073. let star = opts.bash === true ? '.*?' : STAR;
  1074. if (opts.capture) {
  1075. star = `(${star})`;
  1076. }
  1077. const globstar = opts => {
  1078. if (opts.noglobstar === true) return star;
  1079. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1080. };
  1081. const create = str => {
  1082. switch (str) {
  1083. case '*':
  1084. return `${nodot}${ONE_CHAR}${star}`;
  1085. case '.*':
  1086. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  1087. case '*.*':
  1088. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1089. case '*/*':
  1090. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  1091. case '**':
  1092. return nodot + globstar(opts);
  1093. case '**/*':
  1094. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  1095. case '**/*.*':
  1096. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1097. case '**/.*':
  1098. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  1099. default: {
  1100. const match = /^(.*?)\.(\w+)$/.exec(str);
  1101. if (!match) return;
  1102. const source = create(match[1]);
  1103. if (!source) return;
  1104. return source + DOT_LITERAL + match[2];
  1105. }
  1106. }
  1107. };
  1108. const output = utils.removePrefix(input, state);
  1109. let source = create(output);
  1110. if (source && opts.strictSlashes !== true) {
  1111. source += `${SLASH_LITERAL}?`;
  1112. }
  1113. return source;
  1114. };
  1115. module.exports = parse;