statements.js 6.6 KB

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