classes.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const inExport = isExportDefaultDeclaration(parent) || isExportNamedDeclaration(parent);
  21. if (!inExport || !this._shouldPrintDecoratorsBeforeExport(parent)) {
  22. this.printJoin(node.decorators, node);
  23. }
  24. if (node.declare) {
  25. this.word("declare");
  26. this.space();
  27. }
  28. if (node.abstract) {
  29. this.word("abstract");
  30. this.space();
  31. }
  32. this.word("class");
  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.tokenChar(123);
  56. if (node.body.length === 0) {
  57. this.tokenChar(125);
  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.sourceWithOffset("end", node.loc, 0, -1);
  65. this.rightBrace();
  66. }
  67. }
  68. function ClassProperty(node) {
  69. var _node$key$loc, _node$key$loc$end;
  70. this.printJoin(node.decorators, node);
  71. 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;
  72. if (endLine) this.catchUp(endLine);
  73. this.tsPrintClassMemberModifiers(node);
  74. if (node.computed) {
  75. this.tokenChar(91);
  76. this.print(node.key, node);
  77. this.tokenChar(93);
  78. } else {
  79. this._variance(node);
  80. this.print(node.key, node);
  81. }
  82. if (node.optional) {
  83. this.tokenChar(63);
  84. }
  85. if (node.definite) {
  86. this.tokenChar(33);
  87. }
  88. this.print(node.typeAnnotation, node);
  89. if (node.value) {
  90. this.space();
  91. this.tokenChar(61);
  92. this.space();
  93. this.print(node.value, node);
  94. }
  95. this.semicolon();
  96. }
  97. function ClassAccessorProperty(node) {
  98. var _node$key$loc2, _node$key$loc2$end;
  99. this.printJoin(node.decorators, node);
  100. 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;
  101. if (endLine) this.catchUp(endLine);
  102. this.tsPrintClassMemberModifiers(node);
  103. this.word("accessor", true);
  104. this.space();
  105. if (node.computed) {
  106. this.tokenChar(91);
  107. this.print(node.key, node);
  108. this.tokenChar(93);
  109. } else {
  110. this._variance(node);
  111. this.print(node.key, node);
  112. }
  113. if (node.optional) {
  114. this.tokenChar(63);
  115. }
  116. if (node.definite) {
  117. this.tokenChar(33);
  118. }
  119. this.print(node.typeAnnotation, node);
  120. if (node.value) {
  121. this.space();
  122. this.tokenChar(61);
  123. this.space();
  124. this.print(node.value, node);
  125. }
  126. this.semicolon();
  127. }
  128. function ClassPrivateProperty(node) {
  129. this.printJoin(node.decorators, node);
  130. if (node.static) {
  131. this.word("static");
  132. this.space();
  133. }
  134. this.print(node.key, node);
  135. this.print(node.typeAnnotation, node);
  136. if (node.value) {
  137. this.space();
  138. this.tokenChar(61);
  139. this.space();
  140. this.print(node.value, node);
  141. }
  142. this.semicolon();
  143. }
  144. function ClassMethod(node) {
  145. this._classMethodHead(node);
  146. this.space();
  147. this.print(node.body, node);
  148. }
  149. function ClassPrivateMethod(node) {
  150. this._classMethodHead(node);
  151. this.space();
  152. this.print(node.body, node);
  153. }
  154. function _classMethodHead(node) {
  155. var _node$key$loc3, _node$key$loc3$end;
  156. this.printJoin(node.decorators, node);
  157. 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;
  158. if (endLine) this.catchUp(endLine);
  159. this.tsPrintClassMemberModifiers(node);
  160. this._methodHead(node);
  161. }
  162. function StaticBlock(node) {
  163. this.word("static");
  164. this.space();
  165. this.tokenChar(123);
  166. if (node.body.length === 0) {
  167. this.tokenChar(125);
  168. } else {
  169. this.newline();
  170. this.printSequence(node.body, node, {
  171. indent: true
  172. });
  173. this.sourceWithOffset("end", node.loc, 0, -1);
  174. this.rightBrace();
  175. }
  176. }
  177. //# sourceMappingURL=classes.js.map