decorators.js 5.0 KB

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