| 1 | import type Printer from "../printer"; |
| 2 | import { |
| 3 | isCallExpression, |
| 4 | isLiteral, |
| 5 | isMemberExpression, |
| 6 | isNewExpression, |
| 7 | } from "@babel/types"; |
| 8 | import type * as t from "@babel/types"; |
| 9 | import * as n from "../node"; |
| 10 | |
| 11 | export function UnaryExpression(this: Printer, node: t.UnaryExpression) { |
| 12 | if ( |
| 13 | node.operator === "void" || |
| 14 | node.operator === "delete" || |
| 15 | node.operator === "typeof" || |
| 16 | // throwExpressions |
| 17 | node.operator === "throw" |
| 18 | ) { |
| 19 | this.word(node.operator); |
| 20 | this.space(); |
| 21 | } else { |
| 22 | this.token(node.operator); |
| 23 | } |
| 24 | |
| 25 | this.print(node.argument, node); |
| 26 | } |
| 27 | |
| 28 | export function DoExpression(this: Printer, node: t.DoExpression) { |
| 29 | if (node.async) { |
| 30 | this.word("async", true); |
| 31 | this.space(); |
| 32 | } |
| 33 | this.word("do"); |
| 34 | this.space(); |
| 35 | this.print(node.body, node); |
| 36 | } |
| 37 | |
| 38 | export function ParenthesizedExpression( |
| 39 | this: Printer, |
| 40 | node: t.ParenthesizedExpression, |
| 41 | ) { |
| 42 | this.token("("); |
| 43 | this.print(node.expression, node); |
| 44 | this.token(")"); |
| 45 | } |
| 46 | |
| 47 | export function UpdateExpression(this: Printer, node: t.UpdateExpression) { |
| 48 | if (node.prefix) { |
| 49 | this.token(node.operator); |
| 50 | this.print(node.argument, node); |
| 51 | } else { |
| 52 | this.printTerminatorless(node.argument, node, true); |
| 53 | this.token(node.operator); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export function ConditionalExpression( |
| 58 | this: Printer, |
| 59 | node: t.ConditionalExpression, |
| 60 | ) { |
| 61 | this.print(node.test, node); |
| 62 | this.space(); |
| 63 | this.token("?"); |
| 64 | this.space(); |
| 65 | this.print(node.consequent, node); |
| 66 | this.space(); |
| 67 | this.token(":"); |
| 68 | this.space(); |
| 69 | this.print(node.alternate, node); |
| 70 | } |
| 71 | |
| 72 | export function NewExpression( |
| 73 | this: Printer, |
| 74 | node: t.NewExpression, |
| 75 | parent: t.Node, |
| 76 | ) { |
| 77 | this.word("new"); |
| 78 | this.space(); |
| 79 | this.print(node.callee, node); |
| 80 | if ( |
| 81 | this.format.minified && |
| 82 | node.arguments.length === 0 && |
| 83 | !node.optional && |
| 84 | !isCallExpression(parent, { callee: node }) && |
| 85 | !isMemberExpression(parent) && |
| 86 | !isNewExpression(parent) |
| 87 | ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | this.print(node.typeArguments, node); // Flow |
| 92 | this.print(node.typeParameters, node); // TS |
| 93 | |
| 94 | if (node.optional) { |
| 95 | // TODO: This can never happen |
| 96 | this.token("?."); |
| 97 | } |
| 98 | this.token("("); |
| 99 | this.printList(node.arguments, node); |
| 100 | this.token(")"); |
| 101 | } |
| 102 | |
| 103 | export function SequenceExpression(this: Printer, node: t.SequenceExpression) { |
| 104 | this.printList(node.expressions, node); |
| 105 | } |
| 106 | |
| 107 | export function ThisExpression(this: Printer) { |
| 108 | this.word("this"); |
| 109 | } |
| 110 | |
| 111 | export function Super(this: Printer) { |
| 112 | this.word("super"); |
| 113 | } |
| 114 | |
| 115 | function isDecoratorMemberExpression( |
| 116 | node: t.Expression | t.Super | t.V8IntrinsicIdentifier, |
| 117 | ): boolean { |
| 118 | switch (node.type) { |
| 119 | case "Identifier": |
| 120 | return true; |
| 121 | case "MemberExpression": |
| 122 | return ( |
| 123 | !node.computed && |
| 124 | node.property.type === "Identifier" && |
| 125 | isDecoratorMemberExpression(node.object) |
| 126 | ); |
| 127 | default: |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | function shouldParenthesizeDecoratorExpression( |
| 132 | node: t.Expression | t.Super | t.V8IntrinsicIdentifier, |
| 133 | ) { |
| 134 | if (node.type === "ParenthesizedExpression") { |
| 135 | // We didn't check extra?.parenthesized here because we don't track decorators in needsParen |
| 136 | return false; |
| 137 | } |
| 138 | return !isDecoratorMemberExpression( |
| 139 | node.type === "CallExpression" ? node.callee : node, |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | export function Decorator(this: Printer, node: t.Decorator) { |
| 144 | this.token("@"); |
| 145 | const { expression } = node; |
| 146 | if (shouldParenthesizeDecoratorExpression(expression)) { |
| 147 | this.token("("); |
| 148 | this.print(expression, node); |
| 149 | this.token(")"); |
| 150 | } else { |
| 151 | this.print(expression, node); |
| 152 | } |
| 153 | this.newline(); |
| 154 | } |
| 155 | |
| 156 | export function OptionalMemberExpression( |
| 157 | this: Printer, |
| 158 | node: t.OptionalMemberExpression, |
| 159 | ) { |
| 160 | this.print(node.object, node); |
| 161 | |
| 162 | if (!node.computed && isMemberExpression(node.property)) { |
| 163 | throw new TypeError("Got a MemberExpression for MemberExpression property"); |
| 164 | } |
| 165 | |
| 166 | let computed = node.computed; |
| 167 | // @ts-expect-error todo(flow->ts) maybe instead of typeof check specific literal types? |
| 168 | if (isLiteral(node.property) && typeof node.property.value === "number") { |
| 169 | computed = true; |
| 170 | } |
| 171 | if (node.optional) { |
| 172 | this.token("?."); |
| 173 | } |
| 174 | |
| 175 | if (computed) { |
| 176 | this.token("["); |
| 177 | this.print(node.property, node); |
| 178 | this.token("]"); |
| 179 | } else { |
| 180 | if (!node.optional) { |
| 181 | this.token("."); |
| 182 | } |
| 183 | this.print(node.property, node); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | export function OptionalCallExpression( |
| 188 | this: Printer, |
| 189 | node: t.OptionalCallExpression, |
| 190 | ) { |
| 191 | this.print(node.callee, node); |
| 192 | |
| 193 | this.print(node.typeParameters, node); // TS |
| 194 | |
| 195 | if (node.optional) { |
| 196 | this.token("?."); |
| 197 | } |
| 198 | |
| 199 | this.print(node.typeArguments, node); // Flow |
| 200 | |
| 201 | this.token("("); |
| 202 | this.printList(node.arguments, node); |
| 203 | this.token(")"); |
| 204 | } |
| 205 | |
| 206 | export function CallExpression(this: Printer, node: t.CallExpression) { |
| 207 | this.print(node.callee, node); |
| 208 | |
| 209 | this.print(node.typeArguments, node); // Flow |
| 210 | this.print(node.typeParameters, node); // TS |
| 211 | this.token("("); |
| 212 | this.printList(node.arguments, node); |
| 213 | this.token(")"); |
| 214 | } |
| 215 | |
| 216 | export function Import(this: Printer) { |
| 217 | this.word("import"); |
| 218 | } |
| 219 | |
| 220 | export function AwaitExpression(this: Printer, node: t.AwaitExpression) { |
| 221 | this.word("await"); |
| 222 | |
| 223 | if (node.argument) { |
| 224 | this.space(); |
| 225 | this.printTerminatorless(node.argument, node, false); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | export function YieldExpression(this: Printer, node: t.YieldExpression) { |
| 230 | this.word("yield", true); |
| 231 | |
| 232 | if (node.delegate) { |
| 233 | this.token("*"); |
| 234 | if (node.argument) { |
| 235 | this.space(); |
| 236 | // line terminators are allowed after yield* |
| 237 | this.print(node.argument, node); |
| 238 | } |
| 239 | } else { |
| 240 | if (node.argument) { |
| 241 | this.space(); |
| 242 | this.printTerminatorless(node.argument, node, false); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | export function EmptyStatement(this: Printer) { |
| 248 | this.semicolon(true /* force */); |
| 249 | } |
| 250 | |
| 251 | export function ExpressionStatement( |
| 252 | this: Printer, |
| 253 | node: t.ExpressionStatement, |
| 254 | ) { |
| 255 | this.print(node.expression, node); |
| 256 | this.semicolon(); |
| 257 | } |
| 258 | |
| 259 | export function AssignmentPattern(this: Printer, node: t.AssignmentPattern) { |
| 260 | this.print(node.left, node); |
| 261 | // @ts-expect-error todo(flow->ts) property present on some of the types in union but not all |
| 262 | if (node.left.optional) this.token("?"); |
| 263 | // @ts-expect-error todo(flow->ts) property present on some of the types in union but not all |
| 264 | this.print(node.left.typeAnnotation, node); |
| 265 | this.space(); |
| 266 | this.token("="); |
| 267 | this.space(); |
| 268 | this.print(node.right, node); |
| 269 | } |
| 270 | |
| 271 | export function AssignmentExpression( |
| 272 | this: Printer, |
| 273 | node: t.AssignmentExpression, |
| 274 | parent: t.Node, |
| 275 | ) { |
| 276 | // Somewhere inside a for statement `init` node but doesn't usually |
| 277 | // needs a paren except for `in` expressions: `for (a in b ? a : b;;)` |
| 278 | const parens = |
| 279 | this.inForStatementInitCounter && |
| 280 | node.operator === "in" && |
| 281 | !n.needsParens(node, parent); |
| 282 | |
| 283 | if (parens) { |
| 284 | this.token("("); |
| 285 | } |
| 286 | |
| 287 | this.print(node.left, node); |
| 288 | |
| 289 | this.space(); |
| 290 | if (node.operator === "in" || node.operator === "instanceof") { |
| 291 | this.word(node.operator); |
| 292 | } else { |
| 293 | this.token(node.operator); |
| 294 | } |
| 295 | this.space(); |
| 296 | |
| 297 | this.print(node.right, node); |
| 298 | |
| 299 | if (parens) { |
| 300 | this.token(")"); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | export function BindExpression(this: Printer, node: t.BindExpression) { |
| 305 | this.print(node.object, node); |
| 306 | this.token("::"); |
| 307 | this.print(node.callee, node); |
| 308 | } |
| 309 | |
| 310 | export { |
| 311 | AssignmentExpression as BinaryExpression, |
| 312 | AssignmentExpression as LogicalExpression, |
| 313 | }; |
| 314 | |
| 315 | export function MemberExpression(this: Printer, node: t.MemberExpression) { |
| 316 | this.print(node.object, node); |
| 317 | |
| 318 | if (!node.computed && isMemberExpression(node.property)) { |
| 319 | throw new TypeError("Got a MemberExpression for MemberExpression property"); |
| 320 | } |
| 321 | |
| 322 | let computed = node.computed; |
| 323 | // @ts-expect-error todo(flow->ts) maybe use specific literal types |
| 324 | if (isLiteral(node.property) && typeof node.property.value === "number") { |
| 325 | computed = true; |
| 326 | } |
| 327 | |
| 328 | if (computed) { |
| 329 | this.token("["); |
| 330 | this.print(node.property, node); |
| 331 | this.token("]"); |
| 332 | } else { |
| 333 | this.token("."); |
| 334 | this.print(node.property, node); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | export function MetaProperty(this: Printer, node: t.MetaProperty) { |
| 339 | this.print(node.meta, node); |
| 340 | this.token("."); |
| 341 | this.print(node.property, node); |
| 342 | } |
| 343 | |
| 344 | export function PrivateName(this: Printer, node: t.PrivateName) { |
| 345 | this.token("#"); |
| 346 | this.print(node.id, node); |
| 347 | } |
| 348 | |
| 349 | export function V8IntrinsicIdentifier( |
| 350 | this: Printer, |
| 351 | node: t.V8IntrinsicIdentifier, |
| 352 | ) { |
| 353 | this.token("%"); |
| 354 | this.word(node.name); |
| 355 | } |
| 356 | |
| 357 | export function ModuleExpression(this: Printer, node: t.ModuleExpression) { |
| 358 | this.word("module", true); |
| 359 | this.space(); |
| 360 | this.token("{"); |
| 361 | this.indent(); |
| 362 | const { body } = node; |
| 363 | if (body.body.length || body.directives.length) { |
| 364 | this.newline(); |
| 365 | } |
| 366 | this.print(body, node); |
| 367 | this.dedent(); |
| 368 | this.sourceWithOffset("end", node.loc, 0, -1); |
| 369 | this.rightBrace(); |
| 370 | } |