web 3d图形渲染器
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.

119 lines
3.1 KiB

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