classes.js 4.1 KB

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