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