util.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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");
  9. function generateRegexpuOptions(pattern, toTransform) {
  10. const feat = (name, ok = "transform") => {
  11. return (0, _features.hasFeature)(toTransform, _features.FEATURES[name]) ? ok : 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") || feat("unicodeSetsFlag_syntax", "parse"),
  25. dotAllFlag: feat("dotAllFlag"),
  26. unicodePropertyEscapes: feat("unicodePropertyEscape"),
  27. namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(),
  28. onNamedGroup: () => {}
  29. };
  30. }
  31. function canSkipRegexpu(node, options) {
  32. const {
  33. flags,
  34. pattern
  35. } = node;
  36. if (flags.includes("v")) {
  37. if (options.unicodeSetsFlag === "transform") return false;
  38. }
  39. if (flags.includes("u")) {
  40. if (options.unicodeFlag === "transform") return false;
  41. if (options.unicodePropertyEscapes === "transform" && /\\[pP]{/.test(pattern)) {
  42. return false;
  43. }
  44. }
  45. if (flags.includes("s")) {
  46. if (options.dotAllFlag === "transform") return false;
  47. }
  48. if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. function transformFlags(regexpuOptions, flags) {
  54. if (regexpuOptions.unicodeSetsFlag === "transform") {
  55. flags = flags.replace("v", "u");
  56. }
  57. if (regexpuOptions.unicodeFlag === "transform") {
  58. flags = flags.replace("u", "");
  59. }
  60. if (regexpuOptions.dotAllFlag === "transform") {
  61. flags = flags.replace("s", "");
  62. }
  63. return flags;
  64. }
  65. //# sourceMappingURL=util.js.map