decorators.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. objectRef: classRef,
  53. superRef,
  54. file: this,
  55. refToPreserve: classRef
  56. }).replace();
  57. 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);
  58. if (isMethod) {
  59. const id = node.computed ? null : node.key;
  60. _core.types.toExpression(node);
  61. properties.push(prop("value", (0, _helperFunctionName.default)({
  62. node,
  63. id,
  64. scope
  65. }) || node));
  66. } else if (node.value) {
  67. properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
  68. } else {
  69. properties.push(prop("value", scope.buildUndefinedNode()));
  70. }
  71. path.remove();
  72. return _core.types.objectExpression(properties);
  73. }
  74. function addDecorateHelper(file) {
  75. try {
  76. return file.addHelper("decorate");
  77. } catch (err) {
  78. if (err.code === "BABEL_HELPER_UNKNOWN") {
  79. 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.";
  80. }
  81. throw err;
  82. }
  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).map(extractElementDescriptor.bind(file, node.id, superId)));
  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`${_core.types.cloneNode(initializeId)}(this)`],
  125. wrapClass(path) {
  126. path.replaceWith(replacement);
  127. return path.get(classPathDesc);
  128. }
  129. };
  130. }