parse.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. /**
  38. * Parse the given input string.
  39. * @param {String} input
  40. * @param {Object} options
  41. * @return {Object}
  42. */
  43. const parse = (input, options) => {
  44. if (typeof input !== 'string') {
  45. throw new TypeError('Expected a string');
  46. }
  47. input = REPLACEMENTS[input] || input;
  48. const opts = { ...options };
  49. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  50. let len = input.length;
  51. if (len > max) {
  52. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  53. }
  54. const bos = { type: 'bos', value: '', output: opts.prepend || '' };
  55. const tokens = [bos];
  56. const capture = opts.capture ? '' : '?:';
  57. const win32 = utils.isWindows(options);
  58. // create constants based on platform, for windows or posix
  59. const PLATFORM_CHARS = constants.globChars(win32);
  60. const EXTGLOB_CHARS = constants.extglobChars(PLATFORM_CHARS);
  61. const {
  62. DOT_LITERAL,
  63. PLUS_LITERAL,
  64. SLASH_LITERAL,
  65. ONE_CHAR,
  66. DOTS_SLASH,
  67. NO_DOT,
  68. NO_DOT_SLASH,
  69. NO_DOTS_SLASH,
  70. QMARK,
  71. QMARK_NO_DOT,
  72. STAR,
  73. START_ANCHOR
  74. } = PLATFORM_CHARS;
  75. const globstar = opts => {
  76. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  77. };
  78. const nodot = opts.dot ? '' : NO_DOT;
  79. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  80. let star = opts.bash === true ? globstar(opts) : STAR;
  81. if (opts.capture) {
  82. star = `(${star})`;
  83. }
  84. // minimatch options support
  85. if (typeof opts.noext === 'boolean') {
  86. opts.noextglob = opts.noext;
  87. }
  88. const state = {
  89. input,
  90. index: -1,
  91. start: 0,
  92. dot: opts.dot === true,
  93. consumed: '',
  94. output: '',
  95. prefix: '',
  96. backtrack: false,
  97. negated: false,
  98. brackets: 0,
  99. braces: 0,
  100. parens: 0,
  101. quotes: 0,
  102. globstar: false,
  103. tokens
  104. };
  105. input = utils.removePrefix(input, state);
  106. len = input.length;
  107. const extglobs = [];
  108. const braces = [];
  109. const stack = [];
  110. let prev = bos;
  111. let value;
  112. /**
  113. * Tokenizing helpers
  114. */
  115. const eos = () => state.index === len - 1;
  116. const peek = state.peek = (n = 1) => input[state.index + n];
  117. const advance = state.advance = () => input[++state.index] || '';
  118. const remaining = () => input.slice(state.index + 1);
  119. const consume = (value = '', num = 0) => {
  120. state.consumed += value;
  121. state.index += num;
  122. };
  123. const append = token => {
  124. state.output += token.output != null ? token.output : token.value;
  125. consume(token.value);
  126. };
  127. const negate = () => {
  128. let count = 1;
  129. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  130. advance();
  131. state.start++;
  132. count++;
  133. }
  134. if (count % 2 === 0) {
  135. return false;
  136. }
  137. state.negated = true;
  138. state.start++;
  139. return true;
  140. };
  141. const increment = type => {
  142. state[type]++;
  143. stack.push(type);
  144. };
  145. const decrement = type => {
  146. state[type]--;
  147. stack.pop();
  148. };
  149. /**
  150. * Push tokens onto the tokens array. This helper speeds up
  151. * tokenizing by 1) helping us avoid backtracking as much as possible,
  152. * and 2) helping us avoid creating extra tokens when consecutive
  153. * characters are plain text. This improves performance and simplifies
  154. * lookbehinds.
  155. */
  156. const push = tok => {
  157. if (prev.type === 'globstar') {
  158. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  159. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  160. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  161. state.output = state.output.slice(0, -prev.output.length);
  162. prev.type = 'star';
  163. prev.value = '*';
  164. prev.output = star;
  165. state.output += prev.output;
  166. }
  167. }
  168. if (extglobs.length && tok.type !== 'paren') {
  169. extglobs[extglobs.length - 1].inner += tok.value;
  170. }
  171. if (tok.value || tok.output) append(tok);
  172. if (prev && prev.type === 'text' && tok.type === 'text') {
  173. prev.value += tok.value;
  174. prev.output = (prev.output || '') + tok.value;
  175. return;
  176. }
  177. tok.prev = prev;
  178. tokens.push(tok);
  179. prev = tok;
  180. };
  181. const extglobOpen = (type, value) => {
  182. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  183. token.prev = prev;
  184. token.parens = state.parens;
  185. token.output = state.output;
  186. const output = (opts.capture ? '(' : '') + token.open;
  187. increment('parens');
  188. push({ type, value, output: state.output ? '' : ONE_CHAR });
  189. push({ type: 'paren', extglob: true, value: advance(), output });
  190. extglobs.push(token);
  191. };
  192. const extglobClose = token => {
  193. let output = token.close + (opts.capture ? ')' : '');
  194. let rest;
  195. if (token.type === 'negate') {
  196. let extglobStar = star;
  197. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  198. extglobStar = globstar(opts);
  199. }
  200. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  201. output = token.close = `)$))${extglobStar}`;
  202. }
  203. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  204. output = token.close = `)${rest})${extglobStar})`;
  205. }
  206. if (token.prev.type === 'bos') {
  207. state.negatedExtglob = true;
  208. }
  209. }
  210. push({ type: 'paren', extglob: true, value, output });
  211. decrement('parens');
  212. };
  213. /**
  214. * Fast paths
  215. */
  216. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  217. let backslashes = false;
  218. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  219. if (first === '\\') {
  220. backslashes = true;
  221. return m;
  222. }
  223. if (first === '?') {
  224. if (esc) {
  225. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  226. }
  227. if (index === 0) {
  228. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  229. }
  230. return QMARK.repeat(chars.length);
  231. }
  232. if (first === '.') {
  233. return DOT_LITERAL.repeat(chars.length);
  234. }
  235. if (first === '*') {
  236. if (esc) {
  237. return esc + first + (rest ? star : '');
  238. }
  239. return star;
  240. }
  241. return esc ? m : `\\${m}`;
  242. });
  243. if (backslashes === true) {
  244. if (opts.unescape === true) {
  245. output = output.replace(/\\/g, '');
  246. } else {
  247. output = output.replace(/\\+/g, m => {
  248. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  249. });
  250. }
  251. }
  252. if (output === input && opts.contains === true) {
  253. state.output = input;
  254. return state;
  255. }
  256. state.output = utils.wrapOutput(output, state, options);
  257. return state;
  258. }
  259. /**
  260. * Tokenize input until we reach end-of-string
  261. */
  262. while (!eos()) {
  263. value = advance();
  264. if (value === '\u0000') {
  265. continue;
  266. }
  267. /**
  268. * Escaped characters
  269. */
  270. if (value === '\\') {
  271. const next = peek();
  272. if (next === '/' && opts.bash !== true) {
  273. continue;
  274. }
  275. if (next === '.' || next === ';') {
  276. continue;
  277. }
  278. if (!next) {
  279. value += '\\';
  280. push({ type: 'text', value });
  281. continue;
  282. }
  283. // collapse slashes to reduce potential for exploits
  284. const match = /^\\+/.exec(remaining());
  285. let slashes = 0;
  286. if (match && match[0].length > 2) {
  287. slashes = match[0].length;
  288. state.index += slashes;
  289. if (slashes % 2 !== 0) {
  290. value += '\\';
  291. }
  292. }
  293. if (opts.unescape === true) {
  294. value = advance();
  295. } else {
  296. value += advance();
  297. }
  298. if (state.brackets === 0) {
  299. push({ type: 'text', value });
  300. continue;
  301. }
  302. }
  303. /**
  304. * If we're inside a regex character class, continue
  305. * until we reach the closing bracket.
  306. */
  307. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  308. if (opts.posix !== false && value === ':') {
  309. const inner = prev.value.slice(1);
  310. if (inner.includes('[')) {
  311. prev.posix = true;
  312. if (inner.includes(':')) {
  313. const idx = prev.value.lastIndexOf('[');
  314. const pre = prev.value.slice(0, idx);
  315. const rest = prev.value.slice(idx + 2);
  316. const posix = POSIX_REGEX_SOURCE[rest];
  317. if (posix) {
  318. prev.value = pre + posix;
  319. state.backtrack = true;
  320. advance();
  321. if (!bos.output && tokens.indexOf(prev) === 1) {
  322. bos.output = ONE_CHAR;
  323. }
  324. continue;
  325. }
  326. }
  327. }
  328. }
  329. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  330. value = `\\${value}`;
  331. }
  332. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  333. value = `\\${value}`;
  334. }
  335. if (opts.posix === true && value === '!' && prev.value === '[') {
  336. value = '^';
  337. }
  338. prev.value += value;
  339. append({ value });
  340. continue;
  341. }
  342. /**
  343. * If we're inside a quoted string, continue
  344. * until we reach the closing double quote.
  345. */
  346. if (state.quotes === 1 && value !== '"') {
  347. value = utils.escapeRegex(value);
  348. prev.value += value;
  349. append({ value });
  350. continue;
  351. }
  352. /**
  353. * Double quotes
  354. */
  355. if (value === '"') {
  356. state.quotes = state.quotes === 1 ? 0 : 1;
  357. if (opts.keepQuotes === true) {
  358. push({ type: 'text', value });
  359. }
  360. continue;
  361. }
  362. /**
  363. * Parentheses
  364. */
  365. if (value === '(') {
  366. increment('parens');
  367. push({ type: 'paren', value });
  368. continue;
  369. }
  370. if (value === ')') {
  371. if (state.parens === 0 && opts.strictBrackets === true) {
  372. throw new SyntaxError(syntaxError('opening', '('));
  373. }
  374. const extglob = extglobs[extglobs.length - 1];
  375. if (extglob && state.parens === extglob.parens + 1) {
  376. extglobClose(extglobs.pop());
  377. continue;
  378. }
  379. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  380. decrement('parens');
  381. continue;
  382. }
  383. /**
  384. * Square brackets
  385. */
  386. if (value === '[') {
  387. if (opts.nobracket === true || !remaining().includes(']')) {
  388. if (opts.nobracket !== true && opts.strictBrackets === true) {
  389. throw new SyntaxError(syntaxError('closing', ']'));
  390. }
  391. value = `\\${value}`;
  392. } else {
  393. increment('brackets');
  394. }
  395. push({ type: 'bracket', value });
  396. continue;
  397. }
  398. if (value === ']') {
  399. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  400. push({ type: 'text', value, output: `\\${value}` });
  401. continue;
  402. }
  403. if (state.brackets === 0) {
  404. if (opts.strictBrackets === true) {
  405. throw new SyntaxError(syntaxError('opening', '['));
  406. }
  407. push({ type: 'text', value, output: `\\${value}` });
  408. continue;
  409. }
  410. decrement('brackets');
  411. const prevValue = prev.value.slice(1);
  412. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  413. value = `/${value}`;
  414. }
  415. prev.value += value;
  416. append({ value });
  417. // when literal brackets are explicitly disabled
  418. // assume we should match with a regex character class
  419. if (opts.literalBrackets === false || utils.hasRegexChars(prevValue)) {
  420. continue;
  421. }
  422. const escaped = utils.escapeRegex(prev.value);
  423. state.output = state.output.slice(0, -prev.value.length);
  424. // when literal brackets are explicitly enabled
  425. // assume we should escape the brackets to match literal characters
  426. if (opts.literalBrackets === true) {
  427. state.output += escaped;
  428. prev.value = escaped;
  429. continue;
  430. }
  431. // when the user specifies nothing, try to match both
  432. prev.value = `(${capture}${escaped}|${prev.value})`;
  433. state.output += prev.value;
  434. continue;
  435. }
  436. /**
  437. * Braces
  438. */
  439. if (value === '{' && opts.nobrace !== true) {
  440. increment('braces');
  441. const open = {
  442. type: 'brace',
  443. value,
  444. output: '(',
  445. outputIndex: state.output.length,
  446. tokensIndex: state.tokens.length
  447. };
  448. braces.push(open);
  449. push(open);
  450. continue;
  451. }
  452. if (value === '}') {
  453. const brace = braces[braces.length - 1];
  454. if (opts.nobrace === true || !brace) {
  455. push({ type: 'text', value, output: value });
  456. continue;
  457. }
  458. let output = ')';
  459. if (brace.dots === true) {
  460. const arr = tokens.slice();
  461. const range = [];
  462. for (let i = arr.length - 1; i >= 0; i--) {
  463. tokens.pop();
  464. if (arr[i].type === 'brace') {
  465. break;
  466. }
  467. if (arr[i].type !== 'dots') {
  468. range.unshift(arr[i].value);
  469. }
  470. }
  471. output = expandRange(range, opts);
  472. state.backtrack = true;
  473. }
  474. if (brace.comma !== true && brace.dots !== true) {
  475. const out = state.output.slice(0, brace.outputIndex);
  476. const toks = state.tokens.slice(brace.tokensIndex);
  477. brace.value = brace.output = '\\{';
  478. value = output = '\\}';
  479. state.output = out;
  480. for (const t of toks) {
  481. state.output += (t.output || t.value);
  482. }
  483. }
  484. push({ type: 'brace', value, output });
  485. decrement('braces');
  486. braces.pop();
  487. continue;
  488. }
  489. /**
  490. * Pipes
  491. */
  492. if (value === '|') {
  493. if (extglobs.length > 0) {
  494. extglobs[extglobs.length - 1].conditions++;
  495. }
  496. push({ type: 'text', value });
  497. continue;
  498. }
  499. /**
  500. * Commas
  501. */
  502. if (value === ',') {
  503. let output = value;
  504. const brace = braces[braces.length - 1];
  505. if (brace && stack[stack.length - 1] === 'braces') {
  506. brace.comma = true;
  507. output = '|';
  508. }
  509. push({ type: 'comma', value, output });
  510. continue;
  511. }
  512. /**
  513. * Slashes
  514. */
  515. if (value === '/') {
  516. // if the beginning of the glob is "./", advance the start
  517. // to the current index, and don't add the "./" characters
  518. // to the state. This greatly simplifies lookbehinds when
  519. // checking for BOS characters like "!" and "." (not "./")
  520. if (prev.type === 'dot' && state.index === state.start + 1) {
  521. state.start = state.index + 1;
  522. state.consumed = '';
  523. state.output = '';
  524. tokens.pop();
  525. prev = bos; // reset "prev" to the first token
  526. continue;
  527. }
  528. push({ type: 'slash', value, output: SLASH_LITERAL });
  529. continue;
  530. }
  531. /**
  532. * Dots
  533. */
  534. if (value === '.') {
  535. if (state.braces > 0 && prev.type === 'dot') {
  536. if (prev.value === '.') prev.output = DOT_LITERAL;
  537. const brace = braces[braces.length - 1];
  538. prev.type = 'dots';
  539. prev.output += value;
  540. prev.value += value;
  541. brace.dots = true;
  542. continue;
  543. }
  544. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  545. push({ type: 'text', value, output: DOT_LITERAL });
  546. continue;
  547. }
  548. push({ type: 'dot', value, output: DOT_LITERAL });
  549. continue;
  550. }
  551. /**
  552. * Question marks
  553. */
  554. if (value === '?') {
  555. const isGroup = prev && prev.value === '(';
  556. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  557. extglobOpen('qmark', value);
  558. continue;
  559. }
  560. if (prev && prev.type === 'paren') {
  561. const next = peek();
  562. let output = value;
  563. if (next === '<' && !utils.supportsLookbehinds()) {
  564. throw new Error('Node.js v10 or higher is required for regex lookbehinds');
  565. }
  566. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  567. output = `\\${value}`;
  568. }
  569. push({ type: 'text', value, output });
  570. continue;
  571. }
  572. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  573. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  574. continue;
  575. }
  576. push({ type: 'qmark', value, output: QMARK });
  577. continue;
  578. }
  579. /**
  580. * Exclamation
  581. */
  582. if (value === '!') {
  583. if (opts.noextglob !== true && peek() === '(') {
  584. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  585. extglobOpen('negate', value);
  586. continue;
  587. }
  588. }
  589. if (opts.nonegate !== true && state.index === 0) {
  590. negate();
  591. continue;
  592. }
  593. }
  594. /**
  595. * Plus
  596. */
  597. if (value === '+') {
  598. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  599. extglobOpen('plus', value);
  600. continue;
  601. }
  602. if ((prev && prev.value === '(') || opts.regex === false) {
  603. push({ type: 'plus', value, output: PLUS_LITERAL });
  604. continue;
  605. }
  606. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  607. push({ type: 'plus', value });
  608. continue;
  609. }
  610. push({ type: 'plus', value: PLUS_LITERAL });
  611. continue;
  612. }
  613. /**
  614. * Plain text
  615. */
  616. if (value === '@') {
  617. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  618. push({ type: 'at', extglob: true, value, output: '' });
  619. continue;
  620. }
  621. push({ type: 'text', value });
  622. continue;
  623. }
  624. /**
  625. * Plain text
  626. */
  627. if (value !== '*') {
  628. if (value === '$' || value === '^') {
  629. value = `\\${value}`;
  630. }
  631. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  632. if (match) {
  633. value += match[0];
  634. state.index += match[0].length;
  635. }
  636. push({ type: 'text', value });
  637. continue;
  638. }
  639. /**
  640. * Stars
  641. */
  642. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  643. prev.type = 'star';
  644. prev.star = true;
  645. prev.value += value;
  646. prev.output = star;
  647. state.backtrack = true;
  648. state.globstar = true;
  649. consume(value);
  650. continue;
  651. }
  652. let rest = remaining();
  653. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  654. extglobOpen('star', value);
  655. continue;
  656. }
  657. if (prev.type === 'star') {
  658. if (opts.noglobstar === true) {
  659. consume(value);
  660. continue;
  661. }
  662. const prior = prev.prev;
  663. const before = prior.prev;
  664. const isStart = prior.type === 'slash' || prior.type === 'bos';
  665. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  666. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  667. push({ type: 'star', value, output: '' });
  668. continue;
  669. }
  670. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  671. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  672. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  673. push({ type: 'star', value, output: '' });
  674. continue;
  675. }
  676. // strip consecutive `/**/`
  677. while (rest.slice(0, 3) === '/**') {
  678. const after = input[state.index + 4];
  679. if (after && after !== '/') {
  680. break;
  681. }
  682. rest = rest.slice(3);
  683. consume('/**', 3);
  684. }
  685. if (prior.type === 'bos' && eos()) {
  686. prev.type = 'globstar';
  687. prev.value += value;
  688. prev.output = globstar(opts);
  689. state.output = prev.output;
  690. state.globstar = true;
  691. consume(value);
  692. continue;
  693. }
  694. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  695. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  696. prior.output = `(?:${prior.output}`;
  697. prev.type = 'globstar';
  698. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  699. prev.value += value;
  700. state.globstar = true;
  701. state.output += prior.output + prev.output;
  702. consume(value);
  703. continue;
  704. }
  705. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  706. const end = rest[1] !== void 0 ? '|$' : '';
  707. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  708. prior.output = `(?:${prior.output}`;
  709. prev.type = 'globstar';
  710. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  711. prev.value += value;
  712. state.output += prior.output + prev.output;
  713. state.globstar = true;
  714. consume(value + advance());
  715. push({ type: 'slash', value: '/', output: '' });
  716. continue;
  717. }
  718. if (prior.type === 'bos' && rest[0] === '/') {
  719. prev.type = 'globstar';
  720. prev.value += value;
  721. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  722. state.output = prev.output;
  723. state.globstar = true;
  724. consume(value + advance());
  725. push({ type: 'slash', value: '/', output: '' });
  726. continue;
  727. }
  728. // remove single star from output
  729. state.output = state.output.slice(0, -prev.output.length);
  730. // reset previous token to globstar
  731. prev.type = 'globstar';
  732. prev.output = globstar(opts);
  733. prev.value += value;
  734. // reset output with globstar
  735. state.output += prev.output;
  736. state.globstar = true;
  737. consume(value);
  738. continue;
  739. }
  740. const token = { type: 'star', value, output: star };
  741. if (opts.bash === true) {
  742. token.output = '.*?';
  743. if (prev.type === 'bos' || prev.type === 'slash') {
  744. token.output = nodot + token.output;
  745. }
  746. push(token);
  747. continue;
  748. }
  749. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  750. token.output = value;
  751. push(token);
  752. continue;
  753. }
  754. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  755. if (prev.type === 'dot') {
  756. state.output += NO_DOT_SLASH;
  757. prev.output += NO_DOT_SLASH;
  758. } else if (opts.dot === true) {
  759. state.output += NO_DOTS_SLASH;
  760. prev.output += NO_DOTS_SLASH;
  761. } else {
  762. state.output += nodot;
  763. prev.output += nodot;
  764. }
  765. if (peek() !== '*') {
  766. state.output += ONE_CHAR;
  767. prev.output += ONE_CHAR;
  768. }
  769. }
  770. push(token);
  771. }
  772. while (state.brackets > 0) {
  773. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  774. state.output = utils.escapeLast(state.output, '[');
  775. decrement('brackets');
  776. }
  777. while (state.parens > 0) {
  778. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  779. state.output = utils.escapeLast(state.output, '(');
  780. decrement('parens');
  781. }
  782. while (state.braces > 0) {
  783. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  784. state.output = utils.escapeLast(state.output, '{');
  785. decrement('braces');
  786. }
  787. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  788. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  789. }
  790. // rebuild the output if we had to backtrack at any point
  791. if (state.backtrack === true) {
  792. state.output = '';
  793. for (const token of state.tokens) {
  794. state.output += token.output != null ? token.output : token.value;
  795. if (token.suffix) {
  796. state.output += token.suffix;
  797. }
  798. }
  799. }
  800. return state;
  801. };
  802. /**
  803. * Fast paths for creating regular expressions for common glob patterns.
  804. * This can significantly speed up processing and has very little downside
  805. * impact when none of the fast paths match.
  806. */
  807. parse.fastpaths = (input, options) => {
  808. const opts = { ...options };
  809. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  810. const len = input.length;
  811. if (len > max) {
  812. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  813. }
  814. input = REPLACEMENTS[input] || input;
  815. const win32 = utils.isWindows(options);
  816. // create constants based on platform, for windows or posix
  817. const {
  818. DOT_LITERAL,
  819. SLASH_LITERAL,
  820. ONE_CHAR,
  821. DOTS_SLASH,
  822. NO_DOT,
  823. NO_DOTS,
  824. NO_DOTS_SLASH,
  825. STAR,
  826. START_ANCHOR
  827. } = constants.globChars(win32);
  828. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  829. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  830. const capture = opts.capture ? '' : '?:';
  831. const state = { negated: false, prefix: '' };
  832. let star = opts.bash === true ? '.*?' : STAR;
  833. if (opts.capture) {
  834. star = `(${star})`;
  835. }
  836. const globstar = opts => {
  837. if (opts.noglobstar === true) return star;
  838. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  839. };
  840. const create = str => {
  841. switch (str) {
  842. case '*':
  843. return `${nodot}${ONE_CHAR}${star}`;
  844. case '.*':
  845. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  846. case '*.*':
  847. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  848. case '*/*':
  849. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  850. case '**':
  851. return nodot + globstar(opts);
  852. case '**/*':
  853. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  854. case '**/*.*':
  855. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  856. case '**/.*':
  857. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  858. default: {
  859. const match = /^(.*?)\.(\w+)$/.exec(str);
  860. if (!match) return;
  861. const source = create(match[1]);
  862. if (!source) return;
  863. return source + DOT_LITERAL + match[2];
  864. }
  865. }
  866. };
  867. const output = utils.removePrefix(input, state);
  868. let source = create(output);
  869. if (source && opts.strictSlashes !== true) {
  870. source += `${SLASH_LITERAL}?`;
  871. }
  872. return source;
  873. };
  874. module.exports = parse;