classes.js 4.1 KB

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