classes.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ClassExpression = exports.ClassDeclaration = ClassDeclaration;
  6. exports.ClassBody = ClassBody;
  7. exports.ClassProperty = ClassProperty;
  8. exports.ClassPrivateProperty = ClassPrivateProperty;
  9. exports.ClassMethod = ClassMethod;
  10. exports.ClassPrivateMethod = ClassPrivateMethod;
  11. exports._classMethodHead = _classMethodHead;
  12. exports.StaticBlock = StaticBlock;
  13. var _t = require("@babel/types");
  14. const {
  15. isExportDefaultDeclaration,
  16. isExportNamedDeclaration
  17. } = _t;
  18. function ClassDeclaration(node, parent) {
  19. if (!this.format.decoratorsBeforeExport || !isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent)) {
  20. this.printJoin(node.decorators, node);
  21. }
  22. if (node.declare) {
  23. this.word("declare");
  24. this.space();
  25. }
  26. if (node.abstract) {
  27. this.word("abstract");
  28. this.space();
  29. }
  30. this.word("class");
  31. if (node.id) {
  32. this.space();
  33. this.print(node.id, node);
  34. }
  35. this.print(node.typeParameters, node);
  36. if (node.superClass) {
  37. this.space();
  38. this.word("extends");
  39. this.space();
  40. this.print(node.superClass, node);
  41. this.print(node.superTypeParameters, node);
  42. }
  43. if (node.implements) {
  44. this.space();
  45. this.word("implements");
  46. this.space();
  47. this.printList(node.implements, node);
  48. }
  49. this.space();
  50. this.print(node.body, node);
  51. }
  52. function ClassBody(node) {
  53. this.token("{");
  54. this.printInnerComments(node);
  55. if (node.body.length === 0) {
  56. this.token("}");
  57. } else {
  58. this.newline();
  59. this.indent();
  60. this.printSequence(node.body, node);
  61. this.dedent();
  62. if (!this.endsWith(10)) this.newline();
  63. this.rightBrace();
  64. }
  65. }
  66. function ClassProperty(node) {
  67. this.printJoin(node.decorators, node);
  68. this.source("end", node.key.loc);
  69. this.tsPrintClassMemberModifiers(node, true);
  70. if (node.computed) {
  71. this.token("[");
  72. this.print(node.key, node);
  73. this.token("]");
  74. } else {
  75. this._variance(node);
  76. this.print(node.key, node);
  77. }
  78. if (node.optional) {
  79. this.token("?");
  80. }
  81. if (node.definite) {
  82. this.token("!");
  83. }
  84. this.print(node.typeAnnotation, node);
  85. if (node.value) {
  86. this.space();
  87. this.token("=");
  88. this.space();
  89. this.print(node.value, node);
  90. }
  91. this.semicolon();
  92. }
  93. function ClassPrivateProperty(node) {
  94. this.printJoin(node.decorators, node);
  95. if (node.static) {
  96. this.word("static");
  97. this.space();
  98. }
  99. this.print(node.key, node);
  100. this.print(node.typeAnnotation, node);
  101. if (node.value) {
  102. this.space();
  103. this.token("=");
  104. this.space();
  105. this.print(node.value, node);
  106. }
  107. this.semicolon();
  108. }
  109. function ClassMethod(node) {
  110. this._classMethodHead(node);
  111. this.space();
  112. this.print(node.body, node);
  113. }
  114. function ClassPrivateMethod(node) {
  115. this._classMethodHead(node);
  116. this.space();
  117. this.print(node.body, node);
  118. }
  119. function _classMethodHead(node) {
  120. this.printJoin(node.decorators, node);
  121. this.source("end", node.key.loc);
  122. this.tsPrintClassMemberModifiers(node, false);
  123. this._methodHead(node);
  124. }
  125. function StaticBlock(node) {
  126. this.word("static");
  127. this.space();
  128. this.token("{");
  129. if (node.body.length === 0) {
  130. this.token("}");
  131. } else {
  132. this.newline();
  133. this.printSequence(node.body, node, {
  134. indent: true
  135. });
  136. this.rightBrace();
  137. }
  138. }