You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
7.5 KiB

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