statements.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. var data = _interopRequireWildcard(require("@babel/types"));
  21. t = function t() {
  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. var 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. var buildForXStatement = function buildForXStatement(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. var ForInStatement = buildForXStatement("in");
  112. exports.ForInStatement = ForInStatement;
  113. var 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) {
  128. if (key === void 0) {
  129. key = "label";
  130. }
  131. return function (node) {
  132. this.word(prefix);
  133. var label = node[key];
  134. if (label) {
  135. this.space();
  136. var isLabel = key == "label";
  137. var terminatorState = this.startTerminatorless(isLabel);
  138. this.print(label, node);
  139. this.endTerminatorless(terminatorState);
  140. }
  141. this.semicolon();
  142. };
  143. }
  144. var ContinueStatement = buildLabelStatement("continue");
  145. exports.ContinueStatement = ContinueStatement;
  146. var ReturnStatement = buildLabelStatement("return", "argument");
  147. exports.ReturnStatement = ReturnStatement;
  148. var BreakStatement = buildLabelStatement("break");
  149. exports.BreakStatement = BreakStatement;
  150. var 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.token(")");
  182. this.space();
  183. }
  184. this.print(node.body, node);
  185. }
  186. function SwitchStatement(node) {
  187. this.word("switch");
  188. this.space();
  189. this.token("(");
  190. this.print(node.discriminant, node);
  191. this.token(")");
  192. this.space();
  193. this.token("{");
  194. this.printSequence(node.cases, node, {
  195. indent: true,
  196. addNewlines: function addNewlines(leading, cas) {
  197. if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
  198. }
  199. });
  200. this.token("}");
  201. }
  202. function SwitchCase(node) {
  203. if (node.test) {
  204. this.word("case");
  205. this.space();
  206. this.print(node.test, node);
  207. this.token(":");
  208. } else {
  209. this.word("default");
  210. this.token(":");
  211. }
  212. if (node.consequent.length) {
  213. this.newline();
  214. this.printSequence(node.consequent, node, {
  215. indent: true
  216. });
  217. }
  218. }
  219. function DebuggerStatement() {
  220. this.word("debugger");
  221. this.semicolon();
  222. }
  223. function variableDeclarationIndent() {
  224. this.token(",");
  225. this.newline();
  226. if (this.endsWith("\n")) for (var i = 0; i < 4; i++) {
  227. this.space(true);
  228. }
  229. }
  230. function constDeclarationIndent() {
  231. this.token(",");
  232. this.newline();
  233. if (this.endsWith("\n")) for (var i = 0; i < 6; i++) {
  234. this.space(true);
  235. }
  236. }
  237. function VariableDeclaration(node, parent) {
  238. if (node.declare) {
  239. this.word("declare");
  240. this.space();
  241. }
  242. this.word(node.kind);
  243. this.space();
  244. var hasInits = false;
  245. if (!t().isFor(parent)) {
  246. var _arr = node.declarations;
  247. for (var _i = 0; _i < _arr.length; _i++) {
  248. var declar = _arr[_i];
  249. if (declar.init) {
  250. hasInits = true;
  251. }
  252. }
  253. }
  254. var separator;
  255. if (hasInits) {
  256. separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
  257. }
  258. this.printList(node.declarations, node, {
  259. separator: separator
  260. });
  261. if (t().isFor(parent)) {
  262. if (parent.left === node || parent.init === node) return;
  263. }
  264. this.semicolon();
  265. }
  266. function VariableDeclarator(node) {
  267. this.print(node.id, node);
  268. if (node.definite) this.token("!");
  269. this.print(node.id.typeAnnotation, node);
  270. if (node.init) {
  271. this.space();
  272. this.token("=");
  273. this.space();
  274. this.print(node.init, node);
  275. }
  276. }