classes.js 5.5 KB

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