decorators.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.hasOwnDecorators = hasOwnDecorators;
  6. exports.hasDecorators = hasDecorators;
  7. exports.buildDecoratedClass = buildDecoratedClass;
  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(classRef, superRef, path) {
  42. const {
  43. node,
  44. scope
  45. } = path;
  46. const isMethod = path.isClassMethod();
  47. if (path.isPrivate()) {
  48. throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
  49. }
  50. new _helperReplaceSupers.default({
  51. methodPath: path,
  52. methodNode: node,
  53. objectRef: classRef,
  54. isStatic: node.static,
  55. superRef,
  56. scope,
  57. file: this,
  58. refToPreserve: classRef
  59. }, true).replace();
  60. const properties = [prop("kind", _core.types.stringLiteral(isMethod ? node.kind : "field")), prop("decorators", takeDecorators(node)), prop("static", node.static && _core.types.booleanLiteral(true)), prop("key", getKey(node))].filter(Boolean);
  61. if (isMethod) {
  62. const id = node.computed ? null : node.key;
  63. _core.types.toExpression(node);
  64. properties.push(prop("value", (0, _helperFunctionName.default)({
  65. node,
  66. id,
  67. scope
  68. }) || node));
  69. } else if (node.value) {
  70. properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
  71. } else {
  72. properties.push(prop("value", scope.buildUndefinedNode()));
  73. }
  74. path.remove();
  75. return _core.types.objectExpression(properties);
  76. }
  77. function addDecorateHelper(file) {
  78. try {
  79. return file.addHelper("decorate");
  80. } catch (err) {
  81. if (err.code === "BABEL_HELPER_UNKNOWN") {
  82. 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.";
  83. }
  84. throw err;
  85. }
  86. }
  87. function buildDecoratedClass(ref, path, elements, file) {
  88. const {
  89. node,
  90. scope
  91. } = path;
  92. const initializeId = scope.generateUidIdentifier("initialize");
  93. const isDeclaration = node.id && path.isDeclaration();
  94. const isStrict = path.isInStrictMode();
  95. const {
  96. superClass
  97. } = node;
  98. node.type = "ClassDeclaration";
  99. if (!node.id) node.id = _core.types.cloneNode(ref);
  100. let superId;
  101. if (superClass) {
  102. superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
  103. node.superClass = superId;
  104. }
  105. const classDecorators = takeDecorators(node);
  106. const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract).map(extractElementDescriptor.bind(file, node.id, superId)));
  107. let replacement = _core.template.expression.ast`
  108. ${addDecorateHelper(file)}(
  109. ${classDecorators || _core.types.nullLiteral()},
  110. function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
  111. ${node}
  112. return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
  113. },
  114. ${superClass}
  115. )
  116. `;
  117. let classPathDesc = "arguments.1.body.body.0";
  118. if (!isStrict) {
  119. replacement.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
  120. }
  121. if (isDeclaration) {
  122. replacement = _core.template.ast`let ${ref} = ${replacement}`;
  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. }