index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.17.12".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 = true
  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) {
  40. file.set(_features.runtimeKey, false);
  41. }
  42. if (!file.has(versionKey) || file.get(versionKey) < version) {
  43. file.set(versionKey, version);
  44. }
  45. },
  46. visitor: {
  47. RegExpLiteral(path) {
  48. var _file$get2;
  49. const {
  50. node
  51. } = path;
  52. const {
  53. file
  54. } = this;
  55. const features = file.get(_features.featuresKey);
  56. const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
  57. const regexpuOptions = (0, _util.generateRegexpuOptions)(features);
  58. if ((0, _util.canSkipRegexpu)(node, regexpuOptions)) return;
  59. const namedCaptureGroups = {};
  60. if (regexpuOptions.namedGroups === "transform") {
  61. regexpuOptions.onNamedGroup = (name, index) => {
  62. namedCaptureGroups[name] = index;
  63. };
  64. }
  65. node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
  66. if (regexpuOptions.namedGroups === "transform" && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
  67. const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
  68. (0, _helperAnnotateAsPure.default)(call);
  69. path.replaceWith(call);
  70. }
  71. node.flags = (0, _util.transformFlags)(regexpuOptions, node.flags);
  72. }
  73. }
  74. };
  75. }
  76. function isRegExpTest(path) {
  77. return path.parentPath.isMemberExpression({
  78. object: path.node,
  79. computed: false
  80. }) && path.parentPath.get("property").isIdentifier({
  81. name: "test"
  82. });
  83. }