classes.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ClassBody = ClassBody;
  6. exports.ClassExpression = exports.ClassDeclaration = ClassDeclaration;
  7. exports.ClassMethod = ClassMethod;
  8. exports.ClassPrivateMethod = ClassPrivateMethod;
  9. exports.ClassPrivateProperty = ClassPrivateProperty;
  10. exports.ClassProperty = ClassProperty;
  11. exports.StaticBlock = StaticBlock;
  12. exports._classMethodHead = _classMethodHead;
  13. var _t = require("@babel/types");
  14. const {
  15. isExportDefaultDeclaration,
  16. isExportNamedDeclaration
  17. } = _t;
  18. function ClassDeclaration(node, parent) {
  19. if (!this.format.decoratorsBeforeExport || !isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent)) {
  20. this.printJoin(node.decorators, node);
  21. }
  22. if (node.declare) {
  23. this.word("declare");
  24. this.space();
  25. }
  26. if (node.abstract) {
  27. this.word("abstract");
  28. this.space();
  29. }
  30. this.word("class");
  31. this.printInnerComments(node);
  32. if (node.id) {
  33. this.space();
  34. this.print(node.id, node);
  35. }
  36. this.print(node.typeParameters, node);
  37. if (node.superClass) {
  38. this.space();
  39. this.word("extends");
  40. this.space();
  41. this.print(node.superClass, node);
  42. this.print(node.superTypeParameters, node);
  43. }
  44. if (node.implements) {
  45. this.space();
  46. this.word("implements");
  47. this.space();
  48. this.printList(node.implements, node);
  49. }
  50. this.space();
  51. this.print(node.body, node);
  52. }
  53. function ClassBody(node) {
  54. this.token("{");
  55. this.printInnerComments(node);
  56. if (node.body.length === 0) {
  57. this.token("}");
  58. } else {
  59. this.newline();
  60. this.indent();
  61. this.printSequence(node.body, node);
  62. this.dedent();
  63. if (!this.endsWith(10)) this.newline();
  64. this.rightBrace();
  65. }
  66. }
  67. function ClassProperty(node) {
  68. this.printJoin(node.decorators, node);
  69. this.source("end", node.key.loc);
  70. this.tsPrintClassMemberModifiers(node, true);
  71. if (node.computed) {
  72. this.token("[");
  73. this.print(node.key, node);
  74. this.token("]");
  75. } else {
  76. this._variance(node);
  77. this.print(node.key, node);
  78. }
  79. if (node.optional) {
  80. this.token("?");
  81. }
  82. if (node.definite) {
  83. this.token("!");
  84. }
  85. this.print(node.typeAnnotation, node);
  86. if (node.value) {
  87. this.space();
  88. this.token("=");
  89. this.space();
  90. this.print(node.value, node);
  91. }
  92. this.semicolon();
  93. }
  94. function ClassPrivateProperty(node) {
  95. this.printJoin(node.decorators, node);
  96. if (node.static) {
  97. this.word("static");
  98. this.space();
  99. }
  100. this.print(node.key, node);
  101. this.print(node.typeAnnotation, node);
  102. if (node.value) {
  103. this.space();
  104. this.token("=");
  105. this.space();
  106. this.print(node.value, node);
  107. }
  108. this.semicolon();
  109. }
  110. function ClassMethod(node) {
  111. this._classMethodHead(node);
  112. this.space();
  113. this.print(node.body, node);
  114. }
  115. function ClassPrivateMethod(node) {
  116. this._classMethodHead(node);
  117. this.space();
  118. this.print(node.body, node);
  119. }
  120. function _classMethodHead(node) {
  121. this.printJoin(node.decorators, node);
  122. this.source("end", node.key.loc);
  123. this.tsPrintClassMemberModifiers(node, false);
  124. this._methodHead(node);
  125. }
  126. function StaticBlock(node) {
  127. this.word("static");
  128. this.space();
  129. this.token("{");
  130. if (node.body.length === 0) {
  131. this.token("}");
  132. } else {
  133. this.newline();
  134. this.printSequence(node.body, node, {
  135. indent: true
  136. });
  137. this.rightBrace();
  138. }
  139. }