statements.js 6.9 KB

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