statements.js 6.7 KB

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