statements.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.ThrowStatement = exports.BreakStatement = exports.ReturnStatement = exports.ContinueStatement = 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. this.token("(");
  94. this.print(node.left, node);
  95. this.space();
  96. this.word(op);
  97. this.space();
  98. this.print(node.right, node);
  99. this.token(")");
  100. this.printBlock(node);
  101. };
  102. };
  103. var ForInStatement = exports.ForInStatement = buildForXStatement("in");
  104. var ForOfStatement = exports.ForOfStatement = buildForXStatement("of");
  105. function DoWhileStatement(node) {
  106. this.word("do");
  107. this.space();
  108. this.print(node.body, node);
  109. this.space();
  110. this.word("while");
  111. this.space();
  112. this.token("(");
  113. this.print(node.test, node);
  114. this.token(")");
  115. this.semicolon();
  116. }
  117. function buildLabelStatement(prefix) {
  118. var key = arguments.length <= 1 || arguments[1] === undefined ? "label" : arguments[1];
  119. return function (node) {
  120. this.word(prefix);
  121. var label = node[key];
  122. if (label) {
  123. this.space();
  124. var terminatorState = this.startTerminatorless();
  125. this.print(label, node);
  126. this.endTerminatorless(terminatorState);
  127. }
  128. this.semicolon();
  129. };
  130. }
  131. var ContinueStatement = exports.ContinueStatement = buildLabelStatement("continue");
  132. var ReturnStatement = exports.ReturnStatement = buildLabelStatement("return", "argument");
  133. var BreakStatement = exports.BreakStatement = buildLabelStatement("break");
  134. var ThrowStatement = exports.ThrowStatement = buildLabelStatement("throw", "argument");
  135. function LabeledStatement(node) {
  136. this.print(node.label, node);
  137. this.token(":");
  138. this.space();
  139. this.print(node.body, node);
  140. }
  141. function TryStatement(node) {
  142. this.word("try");
  143. this.space();
  144. this.print(node.block, node);
  145. this.space();
  146. if (node.handlers) {
  147. this.print(node.handlers[0], node);
  148. } else {
  149. this.print(node.handler, node);
  150. }
  151. if (node.finalizer) {
  152. this.space();
  153. this.word("finally");
  154. this.space();
  155. this.print(node.finalizer, node);
  156. }
  157. }
  158. function CatchClause(node) {
  159. this.word("catch");
  160. this.space();
  161. this.token("(");
  162. this.print(node.param, node);
  163. this.token(")");
  164. this.space();
  165. this.print(node.body, node);
  166. }
  167. function SwitchStatement(node) {
  168. this.word("switch");
  169. this.space();
  170. this.token("(");
  171. this.print(node.discriminant, node);
  172. this.token(")");
  173. this.space();
  174. this.token("{");
  175. this.printSequence(node.cases, node, {
  176. indent: true,
  177. addNewlines: function addNewlines(leading, cas) {
  178. if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
  179. }
  180. });
  181. this.token("}");
  182. }
  183. function SwitchCase(node) {
  184. if (node.test) {
  185. this.word("case");
  186. this.space();
  187. this.print(node.test, node);
  188. this.token(":");
  189. } else {
  190. this.word("default");
  191. this.token(":");
  192. }
  193. if (node.consequent.length) {
  194. this.newline();
  195. this.printSequence(node.consequent, node, { indent: true });
  196. }
  197. }
  198. function DebuggerStatement() {
  199. this.word("debugger");
  200. this.semicolon();
  201. }
  202. function variableDeclarationIdent() {
  203. this.token(",");
  204. this.newline();
  205. if (this.endsWith("\n")) for (var i = 0; i < 4; i++) {
  206. this.space(true);
  207. }
  208. }
  209. function constDeclarationIdent() {
  210. this.token(",");
  211. this.newline();
  212. if (this.endsWith("\n")) for (var i = 0; i < 6; i++) {
  213. this.space(true);
  214. }
  215. }
  216. function VariableDeclaration(node, parent) {
  217. this.word(node.kind);
  218. this.space();
  219. var hasInits = false;
  220. if (!t.isFor(parent)) {
  221. for (var _iterator = node.declarations, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  222. var _ref;
  223. if (_isArray) {
  224. if (_i >= _iterator.length) break;
  225. _ref = _iterator[_i++];
  226. } else {
  227. _i = _iterator.next();
  228. if (_i.done) break;
  229. _ref = _i.value;
  230. }
  231. var declar = _ref;
  232. if (declar.init) {
  233. hasInits = true;
  234. }
  235. }
  236. }
  237. var separator = void 0;
  238. if (hasInits) {
  239. separator = node.kind === "const" ? constDeclarationIdent : variableDeclarationIdent;
  240. }
  241. this.printList(node.declarations, node, { separator: separator });
  242. if (t.isFor(parent)) {
  243. if (parent.left === node || parent.init === node) return;
  244. }
  245. this.semicolon();
  246. }
  247. function VariableDeclarator(node) {
  248. this.print(node.id, node);
  249. this.print(node.id.typeAnnotation, node);
  250. if (node.init) {
  251. this.space();
  252. this.token("=");
  253. this.space();
  254. this.print(node.init, node);
  255. }
  256. }