| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.BreakStatement = BreakStatement;
|
|---|
| 7 | exports.CatchClause = CatchClause;
|
|---|
| 8 | exports.ContinueStatement = ContinueStatement;
|
|---|
| 9 | exports.DebuggerStatement = DebuggerStatement;
|
|---|
| 10 | exports.DoWhileStatement = DoWhileStatement;
|
|---|
| 11 | exports.ForInStatement = ForInStatement;
|
|---|
| 12 | exports.ForOfStatement = ForOfStatement;
|
|---|
| 13 | exports.ForStatement = ForStatement;
|
|---|
| 14 | exports.IfStatement = IfStatement;
|
|---|
| 15 | exports.LabeledStatement = LabeledStatement;
|
|---|
| 16 | exports.ReturnStatement = ReturnStatement;
|
|---|
| 17 | exports.SwitchCase = SwitchCase;
|
|---|
| 18 | exports.SwitchStatement = SwitchStatement;
|
|---|
| 19 | exports.ThrowStatement = ThrowStatement;
|
|---|
| 20 | exports.TryStatement = TryStatement;
|
|---|
| 21 | exports.VariableDeclaration = VariableDeclaration;
|
|---|
| 22 | exports.VariableDeclarator = VariableDeclarator;
|
|---|
| 23 | exports.WhileStatement = WhileStatement;
|
|---|
| 24 | exports.WithStatement = WithStatement;
|
|---|
| 25 | var _t = require("@babel/types");
|
|---|
| 26 | var _index = require("../node/index.js");
|
|---|
| 27 | const {
|
|---|
| 28 | isFor,
|
|---|
| 29 | isIfStatement,
|
|---|
| 30 | isStatement
|
|---|
| 31 | } = _t;
|
|---|
| 32 | function WithStatement(node) {
|
|---|
| 33 | this.word("with");
|
|---|
| 34 | this.space();
|
|---|
| 35 | this.tokenChar(40);
|
|---|
| 36 | this.print(node.object);
|
|---|
| 37 | this.tokenChar(41);
|
|---|
| 38 | this.printBlock(node.body);
|
|---|
| 39 | }
|
|---|
| 40 | function IfStatement(node) {
|
|---|
| 41 | this.word("if");
|
|---|
| 42 | this.space();
|
|---|
| 43 | this.tokenChar(40);
|
|---|
| 44 | this.print(node.test);
|
|---|
| 45 | this.tokenChar(41);
|
|---|
| 46 | this.space();
|
|---|
| 47 | const needsBlock = node.alternate && isIfStatement(getLastStatement(node.consequent));
|
|---|
| 48 | if (needsBlock) {
|
|---|
| 49 | this.tokenChar(123);
|
|---|
| 50 | this.newline();
|
|---|
| 51 | this.indent();
|
|---|
| 52 | }
|
|---|
| 53 | this.printAndIndentOnComments(node.consequent);
|
|---|
| 54 | if (needsBlock) {
|
|---|
| 55 | this.dedent();
|
|---|
| 56 | this.newline();
|
|---|
| 57 | this.tokenChar(125);
|
|---|
| 58 | }
|
|---|
| 59 | if (node.alternate) {
|
|---|
| 60 | if (this.endsWith(125)) this.space();
|
|---|
| 61 | this.word("else");
|
|---|
| 62 | this.space();
|
|---|
| 63 | this.printAndIndentOnComments(node.alternate);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | function getLastStatement(statement) {
|
|---|
| 67 | const {
|
|---|
| 68 | body
|
|---|
| 69 | } = statement;
|
|---|
| 70 | if (isStatement(body) === false) {
|
|---|
| 71 | return statement;
|
|---|
| 72 | }
|
|---|
| 73 | return getLastStatement(body);
|
|---|
| 74 | }
|
|---|
| 75 | function ForStatement(node) {
|
|---|
| 76 | this.word("for");
|
|---|
| 77 | this.space();
|
|---|
| 78 | this.tokenChar(40);
|
|---|
| 79 | this.tokenContext |= _index.TokenContext.forInitHead | _index.TokenContext.forInOrInitHeadAccumulate;
|
|---|
| 80 | this.print(node.init);
|
|---|
| 81 | this.tokenContext = _index.TokenContext.normal;
|
|---|
| 82 | this.tokenChar(59);
|
|---|
| 83 | if (node.test) {
|
|---|
| 84 | this.space();
|
|---|
| 85 | this.print(node.test);
|
|---|
| 86 | }
|
|---|
| 87 | this.tokenChar(59, 1);
|
|---|
| 88 | if (node.update) {
|
|---|
| 89 | this.space();
|
|---|
| 90 | this.print(node.update);
|
|---|
| 91 | }
|
|---|
| 92 | this.tokenChar(41);
|
|---|
| 93 | this.printBlock(node.body);
|
|---|
| 94 | }
|
|---|
| 95 | function WhileStatement(node) {
|
|---|
| 96 | this.word("while");
|
|---|
| 97 | this.space();
|
|---|
| 98 | this.tokenChar(40);
|
|---|
| 99 | this.print(node.test);
|
|---|
| 100 | this.tokenChar(41);
|
|---|
| 101 | this.printBlock(node.body);
|
|---|
| 102 | }
|
|---|
| 103 | function ForInStatement(node) {
|
|---|
| 104 | this.word("for");
|
|---|
| 105 | this.space();
|
|---|
| 106 | this.noIndentInnerCommentsHere();
|
|---|
| 107 | this.tokenChar(40);
|
|---|
| 108 | this.tokenContext |= _index.TokenContext.forInHead | _index.TokenContext.forInOrInitHeadAccumulate;
|
|---|
| 109 | this.print(node.left);
|
|---|
| 110 | this.tokenContext = _index.TokenContext.normal;
|
|---|
| 111 | this.space();
|
|---|
| 112 | this.word("in");
|
|---|
| 113 | this.space();
|
|---|
| 114 | this.print(node.right);
|
|---|
| 115 | this.tokenChar(41);
|
|---|
| 116 | this.printBlock(node.body);
|
|---|
| 117 | }
|
|---|
| 118 | function ForOfStatement(node) {
|
|---|
| 119 | this.word("for");
|
|---|
| 120 | this.space();
|
|---|
| 121 | if (node.await) {
|
|---|
| 122 | this.word("await");
|
|---|
| 123 | this.space();
|
|---|
| 124 | }
|
|---|
| 125 | this.noIndentInnerCommentsHere();
|
|---|
| 126 | this.tokenChar(40);
|
|---|
| 127 | this.tokenContext |= _index.TokenContext.forOfHead;
|
|---|
| 128 | this.print(node.left);
|
|---|
| 129 | this.space();
|
|---|
| 130 | this.word("of");
|
|---|
| 131 | this.space();
|
|---|
| 132 | this.print(node.right);
|
|---|
| 133 | this.tokenChar(41);
|
|---|
| 134 | this.printBlock(node.body);
|
|---|
| 135 | }
|
|---|
| 136 | function DoWhileStatement(node) {
|
|---|
| 137 | this.word("do");
|
|---|
| 138 | this.space();
|
|---|
| 139 | this.print(node.body);
|
|---|
| 140 | this.space();
|
|---|
| 141 | this.word("while");
|
|---|
| 142 | this.space();
|
|---|
| 143 | this.tokenChar(40);
|
|---|
| 144 | this.print(node.test);
|
|---|
| 145 | this.tokenChar(41);
|
|---|
| 146 | this.semicolon();
|
|---|
| 147 | }
|
|---|
| 148 | function printStatementAfterKeyword(printer, node) {
|
|---|
| 149 | if (node) {
|
|---|
| 150 | printer.space();
|
|---|
| 151 | printer.printTerminatorless(node);
|
|---|
| 152 | }
|
|---|
| 153 | printer.semicolon();
|
|---|
| 154 | }
|
|---|
| 155 | function BreakStatement(node) {
|
|---|
| 156 | this.word("break");
|
|---|
| 157 | printStatementAfterKeyword(this, node.label);
|
|---|
| 158 | }
|
|---|
| 159 | function ContinueStatement(node) {
|
|---|
| 160 | this.word("continue");
|
|---|
| 161 | printStatementAfterKeyword(this, node.label);
|
|---|
| 162 | }
|
|---|
| 163 | function ReturnStatement(node) {
|
|---|
| 164 | this.word("return");
|
|---|
| 165 | printStatementAfterKeyword(this, node.argument);
|
|---|
| 166 | }
|
|---|
| 167 | function ThrowStatement(node) {
|
|---|
| 168 | this.word("throw");
|
|---|
| 169 | printStatementAfterKeyword(this, node.argument);
|
|---|
| 170 | }
|
|---|
| 171 | function LabeledStatement(node) {
|
|---|
| 172 | this.print(node.label);
|
|---|
| 173 | this.tokenChar(58);
|
|---|
| 174 | this.space();
|
|---|
| 175 | this.print(node.body);
|
|---|
| 176 | }
|
|---|
| 177 | function TryStatement(node) {
|
|---|
| 178 | this.word("try");
|
|---|
| 179 | this.space();
|
|---|
| 180 | this.print(node.block);
|
|---|
| 181 | this.space();
|
|---|
| 182 | if (node.handlers) {
|
|---|
| 183 | this.print(node.handlers[0]);
|
|---|
| 184 | } else {
|
|---|
| 185 | this.print(node.handler);
|
|---|
| 186 | }
|
|---|
| 187 | if (node.finalizer) {
|
|---|
| 188 | this.space();
|
|---|
| 189 | this.word("finally");
|
|---|
| 190 | this.space();
|
|---|
| 191 | this.print(node.finalizer);
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | function CatchClause(node) {
|
|---|
| 195 | this.word("catch");
|
|---|
| 196 | this.space();
|
|---|
| 197 | if (node.param) {
|
|---|
| 198 | this.tokenChar(40);
|
|---|
| 199 | this.print(node.param);
|
|---|
| 200 | this.print(node.param.typeAnnotation);
|
|---|
| 201 | this.tokenChar(41);
|
|---|
| 202 | this.space();
|
|---|
| 203 | }
|
|---|
| 204 | this.print(node.body);
|
|---|
| 205 | }
|
|---|
| 206 | function SwitchStatement(node) {
|
|---|
| 207 | this.word("switch");
|
|---|
| 208 | this.space();
|
|---|
| 209 | this.tokenChar(40);
|
|---|
| 210 | this.print(node.discriminant);
|
|---|
| 211 | this.tokenChar(41);
|
|---|
| 212 | this.space();
|
|---|
| 213 | this.tokenChar(123);
|
|---|
| 214 | this.printSequence(node.cases, true);
|
|---|
| 215 | this.rightBrace(node);
|
|---|
| 216 | }
|
|---|
| 217 | function SwitchCase(node) {
|
|---|
| 218 | if (node.test) {
|
|---|
| 219 | this.word("case");
|
|---|
| 220 | this.space();
|
|---|
| 221 | this.print(node.test);
|
|---|
| 222 | this.tokenChar(58);
|
|---|
| 223 | } else {
|
|---|
| 224 | this.word("default");
|
|---|
| 225 | this.tokenChar(58);
|
|---|
| 226 | }
|
|---|
| 227 | if (node.consequent.length) {
|
|---|
| 228 | this.newline();
|
|---|
| 229 | this.printSequence(node.consequent, true);
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | function DebuggerStatement() {
|
|---|
| 233 | this.word("debugger");
|
|---|
| 234 | this.semicolon();
|
|---|
| 235 | }
|
|---|
| 236 | function commaSeparatorWithNewline(occurrenceCount) {
|
|---|
| 237 | this.tokenChar(44, occurrenceCount);
|
|---|
| 238 | this.newline();
|
|---|
| 239 | }
|
|---|
| 240 | function VariableDeclaration(node, parent) {
|
|---|
| 241 | if (node.declare) {
|
|---|
| 242 | this.word("declare");
|
|---|
| 243 | this.space();
|
|---|
| 244 | }
|
|---|
| 245 | const {
|
|---|
| 246 | kind
|
|---|
| 247 | } = node;
|
|---|
| 248 | switch (kind) {
|
|---|
| 249 | case "await using":
|
|---|
| 250 | this.word("await");
|
|---|
| 251 | this.space();
|
|---|
| 252 | case "using":
|
|---|
| 253 | this.word("using", true);
|
|---|
| 254 | break;
|
|---|
| 255 | default:
|
|---|
| 256 | this.word(kind);
|
|---|
| 257 | }
|
|---|
| 258 | this.space();
|
|---|
| 259 | let hasInits = false;
|
|---|
| 260 | if (!isFor(parent)) {
|
|---|
| 261 | for (const declar of node.declarations) {
|
|---|
| 262 | if (declar.init) {
|
|---|
| 263 | hasInits = true;
|
|---|
| 264 | break;
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 | this.printList(node.declarations, undefined, undefined, node.declarations.length > 1, hasInits ? commaSeparatorWithNewline : undefined);
|
|---|
| 269 | if (parent != null) {
|
|---|
| 270 | switch (parent.type) {
|
|---|
| 271 | case "ForStatement":
|
|---|
| 272 | if (parent.init === node) {
|
|---|
| 273 | return;
|
|---|
| 274 | }
|
|---|
| 275 | break;
|
|---|
| 276 | case "ForInStatement":
|
|---|
| 277 | case "ForOfStatement":
|
|---|
| 278 | if (parent.left === node) {
|
|---|
| 279 | return;
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | this.semicolon();
|
|---|
| 284 | }
|
|---|
| 285 | function VariableDeclarator(node) {
|
|---|
| 286 | this.print(node.id);
|
|---|
| 287 | if (node.definite) this.tokenChar(33);
|
|---|
| 288 | this.print(node.id.typeAnnotation);
|
|---|
| 289 | if (node.init) {
|
|---|
| 290 | this.space();
|
|---|
| 291 | this.tokenChar(61);
|
|---|
| 292 | this.space();
|
|---|
| 293 | this.print(node.init);
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | //# sourceMappingURL=statements.js.map
|
|---|