decorators-2018-09.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildDecoratedClass = buildDecoratedClass;
  6. var _core = require("@babel/core");
  7. var _helperReplaceSupers = require("@babel/helper-replace-supers");
  8. ;
  9. function prop(key, value) {
  10. if (!value) return null;
  11. return _core.types.objectProperty(_core.types.identifier(key), value);
  12. }
  13. function method(key, body) {
  14. return _core.types.objectMethod("method", _core.types.identifier(key), [], _core.types.blockStatement(body));
  15. }
  16. function takeDecorators(node) {
  17. let result;
  18. if (node.decorators && node.decorators.length > 0) {
  19. result = _core.types.arrayExpression(node.decorators.map(decorator => decorator.expression));
  20. }
  21. node.decorators = undefined;
  22. return result;
  23. }
  24. function getKey(node) {
  25. if (node.computed) {
  26. return node.key;
  27. } else if (_core.types.isIdentifier(node.key)) {
  28. return _core.types.stringLiteral(node.key.name);
  29. } else {
  30. return _core.types.stringLiteral(String(node.key.value));
  31. }
  32. }
  33. function extractElementDescriptor(file, classRef, superRef, path) {
  34. const isMethod = path.isClassMethod();
  35. if (path.isPrivate()) {
  36. throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
  37. }
  38. if (path.node.type === "ClassAccessorProperty") {
  39. throw path.buildCodeFrameError(`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
  40. }
  41. if (path.node.type === "StaticBlock") {
  42. throw path.buildCodeFrameError(`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
  43. }
  44. const {
  45. node,
  46. scope
  47. } = path;
  48. if (!path.isTSDeclareMethod()) {
  49. new _helperReplaceSupers.default({
  50. methodPath: path,
  51. objectRef: classRef,
  52. superRef,
  53. file,
  54. refToPreserve: classRef
  55. }).replace();
  56. }
  57. const properties = [prop("kind", _core.types.stringLiteral(_core.types.isClassMethod(node) ? node.kind : "field")), prop("decorators", takeDecorators(node)), prop("static", node.static && _core.types.booleanLiteral(true)), prop("key", getKey(node))].filter(Boolean);
  58. if (isMethod) {
  59. {
  60. var _path$ensureFunctionN;
  61. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  62. }
  63. path.ensureFunctionName(false);
  64. properties.push(prop("value", _core.types.toExpression(path.node)));
  65. } else if (_core.types.isClassProperty(node) && node.value) {
  66. properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
  67. } else {
  68. properties.push(prop("value", scope.buildUndefinedNode()));
  69. }
  70. path.remove();
  71. return _core.types.objectExpression(properties);
  72. }
  73. function addDecorateHelper(file) {
  74. return file.addHelper("decorate");
  75. }
  76. function buildDecoratedClass(ref, path, elements, file) {
  77. const {
  78. node,
  79. scope
  80. } = path;
  81. const initializeId = scope.generateUidIdentifier("initialize");
  82. const isDeclaration = node.id && path.isDeclaration();
  83. const isStrict = path.isInStrictMode();
  84. const {
  85. superClass
  86. } = node;
  87. node.type = "ClassDeclaration";
  88. if (!node.id) node.id = _core.types.cloneNode(ref);
  89. let superId;
  90. if (superClass) {
  91. superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
  92. node.superClass = superId;
  93. }
  94. const classDecorators = takeDecorators(node);
  95. const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
  96. const wrapperCall = _core.template.expression.ast`
  97. ${addDecorateHelper(file)}(
  98. ${classDecorators || _core.types.nullLiteral()},
  99. function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
  100. ${node}
  101. return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
  102. },
  103. ${superClass}
  104. )
  105. `;
  106. if (!isStrict) {
  107. wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
  108. }
  109. let replacement = wrapperCall;
  110. let classPathDesc = "arguments.1.body.body.0";
  111. if (isDeclaration) {
  112. replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
  113. classPathDesc = "declarations.0.init." + classPathDesc;
  114. }
  115. return {
  116. instanceNodes: [_core.template.statement.ast`
  117. ${_core.types.cloneNode(initializeId)}(this)
  118. `],
  119. wrapClass(path) {
  120. path.replaceWith(replacement);
  121. return path.get(classPathDesc);
  122. }
  123. };
  124. }
  125. //# sourceMappingURL=decorators-2018-09.js.map