decorators-2018-09.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. function hasOwnDecorators(node) {
  11. var _node$decorators;
  12. return !!((_node$decorators = node.decorators) != null && _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. if (!path.isTSDeclareMethod()) {
  57. new _helperReplaceSupers.default({
  58. methodPath: path,
  59. objectRef: classRef,
  60. superRef,
  61. file,
  62. refToPreserve: classRef
  63. }).replace();
  64. }
  65. 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);
  66. if (isMethod) {
  67. {
  68. var _path$ensureFunctionN;
  69. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  70. }
  71. path.ensureFunctionName(false);
  72. properties.push(prop("value", _core.types.toExpression(path.node)));
  73. } else if (_core.types.isClassProperty(node) && node.value) {
  74. properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
  75. } else {
  76. properties.push(prop("value", scope.buildUndefinedNode()));
  77. }
  78. path.remove();
  79. return _core.types.objectExpression(properties);
  80. }
  81. function addDecorateHelper(file) {
  82. return file.addHelper("decorate");
  83. }
  84. function buildDecoratedClass(ref, path, elements, file) {
  85. const {
  86. node,
  87. scope
  88. } = path;
  89. const initializeId = scope.generateUidIdentifier("initialize");
  90. const isDeclaration = node.id && path.isDeclaration();
  91. const isStrict = path.isInStrictMode();
  92. const {
  93. superClass
  94. } = node;
  95. node.type = "ClassDeclaration";
  96. if (!node.id) node.id = _core.types.cloneNode(ref);
  97. let superId;
  98. if (superClass) {
  99. superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
  100. node.superClass = superId;
  101. }
  102. const classDecorators = takeDecorators(node);
  103. const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
  104. const wrapperCall = _core.template.expression.ast`
  105. ${addDecorateHelper(file)}(
  106. ${classDecorators || _core.types.nullLiteral()},
  107. function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
  108. ${node}
  109. return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
  110. },
  111. ${superClass}
  112. )
  113. `;
  114. if (!isStrict) {
  115. wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
  116. }
  117. let replacement = wrapperCall;
  118. let classPathDesc = "arguments.1.body.body.0";
  119. if (isDeclaration) {
  120. replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
  121. classPathDesc = "declarations.0.init." + classPathDesc;
  122. }
  123. return {
  124. instanceNodes: [_core.template.statement.ast`
  125. ${_core.types.cloneNode(initializeId)}(this)
  126. `],
  127. wrapClass(path) {
  128. path.replaceWith(replacement);
  129. return path.get(classPathDesc);
  130. }
  131. };
  132. }
  133. //# sourceMappingURL=decorators-2018-09.js.map