statements.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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);
  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);
  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);
  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);
  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. {
  78. const exit = this.enterForStatementInit();
  79. this.print(node.init);
  80. exit();
  81. }
  82. this.tokenChar(59);
  83. if (node.test) {
  84. this.space();
  85. this.print(node.test);
  86. }
  87. this.token(";", false, 1);
  88. if (node.update) {
  89. this.space();
  90. this.print(node.update);
  91. }
  92. this.tokenChar(41);
  93. this.printBlock(node);
  94. }
  95. function WhileStatement(node) {
  96. this.word("while");
  97. this.space();
  98. this.tokenChar(40);
  99. this.print(node.test);
  100. this.tokenChar(41);
  101. this.printBlock(node);
  102. }
  103. function ForXStatement(node) {
  104. this.word("for");
  105. this.space();
  106. const isForOf = node.type === "ForOfStatement";
  107. if (isForOf && node.await) {
  108. this.word("await");
  109. this.space();
  110. }
  111. this.noIndentInnerCommentsHere();
  112. this.tokenChar(40);
  113. {
  114. const exit = this.enterForXStatementInit(isForOf);
  115. this.print(node.left);
  116. exit == null || exit();
  117. }
  118. this.space();
  119. this.word(isForOf ? "of" : "in");
  120. this.space();
  121. this.print(node.right);
  122. this.tokenChar(41);
  123. this.printBlock(node);
  124. }
  125. const ForInStatement = exports.ForInStatement = ForXStatement;
  126. const ForOfStatement = exports.ForOfStatement = ForXStatement;
  127. function DoWhileStatement(node) {
  128. this.word("do");
  129. this.space();
  130. this.print(node.body);
  131. this.space();
  132. this.word("while");
  133. this.space();
  134. this.tokenChar(40);
  135. this.print(node.test);
  136. this.tokenChar(41);
  137. this.semicolon();
  138. }
  139. function printStatementAfterKeyword(printer, node) {
  140. if (node) {
  141. printer.space();
  142. printer.printTerminatorless(node);
  143. }
  144. printer.semicolon();
  145. }
  146. function BreakStatement(node) {
  147. this.word("break");
  148. printStatementAfterKeyword(this, node.label);
  149. }
  150. function ContinueStatement(node) {
  151. this.word("continue");
  152. printStatementAfterKeyword(this, node.label);
  153. }
  154. function ReturnStatement(node) {
  155. this.word("return");
  156. printStatementAfterKeyword(this, node.argument);
  157. }
  158. function ThrowStatement(node) {
  159. this.word("throw");
  160. printStatementAfterKeyword(this, node.argument);
  161. }
  162. function LabeledStatement(node) {
  163. this.print(node.label);
  164. this.tokenChar(58);
  165. this.space();
  166. this.print(node.body);
  167. }
  168. function TryStatement(node) {
  169. this.word("try");
  170. this.space();
  171. this.print(node.block);
  172. this.space();
  173. if (node.handlers) {
  174. this.print(node.handlers[0]);
  175. } else {
  176. this.print(node.handler);
  177. }
  178. if (node.finalizer) {
  179. this.space();
  180. this.word("finally");
  181. this.space();
  182. this.print(node.finalizer);
  183. }
  184. }
  185. function CatchClause(node) {
  186. this.word("catch");
  187. this.space();
  188. if (node.param) {
  189. this.tokenChar(40);
  190. this.print(node.param);
  191. this.print(node.param.typeAnnotation);
  192. this.tokenChar(41);
  193. this.space();
  194. }
  195. this.print(node.body);
  196. }
  197. function SwitchStatement(node) {
  198. this.word("switch");
  199. this.space();
  200. this.tokenChar(40);
  201. this.print(node.discriminant);
  202. this.tokenChar(41);
  203. this.space();
  204. this.tokenChar(123);
  205. this.printSequence(node.cases, true, undefined, function addNewlines(leading, cas) {
  206. if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
  207. });
  208. this.rightBrace(node);
  209. }
  210. function SwitchCase(node) {
  211. if (node.test) {
  212. this.word("case");
  213. this.space();
  214. this.print(node.test);
  215. this.tokenChar(58);
  216. } else {
  217. this.word("default");
  218. this.tokenChar(58);
  219. }
  220. if (node.consequent.length) {
  221. this.newline();
  222. this.printSequence(node.consequent, true);
  223. }
  224. }
  225. function DebuggerStatement() {
  226. this.word("debugger");
  227. this.semicolon();
  228. }
  229. function VariableDeclaration(node, parent) {
  230. if (node.declare) {
  231. this.word("declare");
  232. this.space();
  233. }
  234. const {
  235. kind
  236. } = node;
  237. if (kind === "await using") {
  238. this.word("await");
  239. this.space();
  240. this.word("using", true);
  241. } else {
  242. this.word(kind, kind === "using");
  243. }
  244. this.space();
  245. let hasInits = false;
  246. if (!isFor(parent)) {
  247. for (const declar of node.declarations) {
  248. if (declar.init) {
  249. hasInits = true;
  250. }
  251. }
  252. }
  253. this.printList(node.declarations, undefined, undefined, node.declarations.length > 1, hasInits ? function (occurrenceCount) {
  254. this.token(",", false, occurrenceCount);
  255. this.newline();
  256. } : undefined);
  257. if (isFor(parent)) {
  258. if (isForStatement(parent)) {
  259. if (parent.init === node) return;
  260. } else {
  261. if (parent.left === node) return;
  262. }
  263. }
  264. this.semicolon();
  265. }
  266. function VariableDeclarator(node) {
  267. this.print(node.id);
  268. if (node.definite) this.tokenChar(33);
  269. this.print(node.id.typeAnnotation);
  270. if (node.init) {
  271. this.space();
  272. this.tokenChar(61);
  273. this.space();
  274. this.print(node.init);
  275. }
  276. }
  277. //# sourceMappingURL=statements.js.map