misc.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) {
  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. bareSuper.insertAfter(nodes);
  69. isFirst = false;
  70. } else {
  71. bareSuper.insertAfter(nodes.map(n => _core.types.cloneNode(n)));
  72. }
  73. }
  74. } else {
  75. constructor.get("body").unshiftContainer("body", nodes);
  76. }
  77. }
  78. function extractComputedKeys(path, computedPaths, file) {
  79. const declarations = [];
  80. const state = {
  81. classBinding: path.node.id && path.scope.getBinding(path.node.id.name),
  82. file
  83. };
  84. for (const computedPath of computedPaths) {
  85. const computedKey = computedPath.get("key");
  86. if (computedKey.isReferencedIdentifier()) {
  87. handleClassTDZ(computedKey, state);
  88. } else {
  89. computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);
  90. }
  91. const computedNode = computedPath.node;
  92. if (!computedKey.isConstantExpression()) {
  93. const ident = path.scope.generateUidIdentifierBasedOnNode(computedNode.key);
  94. path.scope.push({
  95. id: ident,
  96. kind: "let"
  97. });
  98. declarations.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(ident), computedNode.key)));
  99. computedNode.key = _core.types.cloneNode(ident);
  100. }
  101. }
  102. return declarations;
  103. }