statements.js 6.8 KB

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