You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.generateRegexpuOptions = generateRegexpuOptions;
  6. var _features = require("./features");
  7. function generateRegexpuOptions(node, features) {
  8. let useUnicodeFlag = false,
  9. dotAllFlag = false,
  10. unicodePropertyEscape = false,
  11. namedGroup = false;
  12. const {
  13. flags,
  14. pattern
  15. } = node;
  16. const flagsIncludesU = flags.includes("u");
  17. if (flagsIncludesU) {
  18. if (!(0, _features.hasFeature)(features, _features.FEATURES.unicodeFlag)) {
  19. useUnicodeFlag = true;
  20. }
  21. if ((0, _features.hasFeature)(features, _features.FEATURES.unicodePropertyEscape) && /\\[pP]{/.test(pattern)) {
  22. unicodePropertyEscape = true;
  23. }
  24. }
  25. if ((0, _features.hasFeature)(features, _features.FEATURES.dotAllFlag) && flags.indexOf("s") >= 0) {
  26. dotAllFlag = true;
  27. }
  28. if ((0, _features.hasFeature)(features, _features.FEATURES.namedCaptureGroups) && /\(\?<(?![=!])/.test(pattern)) {
  29. namedGroup = true;
  30. }
  31. if (!namedGroup && !unicodePropertyEscape && !dotAllFlag && (!flagsIncludesU || useUnicodeFlag)) {
  32. return null;
  33. }
  34. if (flagsIncludesU && flags.indexOf("s") >= 0) {
  35. dotAllFlag = true;
  36. }
  37. return {
  38. useUnicodeFlag,
  39. onNamedGroup: () => {},
  40. namedGroup,
  41. unicodePropertyEscape,
  42. dotAllFlag,
  43. lookbehind: true
  44. };
  45. }