index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
  6. var _regexpuCore = require("regexpu-core");
  7. var _features = require("./features");
  8. var _util = require("./util");
  9. var _core = require("@babel/core");
  10. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  11. const version = "7.19.0".split(".").reduce((v, x) => v * 1e5 + +x, 0);
  12. const versionKey = "@babel/plugin-regexp-features/version";
  13. function createRegExpFeaturePlugin({
  14. name,
  15. feature,
  16. options = {},
  17. manipulateOptions = () => {}
  18. }) {
  19. return {
  20. name,
  21. manipulateOptions,
  22. pre() {
  23. var _file$get;
  24. const {
  25. file
  26. } = this;
  27. const features = (_file$get = file.get(_features.featuresKey)) != null ? _file$get : 0;
  28. let newFeatures = (0, _features.enableFeature)(features, _features.FEATURES[feature]);
  29. const {
  30. useUnicodeFlag,
  31. runtime
  32. } = options;
  33. if (useUnicodeFlag === false) {
  34. newFeatures = (0, _features.enableFeature)(newFeatures, _features.FEATURES.unicodeFlag);
  35. }
  36. if (newFeatures !== features) {
  37. file.set(_features.featuresKey, newFeatures);
  38. }
  39. if (runtime !== undefined) {
  40. if (file.has(_features.runtimeKey) && file.get(_features.runtimeKey) !== runtime && (0, _features.hasFeature)(newFeatures, _features.FEATURES.duplicateNamedCaptureGroups)) {
  41. throw new Error(`The 'runtime' option must be the same for ` + `'@babel/plugin-transform-named-capturing-groups-regex' and ` + `'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.`);
  42. }
  43. if (feature === "namedCaptureGroups") {
  44. if (!runtime || !file.has(_features.runtimeKey)) file.set(_features.runtimeKey, runtime);
  45. } else {
  46. file.set(_features.runtimeKey, runtime);
  47. }
  48. }
  49. if (!file.has(versionKey) || file.get(versionKey) < version) {
  50. file.set(versionKey, version);
  51. }
  52. },
  53. visitor: {
  54. RegExpLiteral(path) {
  55. var _file$get2;
  56. const {
  57. node
  58. } = path;
  59. const {
  60. file
  61. } = this;
  62. const features = file.get(_features.featuresKey);
  63. const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
  64. const regexpuOptions = (0, _util.generateRegexpuOptions)(node.pattern, features);
  65. if ((0, _util.canSkipRegexpu)(node, regexpuOptions)) {
  66. return;
  67. }
  68. const namedCaptureGroups = {
  69. __proto__: null
  70. };
  71. if (regexpuOptions.namedGroups === "transform") {
  72. regexpuOptions.onNamedGroup = (name, index) => {
  73. const prev = namedCaptureGroups[name];
  74. if (typeof prev === "number") {
  75. namedCaptureGroups[name] = [prev, index];
  76. } else if (Array.isArray(prev)) {
  77. prev.push(index);
  78. } else {
  79. namedCaptureGroups[name] = index;
  80. }
  81. };
  82. }
  83. node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
  84. if (regexpuOptions.namedGroups === "transform" && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
  85. const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
  86. (0, _helperAnnotateAsPure.default)(call);
  87. path.replaceWith(call);
  88. }
  89. node.flags = (0, _util.transformFlags)(regexpuOptions, node.flags);
  90. }
  91. }
  92. };
  93. }
  94. function isRegExpTest(path) {
  95. return path.parentPath.isMemberExpression({
  96. object: path.node,
  97. computed: false
  98. }) && path.parentPath.get("property").isIdentifier({
  99. name: "test"
  100. });
  101. }
  102. //# sourceMappingURL=index.js.map