statements.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. function t() {
  20. const data = _interopRequireWildcard(require("@babel/types"));
  21. t = function () {
  22. return data;
  23. };
  24. return data;
  25. }
  26. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  27. function WithStatement(node) {
  28. this.word("with");
  29. this.space();
  30. this.token("(");
  31. this.print(node.object, node);
  32. this.token(")");
  33. this.printBlock(node);
  34. }
  35. function IfStatement(node) {
  36. this.word("if");
  37. this.space();
  38. this.token("(");
  39. this.print(node.test, node);
  40. this.token(")");
  41. this.space();
  42. const needsBlock = node.alternate && t().isIfStatement(getLastStatement(node.consequent));
  43. if (needsBlock) {
  44. this.token("{");
  45. this.newline();
  46. this.indent();
  47. }
  48. this.printAndIndentOnComments(node.consequent, node);
  49. if (needsBlock) {
  50. this.dedent();
  51. this.newline();
  52. this.token("}");
  53. }
  54. if (node.alternate) {
  55. if (this.endsWith("}")) this.space();
  56. this.word("else");
  57. this.space();
  58. this.printAndIndentOnComments(node.alternate, node);
  59. }
  60. }
  61. function getLastStatement(statement) {
  62. if (!t().isStatement(statement.body)) return statement;
  63. return getLastStatement(statement.body);
  64. }
  65. function ForStatement(node) {
  66. this.word("for");
  67. this.space();
  68. this.token("(");
  69. this.inForStatementInitCounter++;
  70. this.print(node.init, node);
  71. this.inForStatementInitCounter--;
  72. this.token(";");
  73. if (node.test) {
  74. this.space();
  75. this.print(node.test, node);
  76. }
  77. this.token(";");
  78. if (node.update) {
  79. this.space();
  80. this.print(node.update, node);
  81. }
  82. this.token(")");
  83. this.printBlock(node);
  84. }
  85. function WhileStatement(node) {
  86. this.word("while");
  87. this.space();
  88. this.token("(");
  89. this.print(node.test, node);
  90. this.token(")");
  91. this.printBlock(node);
  92. }
  93. const buildForXStatement = function (op) {
  94. return function (node) {
  95. this.word("for");
  96. this.space();
  97. if (op === "of" && node.await) {
  98. this.word("await");
  99. this.space();
  100. }
  101. this.token("(");
  102. this.print(node.left, node);
  103. this.space();
  104. this.word(op);
  105. this.space();
  106. this.print(node.right, node);
  107. this.token(")");
  108. this.printBlock(node);
  109. };
  110. };
  111. const ForInStatement = buildForXStatement("in");
  112. exports.ForInStatement = ForInStatement;
  113. const ForOfStatement = buildForXStatement("of");
  114. exports.ForOfStatement = ForOfStatement;
  115. function DoWhileStatement(node) {
  116. this.word("do");
  117. this.space();
  118. this.print(node.body, node);
  119. this.space();
  120. this.word("while");
  121. this.space();
  122. this.token("(");
  123. this.print(node.test, node);
  124. this.token(")");
  125. this.semicolon();
  126. }
  127. function buildLabelStatement(prefix, key = "label") {
  128. return function (node) {
  129. this.word(prefix);
  130. const label = node[key];
  131. if (label) {
  132. this.space();
  133. const isLabel = key == "label";
  134. const terminatorState = this.startTerminatorless(isLabel);
  135. this.print(label, node);
  136. this.endTerminatorless(terminatorState);
  137. }
  138. this.semicolon();
  139. };
  140. }
  141. const ContinueStatement = buildLabelStatement("continue");
  142. exports.ContinueStatement = ContinueStatement;
  143. const ReturnStatement = buildLabelStatement("return", "argument");
  144. exports.ReturnStatement = ReturnStatement;
  145. const BreakStatement = buildLabelStatement("break");
  146. exports.BreakStatement = BreakStatement;
  147. const ThrowStatement = buildLabelStatement("throw", "argument");
  148. exports.ThrowStatement = ThrowStatement;
  149. function LabeledStatement(node) {
  150. this.print(node.label, node);
  151. this.token(":");
  152. this.space();
  153. this.print(node.body, node);
  154. }
  155. function TryStatement(node) {
  156. this.word("try");
  157. this.space();
  158. this.print(node.block, node);
  159. this.space();
  160. if (node.handlers) {
  161. this.print(node.handlers[0], node);
  162. } else {
  163. this.print(node.handler, node);
  164. }
  165. if (node.finalizer) {
  166. this.space();
  167. this.word("finally");
  168. this.space();
  169. this.print(node.finalizer, node);
  170. }
  171. }
  172. function CatchClause(node) {
  173. this.word("catch");
  174. this.space();
  175. if (node.param) {
  176. this.token("(");
  177. this.print(node.param, 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("\n")) for (let i = 0; i < 4; i++) this.space(true);
  224. }
  225. function constDeclarationIndent() {
  226. this.token(",");
  227. this.newline();
  228. if (this.endsWith("\n")) for (let i = 0; i < 6; i++) this.space(true);
  229. }
  230. function VariableDeclaration(node, parent) {
  231. if (node.declare) {
  232. this.word("declare");
  233. this.space();
  234. }
  235. this.word(node.kind);
  236. this.space();
  237. let hasInits = false;
  238. if (!t().isFor(parent)) {
  239. for (const declar of node.declarations) {
  240. if (declar.init) {
  241. hasInits = true;
  242. }
  243. }
  244. }
  245. let separator;
  246. if (hasInits) {
  247. separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
  248. }
  249. this.printList(node.declarations, node, {
  250. separator
  251. });
  252. if (t().isFor(parent)) {
  253. if (parent.left === node || parent.init === node) return;
  254. }
  255. this.semicolon();
  256. }
  257. function VariableDeclarator(node) {
  258. this.print(node.id, node);
  259. if (node.definite) this.token("!");
  260. this.print(node.id.typeAnnotation, node);
  261. if (node.init) {
  262. this.space();
  263. this.token("=");
  264. this.space();
  265. this.print(node.init, node);
  266. }
  267. }