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.

115 lines
3.1 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
  6. var _regexpuCore = _interopRequireDefault(require("regexpu-core"));
  7. var _features = require("./features");
  8. var _util = require("./util");
  9. var _package = _interopRequireDefault(require("../package.json"));
  10. var _core = require("@babel/core");
  11. var _helperRegex = require("@babel/helper-regex");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const version = _package.default.version.split(".").reduce((v, x) => v * 1e5 + +x, 0);
  14. const versionKey = "@babel/plugin-regexp-features/version";
  15. function createRegExpFeaturePlugin({
  16. name,
  17. feature,
  18. options = {}
  19. }) {
  20. return {
  21. name,
  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)(node, features);
  58. if (regexpuOptions === null) {
  59. return;
  60. }
  61. const namedCaptureGroups = {};
  62. if (regexpuOptions.namedGroup) {
  63. regexpuOptions.onNamedGroup = (name, index) => {
  64. namedCaptureGroups[name] = index;
  65. };
  66. }
  67. node.pattern = (0, _regexpuCore.default)(node.pattern, node.flags, regexpuOptions);
  68. if (regexpuOptions.namedGroup && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
  69. path.replaceWith(_core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]));
  70. }
  71. if ((0, _features.hasFeature)(features, _features.FEATURES.unicodeFlag)) {
  72. (0, _helperRegex.pullFlag)(node, "u");
  73. }
  74. if ((0, _features.hasFeature)(features, _features.FEATURES.dotAllFlag)) {
  75. (0, _helperRegex.pullFlag)(node, "s");
  76. }
  77. }
  78. }
  79. };
  80. }
  81. function isRegExpTest(path) {
  82. return path.parentPath.isMemberExpression({
  83. object: path.node,
  84. computed: false
  85. }) && path.parentPath.get("property").isIdentifier({
  86. name: "test"
  87. });
  88. }