util.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.canSkipRegexpu = canSkipRegexpu;
  6. exports.generateRegexpuOptions = generateRegexpuOptions;
  7. exports.transformFlags = transformFlags;
  8. var _features = require("./features.js");
  9. function generateRegexpuOptions(pattern, toTransform) {
  10. const feat = name => {
  11. return (0, _features.hasFeature)(toTransform, _features.FEATURES[name]) ? "transform" : false;
  12. };
  13. const featDuplicateNamedGroups = () => {
  14. if (!feat("duplicateNamedCaptureGroups")) return false;
  15. const regex = /\(\?<([^>]+)>/g;
  16. const seen = new Set();
  17. for (let match; match = regex.exec(pattern); seen.add(match[1])) {
  18. if (seen.has(match[1])) return "transform";
  19. }
  20. return false;
  21. };
  22. return {
  23. unicodeFlag: feat("unicodeFlag"),
  24. unicodeSetsFlag: feat("unicodeSetsFlag"),
  25. dotAllFlag: feat("dotAllFlag"),
  26. unicodePropertyEscapes: feat("unicodePropertyEscape"),
  27. namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(),
  28. onNamedGroup: () => {},
  29. modifiers: feat("modifiers")
  30. };
  31. }
  32. function canSkipRegexpu(node, options) {
  33. const {
  34. flags,
  35. pattern
  36. } = node;
  37. if (flags.includes("v")) {
  38. if (options.unicodeSetsFlag === "transform") return false;
  39. }
  40. if (flags.includes("u")) {
  41. if (options.unicodeFlag === "transform") return false;
  42. if (options.unicodePropertyEscapes === "transform" && /\\p\{/i.test(pattern)) {
  43. return false;
  44. }
  45. }
  46. if (flags.includes("s")) {
  47. if (options.dotAllFlag === "transform") return false;
  48. }
  49. if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) {
  50. return false;
  51. }
  52. if (options.modifiers === "transform" && /\(\?[\w-]+:/.test(pattern)) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. function transformFlags(regexpuOptions, flags) {
  58. if (regexpuOptions.unicodeSetsFlag === "transform") {
  59. flags = flags.replace("v", "u");
  60. }
  61. if (regexpuOptions.unicodeFlag === "transform") {
  62. flags = flags.replace("u", "");
  63. }
  64. if (regexpuOptions.dotAllFlag === "transform") {
  65. flags = flags.replace("s", "");
  66. }
  67. return flags;
  68. }
  69. //# sourceMappingURL=util.js.map