misc.js 3.3 KB

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