statements.js 6.8 KB

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