statements.js 7.4 KB

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