misc.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. };
  47. function injectInitialization(path, constructor, nodes, renamer, lastReturnsThis) {
  48. if (!nodes.length) return;
  49. const isDerived = !!path.node.superClass;
  50. if (!constructor) {
  51. const newConstructor = _core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([]));
  52. if (isDerived) {
  53. newConstructor.params = [_core.types.restElement(_core.types.identifier("args"))];
  54. newConstructor.body.body.push(_core.template.statement.ast`super(...args)`);
  55. }
  56. [constructor] = path.get("body").unshiftContainer("body", newConstructor);
  57. }
  58. if (renamer) {
  59. renamer(referenceVisitor, {
  60. scope: constructor.scope
  61. });
  62. }
  63. if (isDerived) {
  64. const bareSupers = [];
  65. constructor.traverse(findBareSupers, bareSupers);
  66. let isFirst = true;
  67. for (const bareSuper of bareSupers) {
  68. if (isFirst) {
  69. isFirst = false;
  70. } else {
  71. nodes = nodes.map(n => _core.types.cloneNode(n));
  72. }
  73. if (!bareSuper.parentPath.isExpressionStatement()) {
  74. const allNodes = [bareSuper.node, ...nodes.map(n => _core.types.toExpression(n))];
  75. if (!lastReturnsThis) allNodes.push(_core.types.thisExpression());
  76. bareSuper.replaceWith(_core.types.sequenceExpression(allNodes));
  77. } else {
  78. bareSuper.insertAfter(nodes);
  79. }
  80. }
  81. } else {
  82. constructor.get("body").unshiftContainer("body", nodes);
  83. }
  84. }
  85. function memoiseComputedKey(keyNode, scope, hint) {
  86. const isUidReference = _core.types.isIdentifier(keyNode) && scope.hasUid(keyNode.name);
  87. if (isUidReference) {
  88. return;
  89. }
  90. const isMemoiseAssignment = _core.types.isAssignmentExpression(keyNode, {
  91. operator: "="
  92. }) && _core.types.isIdentifier(keyNode.left) && scope.hasUid(keyNode.left.name);
  93. if (isMemoiseAssignment) {
  94. return _core.types.cloneNode(keyNode);
  95. } else {
  96. const ident = _core.types.identifier(hint);
  97. scope.push({
  98. id: ident,
  99. kind: "let"
  100. });
  101. return _core.types.assignmentExpression("=", _core.types.cloneNode(ident), keyNode);
  102. }
  103. }
  104. function extractComputedKeys(path, computedPaths, file) {
  105. const {
  106. scope
  107. } = path;
  108. const declarations = [];
  109. const state = {
  110. classBinding: path.node.id && scope.getBinding(path.node.id.name),
  111. file
  112. };
  113. for (const computedPath of computedPaths) {
  114. const computedKey = computedPath.get("key");
  115. if (computedKey.isReferencedIdentifier()) {
  116. handleClassTDZ(computedKey, state);
  117. } else {
  118. computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);
  119. }
  120. const computedNode = computedPath.node;
  121. if (!computedKey.isConstantExpression()) {
  122. const assignment = memoiseComputedKey(computedKey.node, scope, scope.generateUidBasedOnNode(computedKey.node));
  123. if (assignment) {
  124. declarations.push(_core.types.expressionStatement(assignment));
  125. computedNode.key = _core.types.cloneNode(assignment.left);
  126. }
  127. }
  128. }
  129. return declarations;
  130. }
  131. //# sourceMappingURL=misc.js.map