statements.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.BreakStatement = BreakStatement;
  6. exports.CatchClause = CatchClause;
  7. exports.ContinueStatement = ContinueStatement;
  8. exports.DebuggerStatement = DebuggerStatement;
  9. exports.DoWhileStatement = DoWhileStatement;
  10. exports.ForOfStatement = exports.ForInStatement = void 0;
  11. exports.ForStatement = ForStatement;
  12. exports.IfStatement = IfStatement;
  13. exports.LabeledStatement = LabeledStatement;
  14. exports.ReturnStatement = ReturnStatement;
  15. exports.SwitchCase = SwitchCase;
  16. exports.SwitchStatement = SwitchStatement;
  17. exports.ThrowStatement = ThrowStatement;
  18. exports.TryStatement = TryStatement;
  19. exports.VariableDeclaration = VariableDeclaration;
  20. exports.VariableDeclarator = VariableDeclarator;
  21. exports.WhileStatement = WhileStatement;
  22. exports.WithStatement = WithStatement;
  23. var _t = require("@babel/types");
  24. const {
  25. isFor,
  26. isForStatement,
  27. isIfStatement,
  28. isStatement
  29. } = _t;
  30. function WithStatement(node) {
  31. this.word("with");
  32. this.space();
  33. this.tokenChar(40);
  34. this.print(node.object, node);
  35. this.tokenChar(41);
  36. this.printBlock(node);
  37. }
  38. function IfStatement(node) {
  39. this.word("if");
  40. this.space();
  41. this.tokenChar(40);
  42. this.print(node.test, node);
  43. this.tokenChar(41);
  44. this.space();
  45. const needsBlock = node.alternate && isIfStatement(getLastStatement(node.consequent));
  46. if (needsBlock) {
  47. this.tokenChar(123);
  48. this.newline();
  49. this.indent();
  50. }
  51. this.printAndIndentOnComments(node.consequent, node);
  52. if (needsBlock) {
  53. this.dedent();
  54. this.newline();
  55. this.tokenChar(125);
  56. }
  57. if (node.alternate) {
  58. if (this.endsWith(125)) this.space();
  59. this.word("else");
  60. this.space();
  61. this.printAndIndentOnComments(node.alternate, node);
  62. }
  63. }
  64. function getLastStatement(statement) {
  65. const {
  66. body
  67. } = statement;
  68. if (isStatement(body) === false) {
  69. return statement;
  70. }
  71. return getLastStatement(body);
  72. }
  73. function ForStatement(node) {
  74. this.word("for");
  75. this.space();
  76. this.tokenChar(40);
  77. this.inForStatementInitCounter++;
  78. this.print(node.init, node);
  79. this.inForStatementInitCounter--;
  80. this.tokenChar(59);
  81. if (node.test) {
  82. this.space();
  83. this.print(node.test, node);
  84. }
  85. this.tokenChar(59);
  86. if (node.update) {
  87. this.space();
  88. this.print(node.update, node);
  89. }
  90. this.tokenChar(41);
  91. this.printBlock(node);
  92. }
  93. function WhileStatement(node) {
  94. this.word("while");
  95. this.space();
  96. this.tokenChar(40);
  97. this.print(node.test, node);
  98. this.tokenChar(41);
  99. this.printBlock(node);
  100. }
  101. function ForXStatement(node) {
  102. this.word("for");
  103. this.space();
  104. const isForOf = node.type === "ForOfStatement";
  105. if (isForOf && node.await) {
  106. this.word("await");
  107. this.space();
  108. }
  109. this.noIndentInnerCommentsHere();
  110. this.tokenChar(40);
  111. this.print(node.left, node);
  112. this.space();
  113. this.word(isForOf ? "of" : "in");
  114. this.space();
  115. this.print(node.right, node);
  116. this.tokenChar(41);
  117. this.printBlock(node);
  118. }
  119. const ForInStatement = exports.ForInStatement = ForXStatement;
  120. const ForOfStatement = exports.ForOfStatement = ForXStatement;
  121. function DoWhileStatement(node) {
  122. this.word("do");
  123. this.space();
  124. this.print(node.body, node);
  125. this.space();
  126. this.word("while");
  127. this.space();
  128. this.tokenChar(40);
  129. this.print(node.test, node);
  130. this.tokenChar(41);
  131. this.semicolon();
  132. }
  133. function printStatementAfterKeyword(printer, node, parent, isLabel) {
  134. if (node) {
  135. printer.space();
  136. printer.printTerminatorless(node, parent, isLabel);
  137. }
  138. printer.semicolon();
  139. }
  140. function BreakStatement(node) {
  141. this.word("break");
  142. printStatementAfterKeyword(this, node.label, node, true);
  143. }
  144. function ContinueStatement(node) {
  145. this.word("continue");
  146. printStatementAfterKeyword(this, node.label, node, true);
  147. }
  148. function ReturnStatement(node) {
  149. this.word("return");
  150. printStatementAfterKeyword(this, node.argument, node, false);
  151. }
  152. function ThrowStatement(node) {
  153. this.word("throw");
  154. printStatementAfterKeyword(this, node.argument, node, false);
  155. }
  156. function LabeledStatement(node) {
  157. this.print(node.label, node);
  158. this.tokenChar(58);
  159. this.space();
  160. this.print(node.body, node);
  161. }
  162. function TryStatement(node) {
  163. this.word("try");
  164. this.space();
  165. this.print(node.block, node);
  166. this.space();
  167. if (node.handlers) {
  168. this.print(node.handlers[0], node);
  169. } else {
  170. this.print(node.handler, node);
  171. }
  172. if (node.finalizer) {
  173. this.space();
  174. this.word("finally");
  175. this.space();
  176. this.print(node.finalizer, node);
  177. }
  178. }
  179. function CatchClause(node) {
  180. this.word("catch");
  181. this.space();
  182. if (node.param) {
  183. this.tokenChar(40);
  184. this.print(node.param, node);
  185. this.print(node.param.typeAnnotation, node);
  186. this.tokenChar(41);
  187. this.space();
  188. }
  189. this.print(node.body, node);
  190. }
  191. function SwitchStatement(node) {
  192. this.word("switch");
  193. this.space();
  194. this.tokenChar(40);
  195. this.print(node.discriminant, node);
  196. this.tokenChar(41);
  197. this.space();
  198. this.tokenChar(123);
  199. this.printSequence(node.cases, node, {
  200. indent: true,
  201. addNewlines(leading, cas) {
  202. if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
  203. }
  204. });
  205. this.rightBrace(node);
  206. }
  207. function SwitchCase(node) {
  208. if (node.test) {
  209. this.word("case");
  210. this.space();
  211. this.print(node.test, node);
  212. this.tokenChar(58);
  213. } else {
  214. this.word("default");
  215. this.tokenChar(58);
  216. }
  217. if (node.consequent.length) {
  218. this.newline();
  219. this.printSequence(node.consequent, node, {
  220. indent: true
  221. });
  222. }
  223. }
  224. function DebuggerStatement() {
  225. this.word("debugger");
  226. this.semicolon();
  227. }
  228. function VariableDeclaration(node, parent) {
  229. if (node.declare) {
  230. this.word("declare");
  231. this.space();
  232. }
  233. const {
  234. kind
  235. } = node;
  236. this.word(kind, kind === "using" || kind === "await using");
  237. this.space();
  238. let hasInits = false;
  239. if (!isFor(parent)) {
  240. for (const declar of node.declarations) {
  241. if (declar.init) {
  242. hasInits = true;
  243. }
  244. }
  245. }
  246. this.printList(node.declarations, node, {
  247. separator: hasInits ? function () {
  248. this.tokenChar(44);
  249. this.newline();
  250. } : undefined,
  251. indent: node.declarations.length > 1 ? true : false
  252. });
  253. if (isFor(parent)) {
  254. if (isForStatement(parent)) {
  255. if (parent.init === node) return;
  256. } else {
  257. if (parent.left === node) return;
  258. }
  259. }
  260. this.semicolon();
  261. }
  262. function VariableDeclarator(node) {
  263. this.print(node.id, node);
  264. if (node.definite) this.tokenChar(33);
  265. this.print(node.id.typeAnnotation, node);
  266. if (node.init) {
  267. this.space();
  268. this.tokenChar(61);
  269. this.space();
  270. this.print(node.init, node);
  271. }
  272. }
  273. //# sourceMappingURL=statements.js.map