decorators-2018-09.js 4.6 KB

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