statements.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.token("(");
  34. this.print(node.object, node);
  35. this.token(")");
  36. this.printBlock(node);
  37. }
  38. function IfStatement(node) {
  39. this.word("if");
  40. this.space();
  41. this.token("(");
  42. this.print(node.test, node);
  43. this.token(")");
  44. this.space();
  45. const needsBlock = node.alternate && isIfStatement(getLastStatement(node.consequent));
  46. if (needsBlock) {
  47. this.token("{");
  48. this.newline();
  49. this.indent();
  50. }
  51. this.printAndIndentOnComments(node.consequent, node);
  52. if (needsBlock) {
  53. this.dedent();
  54. this.newline();
  55. this.token("}");
  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.token("(");
  77. this.inForStatementInitCounter++;
  78. this.print(node.init, node);
  79. this.inForStatementInitCounter--;
  80. this.token(";");
  81. if (node.test) {
  82. this.space();
  83. this.print(node.test, node);
  84. }
  85. this.token(";");
  86. if (node.update) {
  87. this.space();
  88. this.print(node.update, node);
  89. }
  90. this.token(")");
  91. this.printBlock(node);
  92. }
  93. function WhileStatement(node) {
  94. this.word("while");
  95. this.space();
  96. this.token("(");
  97. this.print(node.test, node);
  98. this.token(")");
  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.token("(");
  110. this.print(node.left, node);
  111. this.space();
  112. this.word(isForOf ? "of" : "in");
  113. this.space();
  114. this.print(node.right, node);
  115. this.token(")");
  116. this.printBlock(node);
  117. }
  118. const ForInStatement = ForXStatement;
  119. exports.ForInStatement = ForInStatement;
  120. const ForOfStatement = ForXStatement;
  121. exports.ForOfStatement = ForOfStatement;
  122. function DoWhileStatement(node) {
  123. this.word("do");
  124. this.space();
  125. this.print(node.body, node);
  126. this.space();
  127. this.word("while");
  128. this.space();
  129. this.token("(");
  130. this.print(node.test, node);
  131. this.token(")");
  132. this.semicolon();
  133. }
  134. function printStatementAfterKeyword(printer, node, parent, isLabel) {
  135. if (node) {
  136. printer.space();
  137. printer.printTerminatorless(node, parent, isLabel);
  138. }
  139. printer.semicolon();
  140. }
  141. function BreakStatement(node) {
  142. this.word("break");
  143. printStatementAfterKeyword(this, node.label, node, true);
  144. }
  145. function ContinueStatement(node) {
  146. this.word("continue");
  147. printStatementAfterKeyword(this, node.label, node, true);
  148. }
  149. function ReturnStatement(node) {
  150. this.word("return");
  151. printStatementAfterKeyword(this, node.argument, node, false);
  152. }
  153. function ThrowStatement(node) {
  154. this.word("throw");
  155. printStatementAfterKeyword(this, node.argument, node, false);
  156. }
  157. function LabeledStatement(node) {
  158. this.print(node.label, node);
  159. this.token(":");
  160. this.space();
  161. this.print(node.body, node);
  162. }
  163. function TryStatement(node) {
  164. this.word("try");
  165. this.space();
  166. this.print(node.block, node);
  167. this.space();
  168. if (node.handlers) {
  169. this.print(node.handlers[0], node);
  170. } else {
  171. this.print(node.handler, node);
  172. }
  173. if (node.finalizer) {
  174. this.space();
  175. this.word("finally");
  176. this.space();
  177. this.print(node.finalizer, node);
  178. }
  179. }
  180. function CatchClause(node) {
  181. this.word("catch");
  182. this.space();
  183. if (node.param) {
  184. this.token("(");
  185. this.print(node.param, node);
  186. this.print(node.param.typeAnnotation, node);
  187. this.token(")");
  188. this.space();
  189. }
  190. this.print(node.body, node);
  191. }
  192. function SwitchStatement(node) {
  193. this.word("switch");
  194. this.space();
  195. this.token("(");
  196. this.print(node.discriminant, node);
  197. this.token(")");
  198. this.space();
  199. this.token("{");
  200. this.printSequence(node.cases, node, {
  201. indent: true,
  202. addNewlines(leading, cas) {
  203. if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
  204. }
  205. });
  206. this.token("}");
  207. }
  208. function SwitchCase(node) {
  209. if (node.test) {
  210. this.word("case");
  211. this.space();
  212. this.print(node.test, node);
  213. this.token(":");
  214. } else {
  215. this.word("default");
  216. this.token(":");
  217. }
  218. if (node.consequent.length) {
  219. this.newline();
  220. this.printSequence(node.consequent, node, {
  221. indent: true
  222. });
  223. }
  224. }
  225. function DebuggerStatement() {
  226. this.word("debugger");
  227. this.semicolon();
  228. }
  229. function variableDeclarationIndent() {
  230. this.token(",");
  231. this.newline();
  232. if (this.endsWith(10)) {
  233. for (let i = 0; i < 4; i++) this.space(true);
  234. }
  235. }
  236. function constDeclarationIndent() {
  237. this.token(",");
  238. this.newline();
  239. if (this.endsWith(10)) {
  240. for (let i = 0; i < 6; i++) this.space(true);
  241. }
  242. }
  243. function VariableDeclaration(node, parent) {
  244. if (node.declare) {
  245. this.word("declare");
  246. this.space();
  247. }
  248. this.word(node.kind);
  249. this.space();
  250. let hasInits = false;
  251. if (!isFor(parent)) {
  252. for (const declar of node.declarations) {
  253. if (declar.init) {
  254. hasInits = true;
  255. }
  256. }
  257. }
  258. let separator;
  259. if (hasInits) {
  260. separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
  261. }
  262. this.printList(node.declarations, node, {
  263. separator
  264. });
  265. if (isFor(parent)) {
  266. if (isForStatement(parent)) {
  267. if (parent.init === node) return;
  268. } else {
  269. if (parent.left === node) return;
  270. }
  271. }
  272. this.semicolon();
  273. }
  274. function VariableDeclarator(node) {
  275. this.print(node.id, node);
  276. if (node.definite) this.token("!");
  277. this.print(node.id.typeAnnotation, node);
  278. if (node.init) {
  279. this.space();
  280. this.token("=");
  281. this.space();
  282. this.print(node.init, node);
  283. }
  284. }