decorators.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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(node.key.value));
  39. }
  40. }
  41. function extractElementDescriptor(file, classRef, superRef, path) {
  42. const isMethod = path.isClassMethod();
  43. if (path.isPrivate()) {
  44. throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
  45. }
  46. if (path.node.type === "ClassAccessorProperty") {
  47. throw path.buildCodeFrameError(`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
  48. }
  49. if (path.node.type === "StaticBlock") {
  50. throw path.buildCodeFrameError(`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
  51. }
  52. const {
  53. node,
  54. scope
  55. } = path;
  56. new _helperReplaceSupers.default({
  57. methodPath: path,
  58. objectRef: classRef,
  59. superRef,
  60. file,
  61. refToPreserve: classRef
  62. }).replace();
  63. 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);
  64. if (_core.types.isClassMethod(node)) {
  65. const id = node.computed ? null : node.key;
  66. const transformed = _core.types.toExpression(node);
  67. properties.push(prop("value", (0, _helperFunctionName.default)({
  68. node: transformed,
  69. id,
  70. scope
  71. }) || transformed));
  72. } else if (_core.types.isClassProperty(node) && node.value) {
  73. properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
  74. } else {
  75. properties.push(prop("value", scope.buildUndefinedNode()));
  76. }
  77. path.remove();
  78. return _core.types.objectExpression(properties);
  79. }
  80. function addDecorateHelper(file) {
  81. try {
  82. return file.addHelper("decorate");
  83. } catch (err) {
  84. if (err.code === "BABEL_HELPER_UNKNOWN") {
  85. 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.";
  86. }
  87. throw err;
  88. }
  89. }
  90. function buildDecoratedClass(ref, path, elements, file) {
  91. const {
  92. node,
  93. scope
  94. } = path;
  95. const initializeId = scope.generateUidIdentifier("initialize");
  96. const isDeclaration = node.id && path.isDeclaration();
  97. const isStrict = path.isInStrictMode();
  98. const {
  99. superClass
  100. } = node;
  101. node.type = "ClassDeclaration";
  102. if (!node.id) node.id = _core.types.cloneNode(ref);
  103. let superId;
  104. if (superClass) {
  105. superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
  106. node.superClass = superId;
  107. }
  108. const classDecorators = takeDecorators(node);
  109. const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
  110. const wrapperCall = _core.template.expression.ast`
  111. ${addDecorateHelper(file)}(
  112. ${classDecorators || _core.types.nullLiteral()},
  113. function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
  114. ${node}
  115. return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
  116. },
  117. ${superClass}
  118. )
  119. `;
  120. if (!isStrict) {
  121. wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
  122. }
  123. let replacement = wrapperCall;
  124. let classPathDesc = "arguments.1.body.body.0";
  125. if (isDeclaration) {
  126. replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
  127. classPathDesc = "declarations.0.init." + classPathDesc;
  128. }
  129. return {
  130. instanceNodes: [_core.template.statement.ast`${_core.types.cloneNode(initializeId)}(this)`],
  131. wrapClass(path) {
  132. path.replaceWith(replacement);
  133. return path.get(classPathDesc);
  134. }
  135. };
  136. }