misc.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.extractComputedKeys = extractComputedKeys;
  6. exports.injectInitialization = injectInitialization;
  7. var _core = require("@babel/core");
  8. var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
  9. const findBareSupers = _core.traverse.visitors.merge([{
  10. Super(path) {
  11. const {
  12. node,
  13. parentPath
  14. } = path;
  15. if (parentPath.isCallExpression({
  16. callee: node
  17. })) {
  18. this.push(parentPath);
  19. }
  20. }
  21. }, _helperEnvironmentVisitor.default]);
  22. const referenceVisitor = {
  23. "TSTypeAnnotation|TypeAnnotation"(path) {
  24. path.skip();
  25. },
  26. ReferencedIdentifier(path, {
  27. scope
  28. }) {
  29. if (scope.hasOwnBinding(path.node.name)) {
  30. scope.rename(path.node.name);
  31. path.skip();
  32. }
  33. }
  34. };
  35. function handleClassTDZ(path, state) {
  36. if (state.classBinding && state.classBinding === path.scope.getBinding(path.node.name)) {
  37. const classNameTDZError = state.file.addHelper("classNameTDZError");
  38. const throwNode = _core.types.callExpression(classNameTDZError, [_core.types.stringLiteral(path.node.name)]);
  39. path.replaceWith(_core.types.sequenceExpression([throwNode, path.node]));
  40. path.skip();
  41. }
  42. }
  43. const classFieldDefinitionEvaluationTDZVisitor = {
  44. ReferencedIdentifier: handleClassTDZ
  45. };
  46. function injectInitialization(path, constructor, nodes, renamer, lastReturnsThis) {
  47. if (!nodes.length) return;
  48. const isDerived = !!path.node.superClass;
  49. if (!constructor) {
  50. const newConstructor = _core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([]));
  51. if (isDerived) {
  52. newConstructor.params = [_core.types.restElement(_core.types.identifier("args"))];
  53. newConstructor.body.body.push(_core.template.statement.ast`super(...args)`);
  54. }
  55. [constructor] = path.get("body").unshiftContainer("body", newConstructor);
  56. }
  57. if (renamer) {
  58. renamer(referenceVisitor, {
  59. scope: constructor.scope
  60. });
  61. }
  62. if (isDerived) {
  63. const bareSupers = [];
  64. constructor.traverse(findBareSupers, bareSupers);
  65. let isFirst = true;
  66. for (const bareSuper of bareSupers) {
  67. if (isFirst) {
  68. isFirst = false;
  69. } else {
  70. nodes = nodes.map(n => _core.types.cloneNode(n));
  71. }
  72. if (!bareSuper.parentPath.isExpressionStatement()) {
  73. const allNodes = [bareSuper.node, ...nodes.map(n => _core.types.toExpression(n))];
  74. if (!lastReturnsThis) allNodes.push(_core.types.thisExpression());
  75. bareSuper.replaceWith(_core.types.sequenceExpression(allNodes));
  76. } else {
  77. bareSuper.insertAfter(nodes);
  78. }
  79. }
  80. } else {
  81. constructor.get("body").unshiftContainer("body", nodes);
  82. }
  83. }
  84. function extractComputedKeys(path, computedPaths, file) {
  85. const declarations = [];
  86. const state = {
  87. classBinding: path.node.id && path.scope.getBinding(path.node.id.name),
  88. file
  89. };
  90. for (const computedPath of computedPaths) {
  91. const computedKey = computedPath.get("key");
  92. if (computedKey.isReferencedIdentifier()) {
  93. handleClassTDZ(computedKey, state);
  94. } else {
  95. computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);
  96. }
  97. const computedNode = computedPath.node;
  98. if (!computedKey.isConstantExpression()) {
  99. const scope = path.scope;
  100. const isUidReference = _core.types.isIdentifier(computedKey.node) && scope.hasUid(computedKey.node.name);
  101. const isMemoiseAssignment = computedKey.isAssignmentExpression({
  102. operator: "="
  103. }) && _core.types.isIdentifier(computedKey.node.left) && scope.hasUid(computedKey.node.left.name);
  104. if (isUidReference) {
  105. continue;
  106. } else if (isMemoiseAssignment) {
  107. declarations.push(_core.types.expressionStatement(_core.types.cloneNode(computedNode.key)));
  108. computedNode.key = _core.types.cloneNode(computedNode.key.left);
  109. } else {
  110. const ident = path.scope.generateUidIdentifierBasedOnNode(computedNode.key);
  111. scope.push({
  112. id: ident,
  113. kind: "let"
  114. });
  115. declarations.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(ident), computedNode.key)));
  116. computedNode.key = _core.types.cloneNode(ident);
  117. }
  118. }
  119. }
  120. return declarations;
  121. }
  122. //# sourceMappingURL=misc.js.map