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.
 
 
 
 

121 lines
3.2 KiB

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