misc.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.injectInitialization = injectInitialization;
  6. exports.extractComputedKeys = extractComputedKeys;
  7. function _core() {
  8. const data = require("@babel/core");
  9. _core = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _helperReplaceSupers() {
  15. const data = require("@babel/helper-replace-supers");
  16. _helperReplaceSupers = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. const findBareSupers = _core().traverse.visitors.merge([{
  22. Super(path) {
  23. const {
  24. node,
  25. parentPath
  26. } = path;
  27. if (parentPath.isCallExpression({
  28. callee: node
  29. })) {
  30. this.push(parentPath);
  31. }
  32. }
  33. }, _helperReplaceSupers().environmentVisitor]);
  34. const referenceVisitor = {
  35. "TSTypeAnnotation|TypeAnnotation"(path) {
  36. path.skip();
  37. },
  38. ReferencedIdentifier(path) {
  39. if (this.scope.hasOwnBinding(path.node.name)) {
  40. this.scope.rename(path.node.name);
  41. path.skip();
  42. }
  43. }
  44. };
  45. const classFieldDefinitionEvaluationTDZVisitor = _core().traverse.visitors.merge([{
  46. ReferencedIdentifier(path) {
  47. if (this.classBinding && this.classBinding === path.scope.getBinding(path.node.name)) {
  48. const classNameTDZError = this.file.addHelper("classNameTDZError");
  49. const throwNode = _core().types.callExpression(classNameTDZError, [_core().types.stringLiteral(path.node.name)]);
  50. path.replaceWith(_core().types.sequenceExpression([throwNode, path.node]));
  51. path.skip();
  52. }
  53. }
  54. }, _helperReplaceSupers().environmentVisitor]);
  55. function injectInitialization(path, constructor, nodes, renamer) {
  56. if (!nodes.length) return;
  57. const isDerived = !!path.node.superClass;
  58. if (!constructor) {
  59. const newConstructor = _core().types.classMethod("constructor", _core().types.identifier("constructor"), [], _core().types.blockStatement([]));
  60. if (isDerived) {
  61. newConstructor.params = [_core().types.restElement(_core().types.identifier("args"))];
  62. newConstructor.body.body.push(_core().template.statement.ast`super(...args)`);
  63. }
  64. [constructor] = path.get("body").unshiftContainer("body", newConstructor);
  65. }
  66. if (renamer) {
  67. renamer(referenceVisitor, {
  68. scope: constructor.scope
  69. });
  70. }
  71. if (isDerived) {
  72. const bareSupers = [];
  73. constructor.traverse(findBareSupers, bareSupers);
  74. for (const bareSuper of bareSupers) {
  75. bareSuper.insertAfter(nodes);
  76. }
  77. } else {
  78. constructor.get("body").unshiftContainer("body", nodes);
  79. }
  80. }
  81. function extractComputedKeys(ref, path, computedPaths, file) {
  82. const declarations = [];
  83. for (const computedPath of computedPaths) {
  84. computedPath.traverse(classFieldDefinitionEvaluationTDZVisitor, {
  85. classBinding: path.node.id && path.scope.getBinding(path.node.id.name),
  86. file
  87. });
  88. const computedNode = computedPath.node;
  89. if (!computedPath.get("key").isConstantExpression()) {
  90. const ident = path.scope.generateUidIdentifierBasedOnNode(computedNode.key);
  91. declarations.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(ident, computedNode.key)]));
  92. computedNode.key = _core().types.cloneNode(ident);
  93. }
  94. }
  95. return declarations;
  96. }