decorators.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. return !!(node.decorators && node.decorators.length);
  13. }
  14. function hasDecorators(node) {
  15. return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
  16. }
  17. function prop(key, value) {
  18. if (!value) return null;
  19. return _core.types.objectProperty(_core.types.identifier(key), value);
  20. }
  21. function method(key, body) {
  22. return _core.types.objectMethod("method", _core.types.identifier(key), [], _core.types.blockStatement(body));
  23. }
  24. function takeDecorators(node) {
  25. let result;
  26. if (node.decorators && node.decorators.length > 0) {
  27. result = _core.types.arrayExpression(node.decorators.map(decorator => decorator.expression));
  28. }
  29. node.decorators = undefined;
  30. return result;
  31. }
  32. function getKey(node) {
  33. if (node.computed) {
  34. return node.key;
  35. } else if (_core.types.isIdentifier(node.key)) {
  36. return _core.types.stringLiteral(node.key.name);
  37. } else {
  38. return _core.types.stringLiteral(String(
  39. 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. try {
  85. return file.addHelper("decorate");
  86. } catch (err) {
  87. if (err.code === "BABEL_HELPER_UNKNOWN") {
  88. err.message += "\n '@babel/plugin-transform-decorators' in non-legacy mode" + " requires '@babel/core' version ^7.0.2 and you appear to be using" + " an older version.";
  89. }
  90. throw err;
  91. }
  92. }
  93. function buildDecoratedClass(ref, path, elements, file) {
  94. const {
  95. node,
  96. scope
  97. } = path;
  98. const initializeId = scope.generateUidIdentifier("initialize");
  99. const isDeclaration = node.id && path.isDeclaration();
  100. const isStrict = path.isInStrictMode();
  101. const {
  102. superClass
  103. } = node;
  104. node.type = "ClassDeclaration";
  105. if (!node.id) node.id = _core.types.cloneNode(ref);
  106. let superId;
  107. if (superClass) {
  108. superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
  109. node.superClass = superId;
  110. }
  111. const classDecorators = takeDecorators(node);
  112. const definitions = _core.types.arrayExpression(elements.filter(element =>
  113. !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId,
  114. path)));
  115. const wrapperCall = _core.template.expression.ast`
  116. ${addDecorateHelper(file)}(
  117. ${classDecorators || _core.types.nullLiteral()},
  118. function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
  119. ${node}
  120. return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
  121. },
  122. ${superClass}
  123. )
  124. `;
  125. if (!isStrict) {
  126. wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
  127. }
  128. let replacement = wrapperCall;
  129. let classPathDesc = "arguments.1.body.body.0";
  130. if (isDeclaration) {
  131. replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
  132. classPathDesc = "declarations.0.init." + classPathDesc;
  133. }
  134. return {
  135. instanceNodes: [_core.template.statement.ast`${_core.types.cloneNode(initializeId)}(this)`],
  136. wrapClass(path) {
  137. path.replaceWith(replacement);
  138. return path.get(classPathDesc);
  139. }
  140. };
  141. }
  142. //# sourceMappingURL=decorators.js.map