util.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(toTransform) {
  10. const feat = (name, ok = "transform") => {
  11. return (0, _features.hasFeature)(toTransform, _features.FEATURES[name]) ? ok : false;
  12. };
  13. return {
  14. unicodeFlag: feat("unicodeFlag"),
  15. unicodeSetsFlag: feat("unicodeSetsFlag") || feat("unicodeSetsFlag_syntax", "parse"),
  16. dotAllFlag: feat("dotAllFlag"),
  17. unicodePropertyEscapes: feat("unicodePropertyEscape"),
  18. namedGroups: feat("namedCaptureGroups"),
  19. onNamedGroup: () => {}
  20. };
  21. }
  22. function canSkipRegexpu(node, options) {
  23. const {
  24. flags,
  25. pattern
  26. } = node;
  27. if (flags.includes("v")) {
  28. if (options.unicodeSetsFlag === "transform") return false;
  29. }
  30. if (flags.includes("u")) {
  31. if (options.unicodeFlag === "transform") return false;
  32. if (options.unicodePropertyEscapes === "transform" && /\\[pP]{/.test(pattern)) {
  33. return false;
  34. }
  35. }
  36. if (flags.includes("s")) {
  37. if (options.dotAllFlag === "transform") return false;
  38. }
  39. if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. function transformFlags(regexpuOptions, flags) {
  45. if (regexpuOptions.unicodeSetsFlag === "transform") {
  46. flags = flags.replace("v", "u");
  47. }
  48. if (regexpuOptions.unicodeFlag === "transform") {
  49. flags = flags.replace("u", "");
  50. }
  51. if (regexpuOptions.dotAllFlag === "transform") {
  52. flags = flags.replace("s", "");
  53. }
  54. return flags;
  55. }