rewrite-pattern.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. 'use strict';
  2. const generate = require('regjsgen').generate;
  3. const parse = require('regjsparser').parse;
  4. const regenerate = require('regenerate');
  5. const unicodeMatchProperty = require('unicode-match-property-ecmascript');
  6. const unicodeMatchPropertyValue = require('unicode-match-property-value-ecmascript');
  7. const iuMappings = require('./data/iu-mappings.js');
  8. const ESCAPE_SETS = require('./data/character-class-escape-sets.js');
  9. // Prepare a Regenerate set containing all code points, used for negative
  10. // character classes (if any).
  11. const UNICODE_SET = regenerate().addRange(0x0, 0x10FFFF);
  12. // Prepare a Regenerate set containing all code points that are supposed to be
  13. // matched by `/./u`. https://mths.be/es6#sec-atom
  14. const DOT_SET_UNICODE = UNICODE_SET.clone() // all Unicode code points
  15. .remove(
  16. // minus `LineTerminator`s (https://mths.be/es6#sec-line-terminators):
  17. 0x000A, // Line Feed <LF>
  18. 0x000D, // Carriage Return <CR>
  19. 0x2028, // Line Separator <LS>
  20. 0x2029 // Paragraph Separator <PS>
  21. );
  22. const getCharacterClassEscapeSet = (character, unicode, ignoreCase) => {
  23. if (unicode) {
  24. if (ignoreCase) {
  25. return ESCAPE_SETS.UNICODE_IGNORE_CASE.get(character);
  26. }
  27. return ESCAPE_SETS.UNICODE.get(character);
  28. }
  29. return ESCAPE_SETS.REGULAR.get(character);
  30. };
  31. const getUnicodeDotSet = (dotAll) => {
  32. return dotAll ? UNICODE_SET : DOT_SET_UNICODE;
  33. };
  34. const getUnicodePropertyValueSet = (property, value) => {
  35. const path = value ?
  36. `${ property }/${ value }` :
  37. `Binary_Property/${ property }`;
  38. try {
  39. return require(`regenerate-unicode-properties/${ path }.js`);
  40. } catch (exception) {
  41. throw new Error(
  42. `Failed to recognize value \`${ value }\` for property ` +
  43. `\`${ property }\`.`
  44. );
  45. }
  46. };
  47. const handleLoneUnicodePropertyNameOrValue = (value) => {
  48. // It could be a `General_Category` value or a binary property.
  49. // Note: `unicodeMatchPropertyValue` throws on invalid values.
  50. try {
  51. const property = 'General_Category';
  52. const category = unicodeMatchPropertyValue(property, value);
  53. return getUnicodePropertyValueSet(property, category);
  54. } catch (exception) {}
  55. // It’s not a `General_Category` value, so check if it’s a property
  56. // of strings.
  57. try {
  58. return getUnicodePropertyValueSet('Property_of_Strings', value);
  59. } catch (exception) {}
  60. // Lastly, check if it’s a binary property of single code points.
  61. // Note: `unicodeMatchProperty` throws on invalid properties.
  62. const property = unicodeMatchProperty(value);
  63. return getUnicodePropertyValueSet(property);
  64. };
  65. const getUnicodePropertyEscapeSet = (value, isNegative) => {
  66. const parts = value.split('=');
  67. const firstPart = parts[0];
  68. let set;
  69. if (parts.length == 1) {
  70. set = handleLoneUnicodePropertyNameOrValue(firstPart);
  71. } else {
  72. // The pattern consists of two parts, i.e. `Property=Value`.
  73. const property = unicodeMatchProperty(firstPart);
  74. const value = unicodeMatchPropertyValue(property, parts[1]);
  75. set = getUnicodePropertyValueSet(property, value);
  76. }
  77. if (isNegative) {
  78. if (set.strings) {
  79. throw new Error('Cannot negate Unicode property of strings');
  80. }
  81. return {
  82. characters: UNICODE_SET.clone().remove(set.characters),
  83. strings: new Set()
  84. };
  85. }
  86. return {
  87. characters: set.characters.clone(),
  88. strings: new Set(set.strings || [])
  89. };
  90. };
  91. const getUnicodePropertyEscapeCharacterClassData = (property, isNegative) => {
  92. const set = getUnicodePropertyEscapeSet(property, isNegative);
  93. const data = getCharacterClassEmptyData();
  94. data.singleChars = set.characters;
  95. if (set.strings.size > 0) {
  96. data.longStrings = set.strings;
  97. data.maybeIncludesStrings = true;
  98. }
  99. return data;
  100. };
  101. // Given a range of code points, add any case-folded code points in that range
  102. // to a set.
  103. regenerate.prototype.iuAddRange = function(min, max) {
  104. const $this = this;
  105. do {
  106. const folded = caseFold(min);
  107. if (folded) {
  108. $this.add(folded);
  109. }
  110. } while (++min <= max);
  111. return $this;
  112. };
  113. regenerate.prototype.iuRemoveRange = function(min, max) {
  114. const $this = this;
  115. do {
  116. const folded = caseFold(min);
  117. if (folded) {
  118. $this.remove(folded);
  119. }
  120. } while (++min <= max);
  121. return $this;
  122. };
  123. const update = (item, pattern) => {
  124. let tree = parse(pattern, config.useUnicodeFlag ? 'u' : '');
  125. switch (tree.type) {
  126. case 'characterClass':
  127. case 'group':
  128. case 'value':
  129. // No wrapping needed.
  130. break;
  131. default:
  132. // Wrap the pattern in a non-capturing group.
  133. tree = wrap(tree, pattern);
  134. }
  135. Object.assign(item, tree);
  136. };
  137. const wrap = (tree, pattern) => {
  138. // Wrap the pattern in a non-capturing group.
  139. return {
  140. 'type': 'group',
  141. 'behavior': 'ignore',
  142. 'body': [tree],
  143. 'raw': `(?:${ pattern })`
  144. };
  145. };
  146. const caseFold = (codePoint) => {
  147. return iuMappings.get(codePoint) || false;
  148. };
  149. const buildHandler = (action) => {
  150. switch (action) {
  151. case 'union':
  152. return {
  153. single: (data, cp) => {
  154. data.singleChars.add(cp);
  155. },
  156. regSet: (data, set2) => {
  157. data.singleChars.add(set2);
  158. },
  159. range: (data, start, end) => {
  160. data.singleChars.addRange(start, end);
  161. },
  162. iuRange: (data, start, end) => {
  163. data.singleChars.iuAddRange(start, end);
  164. },
  165. nested: (data, nestedData) => {
  166. data.singleChars.add(nestedData.singleChars);
  167. for (const str of nestedData.longStrings) data.longStrings.add(str);
  168. if (nestedData.maybeIncludesStrings) data.maybeIncludesStrings = true;
  169. }
  170. };
  171. case 'union-negative': {
  172. const regSet = (data, set2) => {
  173. data.singleChars = UNICODE_SET.clone().remove(set2).add(data.singleChars);
  174. };
  175. return {
  176. single: (data, cp) => {
  177. const unicode = UNICODE_SET.clone();
  178. data.singleChars = data.singleChars.contains(cp) ? unicode : unicode.remove(cp);
  179. },
  180. regSet: regSet,
  181. range: (data, start, end) => {
  182. data.singleChars = UNICODE_SET.clone().removeRange(start, end).add(data.singleChars);
  183. },
  184. iuRange: (data, start, end) => {
  185. data.singleChars = UNICODE_SET.clone().iuRemoveRange(start, end).add(data.singleChars);
  186. },
  187. nested: (data, nestedData) => {
  188. regSet(data, nestedData.singleChars);
  189. if (nestedData.maybeIncludesStrings) throw new Error('ASSERTION ERROR');
  190. }
  191. };
  192. }
  193. case 'intersection': {
  194. const regSet = (data, set2) => {
  195. if (data.first) data.singleChars = set2;
  196. else data.singleChars.intersection(set2);
  197. };
  198. return {
  199. single: (data, cp) => {
  200. data.singleChars = data.first || data.singleChars.contains(cp) ? regenerate(cp) : regenerate();
  201. data.longStrings.clear();
  202. data.maybeIncludesStrings = false;
  203. },
  204. regSet: (data, set) => {
  205. regSet(data, set);
  206. data.longStrings.clear();
  207. data.maybeIncludesStrings = false;
  208. },
  209. range: (data, start, end) => {
  210. if (data.first) data.singleChars.addRange(start, end);
  211. else data.singleChars.intersection(regenerate().addRange(start, end));
  212. data.longStrings.clear();
  213. data.maybeIncludesStrings = false;
  214. },
  215. iuRange: (data, start, end) => {
  216. if (data.first) data.singleChars.iuAddRange(start, end);
  217. else data.singleChars.intersection(regenerate().iuAddRange(start, end));
  218. data.longStrings.clear();
  219. data.maybeIncludesStrings = false;
  220. },
  221. nested: (data, nestedData) => {
  222. regSet(data, nestedData.singleChars);
  223. if (data.first) {
  224. data.longStrings = nestedData.longStrings;
  225. data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
  226. } else {
  227. for (const str of data.longStrings) {
  228. if (!nestedData.longStrings.has(str)) data.longStrings.delete(str);
  229. }
  230. if (!nestedData.maybeIncludesStrings) data.maybeIncludesStrings = false;
  231. }
  232. }
  233. };
  234. }
  235. case 'subtraction': {
  236. const regSet = (data, set2) => {
  237. if (data.first) data.singleChars.add(set2);
  238. else data.singleChars.remove(set2);
  239. };
  240. return {
  241. single: (data, cp) => {
  242. if (data.first) data.singleChars.add(cp);
  243. else data.singleChars.remove(cp);
  244. },
  245. regSet: regSet,
  246. range: (data, start, end) => {
  247. if (data.first) data.singleChars.addRange(start, end);
  248. else data.singleChars.removeRange(start, end);
  249. },
  250. iuRange: (data, start, end) => {
  251. if (data.first) data.singleChars.iuAddRange(start, end);
  252. else data.singleChars.iuRemoveRange(start, end);
  253. },
  254. nested: (data, nestedData) => {
  255. regSet(data, nestedData.singleChars);
  256. if (data.first) {
  257. data.longStrings = nestedData.longStrings;
  258. data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
  259. } else {
  260. for (const str of data.longStrings) {
  261. if (nestedData.longStrings.has(str)) data.longStrings.delete(str);
  262. }
  263. }
  264. }
  265. };
  266. }
  267. // The `default` clause is only here as a safeguard; it should never be
  268. // reached. Code coverage tools should ignore it.
  269. /* istanbul ignore next */
  270. default:
  271. throw new Error(`Unknown set action: ${ characterClassItem.kind }`);
  272. }
  273. };
  274. const getCharacterClassEmptyData = () => ({
  275. transformed: config.transform.unicodeFlag,
  276. singleChars: regenerate(),
  277. longStrings: new Set(),
  278. hasEmptyString: false,
  279. first: true,
  280. maybeIncludesStrings: false
  281. });
  282. const maybeFold = (codePoint) => {
  283. if (config.flags.ignoreCase && config.transform.unicodeFlag) {
  284. const folded = caseFold(codePoint);
  285. if (folded) {
  286. return [codePoint, folded];
  287. }
  288. }
  289. return [codePoint];
  290. };
  291. const computeClassStrings = (classStrings, regenerateOptions) => {
  292. let data = getCharacterClassEmptyData();
  293. for (const string of classStrings.strings) {
  294. if (string.characters.length === 1) {
  295. maybeFold(string.characters[0].codePoint).forEach((cp) => {
  296. data.singleChars.add(cp);
  297. });
  298. } else {
  299. let stringifiedString;
  300. if (config.flags.ignoreCase && config.transform.unicodeFlag) {
  301. stringifiedString = '';
  302. for (const ch of string.characters) {
  303. let set = regenerate(ch.codePoint);
  304. const folded = caseFold(ch.codePoint);
  305. if (folded) set.add(folded);
  306. stringifiedString += set.toString(regenerateOptions);
  307. }
  308. } else {
  309. stringifiedString = string.characters.map(ch => generate(ch)).join('')
  310. }
  311. data.longStrings.add(stringifiedString);
  312. data.maybeIncludesStrings = true;
  313. }
  314. }
  315. return data;
  316. }
  317. const computeCharacterClass = (characterClassItem, regenerateOptions) => {
  318. let data = getCharacterClassEmptyData();
  319. let handlePositive;
  320. let handleNegative;
  321. switch (characterClassItem.kind) {
  322. case 'union':
  323. handlePositive = buildHandler('union');
  324. handleNegative = buildHandler('union-negative');
  325. break;
  326. case 'intersection':
  327. handlePositive = buildHandler('intersection');
  328. handleNegative = buildHandler('subtraction');
  329. break;
  330. case 'subtraction':
  331. handlePositive = buildHandler('subtraction');
  332. handleNegative = buildHandler('intersection');
  333. break;
  334. // The `default` clause is only here as a safeguard; it should never be
  335. // reached. Code coverage tools should ignore it.
  336. /* istanbul ignore next */
  337. default:
  338. throw new Error(`Unknown character class kind: ${ characterClassItem.kind }`);
  339. }
  340. for (const item of characterClassItem.body) {
  341. switch (item.type) {
  342. case 'value':
  343. maybeFold(item.codePoint).forEach((cp) => {
  344. handlePositive.single(data, cp);
  345. });
  346. break;
  347. case 'characterClassRange':
  348. const min = item.min.codePoint;
  349. const max = item.max.codePoint;
  350. handlePositive.range(data, min, max);
  351. if (config.flags.ignoreCase && config.transform.unicodeFlag) {
  352. handlePositive.iuRange(data, min, max);
  353. }
  354. break;
  355. case 'characterClassEscape':
  356. handlePositive.regSet(data, getCharacterClassEscapeSet(
  357. item.value,
  358. config.flags.unicode,
  359. config.flags.ignoreCase
  360. ));
  361. break;
  362. case 'unicodePropertyEscape':
  363. const nestedData = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
  364. handlePositive.nested(data, nestedData);
  365. data.transformed =
  366. data.transformed ||
  367. config.transform.unicodePropertyEscapes ||
  368. (config.transform.unicodeSetsFlag && nestedData.maybeIncludesStrings);
  369. break;
  370. case 'characterClass':
  371. const handler = item.negative ? handleNegative : handlePositive;
  372. const res = computeCharacterClass(item, regenerateOptions);
  373. handler.nested(data, res);
  374. data.transformed = true;
  375. break;
  376. case 'classStrings':
  377. handlePositive.nested(data, computeClassStrings(item, regenerateOptions));
  378. data.transformed = true;
  379. break;
  380. // The `default` clause is only here as a safeguard; it should never be
  381. // reached. Code coverage tools should ignore it.
  382. /* istanbul ignore next */
  383. default:
  384. throw new Error(`Unknown term type: ${ item.type }`);
  385. }
  386. data.first = false;
  387. }
  388. if (characterClassItem.negative && data.maybeIncludesStrings) {
  389. throw new SyntaxError('Cannot negate set containing strings');
  390. }
  391. return data;
  392. }
  393. const processCharacterClass = (
  394. characterClassItem,
  395. regenerateOptions,
  396. computed = computeCharacterClass(characterClassItem, regenerateOptions)
  397. ) => {
  398. const negative = characterClassItem.negative;
  399. const { singleChars, transformed, longStrings } = computed;
  400. if (transformed) {
  401. const setStr = singleChars.toString(regenerateOptions);
  402. if (negative) {
  403. if (config.useUnicodeFlag) {
  404. update(characterClassItem, `[^${setStr[0] === '[' ? setStr.slice(1, -1) : setStr}]`)
  405. } else {
  406. update(characterClassItem, `(?!${setStr})[\\s\\S]`)
  407. }
  408. } else {
  409. const hasEmptyString = longStrings.has('');
  410. const pieces = Array.from(longStrings).sort((a, b) => b.length - a.length);
  411. if (setStr !== '[]' || longStrings.size === 0) {
  412. pieces.splice(pieces.length - (hasEmptyString ? 1 : 0), 0, setStr);
  413. }
  414. update(characterClassItem, pieces.join('|'));
  415. }
  416. }
  417. return characterClassItem;
  418. };
  419. const updateNamedReference = (item, index) => {
  420. delete item.name;
  421. item.matchIndex = index;
  422. };
  423. const assertNoUnmatchedReferences = (groups) => {
  424. const unmatchedReferencesNames = Object.keys(groups.unmatchedReferences);
  425. if (unmatchedReferencesNames.length > 0) {
  426. throw new Error(`Unknown group names: ${unmatchedReferencesNames}`);
  427. }
  428. };
  429. const processTerm = (item, regenerateOptions, groups) => {
  430. switch (item.type) {
  431. case 'dot':
  432. if (config.transform.unicodeFlag) {
  433. update(
  434. item,
  435. getUnicodeDotSet(config.flags.dotAll).toString(regenerateOptions)
  436. );
  437. } else if (config.transform.dotAllFlag) {
  438. // TODO: consider changing this at the regenerate level.
  439. update(item, '[\\s\\S]');
  440. }
  441. break;
  442. case 'characterClass':
  443. item = processCharacterClass(item, regenerateOptions);
  444. break;
  445. case 'unicodePropertyEscape':
  446. const data = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
  447. if (data.maybeIncludesStrings) {
  448. if (!config.flags.unicodeSets) {
  449. throw new Error(
  450. 'Properties of strings are only supported when using the unicodeSets (v) flag.'
  451. );
  452. }
  453. if (config.transform.unicodeSetsFlag) {
  454. data.transformed = true;
  455. item = processCharacterClass(item, regenerateOptions, data);
  456. }
  457. } else if (config.transform.unicodePropertyEscapes) {
  458. update(
  459. item,
  460. data.singleChars.toString(regenerateOptions)
  461. );
  462. }
  463. break;
  464. case 'characterClassEscape':
  465. if (config.transform.unicodeFlag) {
  466. update(
  467. item,
  468. getCharacterClassEscapeSet(
  469. item.value,
  470. /* config.transform.unicodeFlag implies config.flags.unicode */ true,
  471. config.flags.ignoreCase
  472. ).toString(regenerateOptions)
  473. );
  474. }
  475. break;
  476. case 'group':
  477. if (item.behavior == 'normal') {
  478. groups.lastIndex++;
  479. }
  480. if (item.name && config.transform.namedGroups) {
  481. const name = item.name.value;
  482. if (groups.names[name]) {
  483. throw new Error(
  484. `Multiple groups with the same name (${ name }) are not allowed.`
  485. );
  486. }
  487. const index = groups.lastIndex;
  488. delete item.name;
  489. groups.names[name] = index;
  490. if (groups.onNamedGroup) {
  491. groups.onNamedGroup.call(null, name, index);
  492. }
  493. if (groups.unmatchedReferences[name]) {
  494. groups.unmatchedReferences[name].forEach(reference => {
  495. updateNamedReference(reference, index);
  496. });
  497. delete groups.unmatchedReferences[name];
  498. }
  499. }
  500. /* falls through */
  501. case 'alternative':
  502. case 'disjunction':
  503. case 'quantifier':
  504. item.body = item.body.map(term => {
  505. return processTerm(term, regenerateOptions, groups);
  506. });
  507. break;
  508. case 'value':
  509. const codePoint = item.codePoint;
  510. const set = regenerate(codePoint);
  511. if (config.flags.ignoreCase && config.transform.unicodeFlag) {
  512. const folded = caseFold(codePoint);
  513. if (folded) {
  514. set.add(folded);
  515. }
  516. }
  517. update(item, set.toString(regenerateOptions));
  518. break;
  519. case 'reference':
  520. if (item.name) {
  521. const name = item.name.value;
  522. const index = groups.names[name];
  523. if (index) {
  524. updateNamedReference(item, index);
  525. break;
  526. }
  527. if (!groups.unmatchedReferences[name]) {
  528. groups.unmatchedReferences[name] = [];
  529. }
  530. // Keep track of references used before the corresponding group.
  531. groups.unmatchedReferences[name].push(item);
  532. }
  533. break;
  534. case 'anchor':
  535. case 'empty':
  536. case 'group':
  537. // Nothing to do here.
  538. break;
  539. // The `default` clause is only here as a safeguard; it should never be
  540. // reached. Code coverage tools should ignore it.
  541. /* istanbul ignore next */
  542. default:
  543. throw new Error(`Unknown term type: ${ item.type }`);
  544. }
  545. return item;
  546. };
  547. const config = {
  548. 'flags': {
  549. 'ignoreCase': false,
  550. 'unicode': false,
  551. 'unicodeSets': false,
  552. 'dotAll': false,
  553. },
  554. 'transform': {
  555. 'dotAllFlag': false,
  556. 'unicodeFlag': false,
  557. 'unicodeSetsFlag': false,
  558. 'unicodePropertyEscapes': false,
  559. 'namedGroups': false,
  560. },
  561. get useUnicodeFlag() {
  562. return (this.flags.unicode || this.flags.unicodeSets) && !this.transform.unicodeFlag;
  563. }
  564. };
  565. const validateOptions = (options) => {
  566. if (!options) return;
  567. for (const key of Object.keys(options)) {
  568. const value = options[key];
  569. switch (key) {
  570. case 'dotAllFlag':
  571. case 'unicodeFlag':
  572. case 'unicodePropertyEscapes':
  573. case 'namedGroups':
  574. if (value != null && value !== false && value !== 'transform') {
  575. throw new Error(`.${key} must be false (default) or 'transform'.`);
  576. }
  577. break;
  578. case 'unicodeSetsFlag':
  579. if (value != null && value !== false && value !== 'parse' && value !== 'transform') {
  580. throw new Error(`.${key} must be false (default), 'parse' or 'transform'.`);
  581. }
  582. break;
  583. case 'onNamedGroup':
  584. if (value != null && typeof value !== 'function') {
  585. throw new Error('.onNamedGroup must be a function.');
  586. }
  587. break;
  588. default:
  589. throw new Error(`.${key} is not a valid regexpu-core option.`);
  590. }
  591. }
  592. };
  593. const hasFlag = (flags, flag) => flags ? flags.includes(flag) : false;
  594. const transform = (options, name) => options ? options[name] === 'transform' : false;
  595. const rewritePattern = (pattern, flags, options) => {
  596. validateOptions(options);
  597. config.flags.unicode = hasFlag(flags, 'u');
  598. config.flags.unicodeSets = hasFlag(flags, 'v');
  599. config.flags.ignoreCase = hasFlag(flags, 'i');
  600. config.flags.dotAll = hasFlag(flags, 's');
  601. config.transform.dotAllFlag = config.flags.dotAll && transform(options, 'dotAllFlag');
  602. config.transform.unicodeFlag = (config.flags.unicode || config.flags.unicodeSets) && transform(options, 'unicodeFlag');
  603. config.transform.unicodeSetsFlag = config.flags.unicodeSets && transform(options, 'unicodeSetsFlag');
  604. // unicodeFlag: 'transform' implies unicodePropertyEscapes: 'transform'
  605. config.transform.unicodePropertyEscapes = config.flags.unicode && (
  606. transform(options, 'unicodeFlag') || transform(options, 'unicodePropertyEscapes')
  607. );
  608. config.transform.namedGroups = transform(options, 'namedGroups');
  609. const regjsparserFeatures = {
  610. 'unicodeSet': Boolean(options && options.unicodeSetsFlag),
  611. // Enable every stable RegExp feature by default
  612. 'unicodePropertyEscape': true,
  613. 'namedGroups': true,
  614. 'lookbehind': true,
  615. };
  616. const regenerateOptions = {
  617. 'hasUnicodeFlag': config.useUnicodeFlag,
  618. 'bmpOnly': !config.flags.unicode
  619. };
  620. const groups = {
  621. 'onNamedGroup': options && options.onNamedGroup,
  622. 'lastIndex': 0,
  623. 'names': Object.create(null), // { [name]: index }
  624. 'unmatchedReferences': Object.create(null) // { [name]: Array<reference> }
  625. };
  626. const tree = parse(pattern, flags, regjsparserFeatures);
  627. // Note: `processTerm` mutates `tree` and `groups`.
  628. processTerm(tree, regenerateOptions, groups);
  629. assertNoUnmatchedReferences(groups);
  630. return generate(tree);
  631. };
  632. module.exports = rewritePattern;