misc.js 4.3 KB

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