classes.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (node.id) {
  35. this.space();
  36. this.print(node.id, node);
  37. }
  38. this.print(node.typeParameters, node);
  39. if (node.superClass) {
  40. this.space();
  41. this.word("extends");
  42. this.space();
  43. this.print(node.superClass, node);
  44. this.print(node.superTypeParameters, node);
  45. }
  46. if (node.implements) {
  47. this.space();
  48. this.word("implements");
  49. this.space();
  50. this.printList(node.implements, node);
  51. }
  52. this.space();
  53. this.print(node.body, node);
  54. }
  55. function ClassBody(node) {
  56. this.tokenChar(123);
  57. if (node.body.length === 0) {
  58. this.tokenChar(125);
  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.sourceWithOffset("end", node.loc, 0, -1);
  66. this.rightBrace();
  67. }
  68. }
  69. function ClassProperty(node) {
  70. var _node$key$loc, _node$key$loc$end;
  71. this.printJoin(node.decorators, node);
  72. const endLine = (_node$key$loc = node.key.loc) == null ? void 0 : (_node$key$loc$end = _node$key$loc.end) == null ? void 0 : _node$key$loc$end.line;
  73. if (endLine) this.catchUp(endLine);
  74. this.tsPrintClassMemberModifiers(node);
  75. if (node.computed) {
  76. this.tokenChar(91);
  77. this.print(node.key, node);
  78. this.tokenChar(93);
  79. } else {
  80. this._variance(node);
  81. this.print(node.key, node);
  82. }
  83. if (node.optional) {
  84. this.tokenChar(63);
  85. }
  86. if (node.definite) {
  87. this.tokenChar(33);
  88. }
  89. this.print(node.typeAnnotation, node);
  90. if (node.value) {
  91. this.space();
  92. this.tokenChar(61);
  93. this.space();
  94. this.print(node.value, node);
  95. }
  96. this.semicolon();
  97. }
  98. function ClassAccessorProperty(node) {
  99. var _node$key$loc2, _node$key$loc2$end;
  100. this.printJoin(node.decorators, node);
  101. const endLine = (_node$key$loc2 = node.key.loc) == null ? void 0 : (_node$key$loc2$end = _node$key$loc2.end) == null ? void 0 : _node$key$loc2$end.line;
  102. if (endLine) this.catchUp(endLine);
  103. this.tsPrintClassMemberModifiers(node);
  104. this.word("accessor", true);
  105. this.space();
  106. if (node.computed) {
  107. this.tokenChar(91);
  108. this.print(node.key, node);
  109. this.tokenChar(93);
  110. } else {
  111. this._variance(node);
  112. this.print(node.key, node);
  113. }
  114. if (node.optional) {
  115. this.tokenChar(63);
  116. }
  117. if (node.definite) {
  118. this.tokenChar(33);
  119. }
  120. this.print(node.typeAnnotation, node);
  121. if (node.value) {
  122. this.space();
  123. this.tokenChar(61);
  124. this.space();
  125. this.print(node.value, node);
  126. }
  127. this.semicolon();
  128. }
  129. function ClassPrivateProperty(node) {
  130. this.printJoin(node.decorators, node);
  131. if (node.static) {
  132. this.word("static");
  133. this.space();
  134. }
  135. this.print(node.key, node);
  136. this.print(node.typeAnnotation, node);
  137. if (node.value) {
  138. this.space();
  139. this.tokenChar(61);
  140. this.space();
  141. this.print(node.value, node);
  142. }
  143. this.semicolon();
  144. }
  145. function ClassMethod(node) {
  146. this._classMethodHead(node);
  147. this.space();
  148. this.print(node.body, node);
  149. }
  150. function ClassPrivateMethod(node) {
  151. this._classMethodHead(node);
  152. this.space();
  153. this.print(node.body, node);
  154. }
  155. function _classMethodHead(node) {
  156. var _node$key$loc3, _node$key$loc3$end;
  157. this.printJoin(node.decorators, node);
  158. const endLine = (_node$key$loc3 = node.key.loc) == null ? void 0 : (_node$key$loc3$end = _node$key$loc3.end) == null ? void 0 : _node$key$loc3$end.line;
  159. if (endLine) this.catchUp(endLine);
  160. this.tsPrintClassMemberModifiers(node);
  161. this._methodHead(node);
  162. }
  163. function StaticBlock(node) {
  164. this.word("static");
  165. this.space();
  166. this.tokenChar(123);
  167. if (node.body.length === 0) {
  168. this.tokenChar(125);
  169. } else {
  170. this.newline();
  171. this.printSequence(node.body, node, {
  172. indent: true
  173. });
  174. this.sourceWithOffset("end", node.loc, 0, -1);
  175. this.rightBrace();
  176. }
  177. }
  178. //# sourceMappingURL=classes.js.map