index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. function pullFlag(node, flag) {
  12. node.flags = node.flags.replace(flag, "");
  13. }
  14. const version = "7.14.5".split(".").reduce((v, x) => v * 1e5 + +x, 0);
  15. const versionKey = "@babel/plugin-regexp-features/version";
  16. function createRegExpFeaturePlugin({
  17. name,
  18. feature,
  19. options = {}
  20. }) {
  21. return {
  22. name,
  23. pre() {
  24. var _file$get;
  25. const {
  26. file
  27. } = this;
  28. const features = (_file$get = file.get(_features.featuresKey)) != null ? _file$get : 0;
  29. let newFeatures = (0, _features.enableFeature)(features, _features.FEATURES[feature]);
  30. const {
  31. useUnicodeFlag,
  32. runtime = true
  33. } = options;
  34. if (useUnicodeFlag === false) {
  35. newFeatures = (0, _features.enableFeature)(newFeatures, _features.FEATURES.unicodeFlag);
  36. }
  37. if (newFeatures !== features) {
  38. file.set(_features.featuresKey, newFeatures);
  39. }
  40. if (!runtime) {
  41. file.set(_features.runtimeKey, false);
  42. }
  43. if (!file.has(versionKey) || file.get(versionKey) < version) {
  44. file.set(versionKey, version);
  45. }
  46. },
  47. visitor: {
  48. RegExpLiteral(path) {
  49. var _file$get2;
  50. const {
  51. node
  52. } = path;
  53. const {
  54. file
  55. } = this;
  56. const features = file.get(_features.featuresKey);
  57. const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
  58. const regexpuOptions = (0, _util.generateRegexpuOptions)(node, features);
  59. if (regexpuOptions === null) {
  60. return;
  61. }
  62. const namedCaptureGroups = {};
  63. if (regexpuOptions.namedGroup) {
  64. regexpuOptions.onNamedGroup = (name, index) => {
  65. namedCaptureGroups[name] = index;
  66. };
  67. }
  68. node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
  69. if (regexpuOptions.namedGroup && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
  70. const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
  71. (0, _helperAnnotateAsPure.default)(call);
  72. path.replaceWith(call);
  73. }
  74. if ((0, _features.hasFeature)(features, _features.FEATURES.unicodeFlag)) {
  75. pullFlag(node, "u");
  76. }
  77. if ((0, _features.hasFeature)(features, _features.FEATURES.dotAllFlag)) {
  78. pullFlag(node, "s");
  79. }
  80. }
  81. }
  82. };
  83. }
  84. function isRegExpTest(path) {
  85. return path.parentPath.isMemberExpression({
  86. object: path.node,
  87. computed: false
  88. }) && path.parentPath.get("property").isIdentifier({
  89. name: "test"
  90. });
  91. }