index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
  6. var _regexpuCore = require("regexpu-core");
  7. var _core = require("@babel/core");
  8. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  9. var _semver = require("semver");
  10. var _features = require("./features.js");
  11. var _util = require("./util.js");
  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-transform-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. {
  50. if (typeof file.get(versionKey) === "number") {
  51. file.set(versionKey, "7.25.9");
  52. return;
  53. }
  54. }
  55. if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.25.9")) {
  56. file.set(versionKey, "7.25.9");
  57. }
  58. },
  59. visitor: {
  60. RegExpLiteral(path) {
  61. var _file$get2, _newFlags;
  62. const {
  63. node
  64. } = path;
  65. const {
  66. file
  67. } = this;
  68. const features = file.get(_features.featuresKey);
  69. const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
  70. const regexpuOptions = (0, _util.generateRegexpuOptions)(node.pattern, features);
  71. if ((0, _util.canSkipRegexpu)(node, regexpuOptions)) {
  72. return;
  73. }
  74. const namedCaptureGroups = {
  75. __proto__: null
  76. };
  77. if (regexpuOptions.namedGroups === "transform") {
  78. regexpuOptions.onNamedGroup = (name, index) => {
  79. const prev = namedCaptureGroups[name];
  80. if (typeof prev === "number") {
  81. namedCaptureGroups[name] = [prev, index];
  82. } else if (Array.isArray(prev)) {
  83. prev.push(index);
  84. } else {
  85. namedCaptureGroups[name] = index;
  86. }
  87. };
  88. }
  89. let newFlags;
  90. if (regexpuOptions.modifiers === "transform") {
  91. regexpuOptions.onNewFlags = flags => {
  92. newFlags = flags;
  93. };
  94. }
  95. node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
  96. if (regexpuOptions.namedGroups === "transform" && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
  97. const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
  98. (0, _helperAnnotateAsPure.default)(call);
  99. path.replaceWith(call);
  100. }
  101. node.flags = (0, _util.transformFlags)(regexpuOptions, (_newFlags = newFlags) != null ? _newFlags : node.flags);
  102. }
  103. }
  104. };
  105. }
  106. function isRegExpTest(path) {
  107. return path.parentPath.isMemberExpression({
  108. object: path.node,
  109. computed: false
  110. }) && path.parentPath.get("property").isIdentifier({
  111. name: "test"
  112. });
  113. }
  114. //# sourceMappingURL=index.js.map