classes.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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);
  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);
  36. }
  37. this.print(node.typeParameters);
  38. if (node.superClass) {
  39. this.space();
  40. this.word("extends");
  41. this.space();
  42. this.print(node.superClass);
  43. this.print(node.superTypeParameters);
  44. }
  45. if (node.implements) {
  46. this.space();
  47. this.word("implements");
  48. this.space();
  49. this.printList(node.implements);
  50. }
  51. this.space();
  52. this.print(node.body);
  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. const separator = classBodyEmptySemicolonsPrinter(this, node);
  61. separator == null || separator(-1);
  62. const exit = this.enterDelimited();
  63. this.printJoin(node.body, {
  64. statement: true,
  65. indent: true,
  66. separator,
  67. printTrailingSeparator: true
  68. });
  69. exit();
  70. if (!this.endsWith(10)) this.newline();
  71. this.rightBrace(node);
  72. }
  73. }
  74. function classBodyEmptySemicolonsPrinter(printer, node) {
  75. if (!printer.tokenMap || node.start == null || node.end == null) {
  76. return null;
  77. }
  78. const indexes = printer.tokenMap.getIndexes(node);
  79. if (!indexes) return null;
  80. let k = 1;
  81. let occurrenceCount = 0;
  82. let nextLocIndex = 0;
  83. const advanceNextLocIndex = () => {
  84. while (nextLocIndex < node.body.length && node.body[nextLocIndex].start == null) {
  85. nextLocIndex++;
  86. }
  87. };
  88. advanceNextLocIndex();
  89. return i => {
  90. if (nextLocIndex <= i) {
  91. nextLocIndex = i + 1;
  92. advanceNextLocIndex();
  93. }
  94. const end = nextLocIndex === node.body.length ? node.end : node.body[nextLocIndex].start;
  95. let tok;
  96. while (k < indexes.length && printer.tokenMap.matchesOriginal(tok = printer._tokens[indexes[k]], ";") && tok.start < end) {
  97. printer.token(";", undefined, occurrenceCount++);
  98. k++;
  99. }
  100. };
  101. }
  102. function ClassProperty(node) {
  103. this.printJoin(node.decorators);
  104. if (!node.static && !this.format.preserveFormat) {
  105. var _node$key$loc;
  106. const endLine = (_node$key$loc = node.key.loc) == null || (_node$key$loc = _node$key$loc.end) == null ? void 0 : _node$key$loc.line;
  107. if (endLine) this.catchUp(endLine);
  108. }
  109. this.tsPrintClassMemberModifiers(node);
  110. if (node.computed) {
  111. this.tokenChar(91);
  112. this.print(node.key);
  113. this.tokenChar(93);
  114. } else {
  115. this._variance(node);
  116. this.print(node.key);
  117. }
  118. if (node.optional) {
  119. this.tokenChar(63);
  120. }
  121. if (node.definite) {
  122. this.tokenChar(33);
  123. }
  124. this.print(node.typeAnnotation);
  125. if (node.value) {
  126. this.space();
  127. this.tokenChar(61);
  128. this.space();
  129. this.print(node.value);
  130. }
  131. this.semicolon();
  132. }
  133. function ClassAccessorProperty(node) {
  134. var _node$key$loc2;
  135. this.printJoin(node.decorators);
  136. const endLine = (_node$key$loc2 = node.key.loc) == null || (_node$key$loc2 = _node$key$loc2.end) == null ? void 0 : _node$key$loc2.line;
  137. if (endLine) this.catchUp(endLine);
  138. this.tsPrintClassMemberModifiers(node);
  139. this.word("accessor", true);
  140. this.space();
  141. if (node.computed) {
  142. this.tokenChar(91);
  143. this.print(node.key);
  144. this.tokenChar(93);
  145. } else {
  146. this._variance(node);
  147. this.print(node.key);
  148. }
  149. if (node.optional) {
  150. this.tokenChar(63);
  151. }
  152. if (node.definite) {
  153. this.tokenChar(33);
  154. }
  155. this.print(node.typeAnnotation);
  156. if (node.value) {
  157. this.space();
  158. this.tokenChar(61);
  159. this.space();
  160. this.print(node.value);
  161. }
  162. this.semicolon();
  163. }
  164. function ClassPrivateProperty(node) {
  165. this.printJoin(node.decorators);
  166. if (node.static) {
  167. this.word("static");
  168. this.space();
  169. }
  170. this.print(node.key);
  171. this.print(node.typeAnnotation);
  172. if (node.value) {
  173. this.space();
  174. this.tokenChar(61);
  175. this.space();
  176. this.print(node.value);
  177. }
  178. this.semicolon();
  179. }
  180. function ClassMethod(node) {
  181. this._classMethodHead(node);
  182. this.space();
  183. this.print(node.body);
  184. }
  185. function ClassPrivateMethod(node) {
  186. this._classMethodHead(node);
  187. this.space();
  188. this.print(node.body);
  189. }
  190. function _classMethodHead(node) {
  191. this.printJoin(node.decorators);
  192. if (!this.format.preserveFormat) {
  193. var _node$key$loc3;
  194. const endLine = (_node$key$loc3 = node.key.loc) == null || (_node$key$loc3 = _node$key$loc3.end) == null ? void 0 : _node$key$loc3.line;
  195. if (endLine) this.catchUp(endLine);
  196. }
  197. this.tsPrintClassMemberModifiers(node);
  198. this._methodHead(node);
  199. }
  200. function StaticBlock(node) {
  201. this.word("static");
  202. this.space();
  203. this.tokenChar(123);
  204. if (node.body.length === 0) {
  205. this.tokenChar(125);
  206. } else {
  207. this.newline();
  208. this.printSequence(node.body, {
  209. indent: true
  210. });
  211. this.rightBrace(node);
  212. }
  213. }
  214. //# sourceMappingURL=classes.js.map