| 1 | import { |
| 2 | isArrayTypeAnnotation, |
| 3 | isArrowFunctionExpression, |
| 4 | isAssignmentExpression, |
| 5 | isAwaitExpression, |
| 6 | isBinary, |
| 7 | isBinaryExpression, |
| 8 | isUpdateExpression, |
| 9 | isCallExpression, |
| 10 | isClass, |
| 11 | isClassExpression, |
| 12 | isConditional, |
| 13 | isConditionalExpression, |
| 14 | isExportDeclaration, |
| 15 | isExportDefaultDeclaration, |
| 16 | isExpressionStatement, |
| 17 | isFor, |
| 18 | isForInStatement, |
| 19 | isForOfStatement, |
| 20 | isForStatement, |
| 21 | isFunctionExpression, |
| 22 | isIfStatement, |
| 23 | isIndexedAccessType, |
| 24 | isIntersectionTypeAnnotation, |
| 25 | isLogicalExpression, |
| 26 | isMemberExpression, |
| 27 | isNewExpression, |
| 28 | isNullableTypeAnnotation, |
| 29 | isObjectPattern, |
| 30 | isOptionalCallExpression, |
| 31 | isOptionalMemberExpression, |
| 32 | isReturnStatement, |
| 33 | isSequenceExpression, |
| 34 | isSwitchStatement, |
| 35 | isTSArrayType, |
| 36 | isTSAsExpression, |
| 37 | isTSInstantiationExpression, |
| 38 | isTSIntersectionType, |
| 39 | isTSNonNullExpression, |
| 40 | isTSOptionalType, |
| 41 | isTSRestType, |
| 42 | isTSTypeAssertion, |
| 43 | isTSUnionType, |
| 44 | isTaggedTemplateExpression, |
| 45 | isThrowStatement, |
| 46 | isTypeAnnotation, |
| 47 | isUnaryLike, |
| 48 | isUnionTypeAnnotation, |
| 49 | isVariableDeclarator, |
| 50 | isWhileStatement, |
| 51 | isYieldExpression, |
| 52 | isTSSatisfiesExpression, |
| 53 | } from "@babel/types"; |
| 54 | import type * as t from "@babel/types"; |
| 55 | const PRECEDENCE = { |
| 56 | "||": 0, |
| 57 | "??": 0, |
| 58 | "|>": 0, |
| 59 | "&&": 1, |
| 60 | "|": 2, |
| 61 | "^": 3, |
| 62 | "&": 4, |
| 63 | "==": 5, |
| 64 | "===": 5, |
| 65 | "!=": 5, |
| 66 | "!==": 5, |
| 67 | "<": 6, |
| 68 | ">": 6, |
| 69 | "<=": 6, |
| 70 | ">=": 6, |
| 71 | in: 6, |
| 72 | instanceof: 6, |
| 73 | ">>": 7, |
| 74 | "<<": 7, |
| 75 | ">>>": 7, |
| 76 | "+": 8, |
| 77 | "-": 8, |
| 78 | "*": 9, |
| 79 | "/": 9, |
| 80 | "%": 9, |
| 81 | "**": 10, |
| 82 | }; |
| 83 | |
| 84 | const enum CheckParam { |
| 85 | expressionStatement = 1 << 0, |
| 86 | arrowBody = 1 << 1, |
| 87 | exportDefault = 1 << 2, |
| 88 | forHead = 1 << 3, |
| 89 | forInHead = 1 << 4, |
| 90 | forOfHead = 1 << 5, |
| 91 | } |
| 92 | |
| 93 | function isTSTypeExpression(node: t.Node) { |
| 94 | return ( |
| 95 | isTSAsExpression(node) || |
| 96 | isTSSatisfiesExpression(node) || |
| 97 | isTSTypeAssertion(node) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | const isClassExtendsClause = ( |
| 102 | node: t.Node, |
| 103 | parent: t.Node, |
| 104 | ): parent is t.Class => isClass(parent, { superClass: node }); |
| 105 | |
| 106 | const hasPostfixPart = (node: t.Node, parent: t.Node) => |
| 107 | ((isMemberExpression(parent) || isOptionalMemberExpression(parent)) && |
| 108 | parent.object === node) || |
| 109 | ((isCallExpression(parent) || |
| 110 | isOptionalCallExpression(parent) || |
| 111 | isNewExpression(parent)) && |
| 112 | parent.callee === node) || |
| 113 | (isTaggedTemplateExpression(parent) && parent.tag === node) || |
| 114 | isTSNonNullExpression(parent); |
| 115 | |
| 116 | export function NullableTypeAnnotation( |
| 117 | node: t.NullableTypeAnnotation, |
| 118 | parent: t.Node, |
| 119 | ): boolean { |
| 120 | return isArrayTypeAnnotation(parent); |
| 121 | } |
| 122 | |
| 123 | export function FunctionTypeAnnotation( |
| 124 | node: t.FunctionTypeAnnotation, |
| 125 | parent: t.Node, |
| 126 | printStack: Array<t.Node>, |
| 127 | ): boolean { |
| 128 | if (printStack.length < 3) return; |
| 129 | |
| 130 | return ( |
| 131 | // (() => A) | (() => B) |
| 132 | isUnionTypeAnnotation(parent) || |
| 133 | // (() => A) & (() => B) |
| 134 | isIntersectionTypeAnnotation(parent) || |
| 135 | // (() => A)[] |
| 136 | isArrayTypeAnnotation(parent) || |
| 137 | // <T>(A: T): (T => T[]) => B => [A, B] |
| 138 | (isTypeAnnotation(parent) && |
| 139 | // Check grandparent |
| 140 | isArrowFunctionExpression(printStack[printStack.length - 3])) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | export function UpdateExpression( |
| 145 | node: t.UpdateExpression, |
| 146 | parent: t.Node, |
| 147 | ): boolean { |
| 148 | return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent); |
| 149 | } |
| 150 | |
| 151 | export function ObjectExpression( |
| 152 | node: t.ObjectExpression, |
| 153 | parent: t.Node, |
| 154 | printStack: Array<t.Node>, |
| 155 | ): boolean { |
| 156 | return isFirstInContext( |
| 157 | printStack, |
| 158 | CheckParam.expressionStatement | CheckParam.arrowBody, |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | export function DoExpression( |
| 163 | node: t.DoExpression, |
| 164 | parent: t.Node, |
| 165 | printStack: Array<t.Node>, |
| 166 | ): boolean { |
| 167 | // `async do` can start an expression statement |
| 168 | return ( |
| 169 | !node.async && isFirstInContext(printStack, CheckParam.expressionStatement) |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | export function Binary(node: t.BinaryExpression, parent: t.Node): boolean { |
| 174 | if ( |
| 175 | node.operator === "**" && |
| 176 | isBinaryExpression(parent, { operator: "**" }) |
| 177 | ) { |
| 178 | return parent.left === node; |
| 179 | } |
| 180 | |
| 181 | if (isClassExtendsClause(node, parent)) { |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | if ( |
| 186 | hasPostfixPart(node, parent) || |
| 187 | isUnaryLike(parent) || |
| 188 | isAwaitExpression(parent) |
| 189 | ) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | if (isBinary(parent)) { |
| 194 | const parentOp = parent.operator; |
| 195 | const parentPos = PRECEDENCE[parentOp]; |
| 196 | |
| 197 | const nodeOp = node.operator; |
| 198 | const nodePos = PRECEDENCE[nodeOp]; |
| 199 | |
| 200 | if ( |
| 201 | // Logical expressions with the same precedence don't need parens. |
| 202 | (parentPos === nodePos && |
| 203 | parent.right === node && |
| 204 | !isLogicalExpression(parent)) || |
| 205 | parentPos > nodePos |
| 206 | ) { |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | export function UnionTypeAnnotation( |
| 213 | node: t.UnionTypeAnnotation, |
| 214 | parent: t.Node, |
| 215 | ): boolean { |
| 216 | return ( |
| 217 | isArrayTypeAnnotation(parent) || |
| 218 | isNullableTypeAnnotation(parent) || |
| 219 | isIntersectionTypeAnnotation(parent) || |
| 220 | isUnionTypeAnnotation(parent) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | export { UnionTypeAnnotation as IntersectionTypeAnnotation }; |
| 225 | |
| 226 | export function OptionalIndexedAccessType( |
| 227 | node: t.OptionalIndexedAccessType, |
| 228 | parent: t.Node, |
| 229 | ): boolean { |
| 230 | return isIndexedAccessType(parent, { objectType: node }); |
| 231 | } |
| 232 | |
| 233 | export function TSAsExpression() { |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | export { |
| 238 | TSAsExpression as TSSatisfiesExpression, |
| 239 | TSAsExpression as TSTypeAssertion, |
| 240 | }; |
| 241 | |
| 242 | export function TSUnionType(node: t.TSUnionType, parent: t.Node): boolean { |
| 243 | return ( |
| 244 | isTSArrayType(parent) || |
| 245 | isTSOptionalType(parent) || |
| 246 | isTSIntersectionType(parent) || |
| 247 | isTSUnionType(parent) || |
| 248 | isTSRestType(parent) |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | export { TSUnionType as TSIntersectionType }; |
| 253 | |
| 254 | export function TSInferType(node: t.TSInferType, parent: t.Node): boolean { |
| 255 | return isTSArrayType(parent) || isTSOptionalType(parent); |
| 256 | } |
| 257 | |
| 258 | export function TSInstantiationExpression( |
| 259 | node: t.TSInstantiationExpression, |
| 260 | parent: t.Node, |
| 261 | ) { |
| 262 | return ( |
| 263 | (isCallExpression(parent) || |
| 264 | isOptionalCallExpression(parent) || |
| 265 | isNewExpression(parent) || |
| 266 | isTSInstantiationExpression(parent)) && |
| 267 | !!parent.typeParameters |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | export function BinaryExpression( |
| 272 | node: t.BinaryExpression, |
| 273 | parent: t.Node, |
| 274 | ): boolean { |
| 275 | // let i = (1 in []); |
| 276 | // for ((1 in []);;); |
| 277 | return ( |
| 278 | node.operator === "in" && (isVariableDeclarator(parent) || isFor(parent)) |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | export function SequenceExpression( |
| 283 | node: t.SequenceExpression, |
| 284 | parent: t.Node, |
| 285 | ): boolean { |
| 286 | if ( |
| 287 | // Although parentheses wouldn"t hurt around sequence |
| 288 | // expressions in the head of for loops, traditional style |
| 289 | // dictates that e.g. i++, j++ should not be wrapped with |
| 290 | // parentheses. |
| 291 | isForStatement(parent) || |
| 292 | isThrowStatement(parent) || |
| 293 | isReturnStatement(parent) || |
| 294 | (isIfStatement(parent) && parent.test === node) || |
| 295 | (isWhileStatement(parent) && parent.test === node) || |
| 296 | (isForInStatement(parent) && parent.right === node) || |
| 297 | (isSwitchStatement(parent) && parent.discriminant === node) || |
| 298 | (isExpressionStatement(parent) && parent.expression === node) |
| 299 | ) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | // Otherwise err on the side of overparenthesization, adding |
| 304 | // explicit exceptions above if this proves overzealous. |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | export function YieldExpression( |
| 309 | node: t.YieldExpression, |
| 310 | parent: t.Node, |
| 311 | ): boolean { |
| 312 | return ( |
| 313 | isBinary(parent) || |
| 314 | isUnaryLike(parent) || |
| 315 | hasPostfixPart(node, parent) || |
| 316 | (isAwaitExpression(parent) && isYieldExpression(node)) || |
| 317 | (isConditionalExpression(parent) && node === parent.test) || |
| 318 | isClassExtendsClause(node, parent) |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | export { YieldExpression as AwaitExpression }; |
| 323 | |
| 324 | export function ClassExpression( |
| 325 | node: t.ClassExpression, |
| 326 | parent: t.Node, |
| 327 | printStack: Array<t.Node>, |
| 328 | ): boolean { |
| 329 | return isFirstInContext( |
| 330 | printStack, |
| 331 | CheckParam.expressionStatement | CheckParam.exportDefault, |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | export function UnaryLike( |
| 336 | node: |
| 337 | | t.UnaryLike |
| 338 | | t.ArrowFunctionExpression |
| 339 | | t.ConditionalExpression |
| 340 | | t.AssignmentExpression, |
| 341 | parent: t.Node, |
| 342 | ): boolean { |
| 343 | return ( |
| 344 | hasPostfixPart(node, parent) || |
| 345 | isBinaryExpression(parent, { operator: "**", left: node }) || |
| 346 | isClassExtendsClause(node, parent) |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | export function FunctionExpression( |
| 351 | node: t.FunctionExpression, |
| 352 | parent: t.Node, |
| 353 | printStack: Array<t.Node>, |
| 354 | ): boolean { |
| 355 | return isFirstInContext( |
| 356 | printStack, |
| 357 | CheckParam.expressionStatement | CheckParam.exportDefault, |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | export function ArrowFunctionExpression( |
| 362 | node: t.ArrowFunctionExpression, |
| 363 | parent: t.Node, |
| 364 | ): boolean { |
| 365 | return isExportDeclaration(parent) || ConditionalExpression(node, parent); |
| 366 | } |
| 367 | |
| 368 | export function ConditionalExpression( |
| 369 | node: |
| 370 | | t.ConditionalExpression |
| 371 | | t.ArrowFunctionExpression |
| 372 | | t.AssignmentExpression, |
| 373 | parent?: t.Node, |
| 374 | ): boolean { |
| 375 | if ( |
| 376 | isUnaryLike(parent) || |
| 377 | isBinary(parent) || |
| 378 | isConditionalExpression(parent, { test: node }) || |
| 379 | isAwaitExpression(parent) || |
| 380 | isTSTypeExpression(parent) |
| 381 | ) { |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | return UnaryLike(node, parent); |
| 386 | } |
| 387 | |
| 388 | export function OptionalMemberExpression( |
| 389 | node: t.OptionalMemberExpression, |
| 390 | parent: t.Node, |
| 391 | ): boolean { |
| 392 | return ( |
| 393 | isCallExpression(parent, { callee: node }) || |
| 394 | isMemberExpression(parent, { object: node }) |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | export { OptionalMemberExpression as OptionalCallExpression }; |
| 399 | |
| 400 | export function AssignmentExpression( |
| 401 | node: t.AssignmentExpression, |
| 402 | parent: t.Node, |
| 403 | ): boolean { |
| 404 | if (isObjectPattern(node.left)) { |
| 405 | return true; |
| 406 | } else { |
| 407 | return ConditionalExpression(node, parent); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | export function LogicalExpression( |
| 412 | node: t.LogicalExpression, |
| 413 | parent: t.Node, |
| 414 | ): boolean { |
| 415 | if (isTSTypeExpression(parent)) return true; |
| 416 | switch (node.operator) { |
| 417 | case "||": |
| 418 | if (!isLogicalExpression(parent)) return false; |
| 419 | return parent.operator === "??" || parent.operator === "&&"; |
| 420 | case "&&": |
| 421 | return isLogicalExpression(parent, { operator: "??" }); |
| 422 | case "??": |
| 423 | return isLogicalExpression(parent) && parent.operator !== "??"; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | export function Identifier( |
| 428 | node: t.Identifier, |
| 429 | parent: t.Node, |
| 430 | printStack: Array<t.Node>, |
| 431 | ): boolean { |
| 432 | // 13.15.2 AssignmentExpression RS: Evaluation |
| 433 | // (fn) = function () {}; |
| 434 | if ( |
| 435 | node.extra?.parenthesized && |
| 436 | isAssignmentExpression(parent, { left: node }) && |
| 437 | (isFunctionExpression(parent.right) || isClassExpression(parent.right)) && |
| 438 | parent.right.id == null |
| 439 | ) { |
| 440 | return true; |
| 441 | } |
| 442 | // Non-strict code allows the identifier `let`, but it cannot occur as-is in |
| 443 | // certain contexts to avoid ambiguity with contextual keyword `let`. |
| 444 | if (node.name === "let") { |
| 445 | // Some contexts only forbid `let [`, so check if the next token would |
| 446 | // be the left bracket of a computed member expression. |
| 447 | const isFollowedByBracket = |
| 448 | isMemberExpression(parent, { |
| 449 | object: node, |
| 450 | computed: true, |
| 451 | }) || |
| 452 | isOptionalMemberExpression(parent, { |
| 453 | object: node, |
| 454 | computed: true, |
| 455 | optional: false, |
| 456 | }); |
| 457 | return isFirstInContext( |
| 458 | printStack, |
| 459 | isFollowedByBracket |
| 460 | ? CheckParam.expressionStatement | |
| 461 | CheckParam.forHead | |
| 462 | CheckParam.forInHead | |
| 463 | CheckParam.forOfHead |
| 464 | : CheckParam.forOfHead, |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | // ECMAScript specifically forbids a for-of loop from starting with the |
| 469 | // token sequence `for (async of`, because it would be ambiguous with |
| 470 | // `for (async of => {};;)`, so we need to add extra parentheses. |
| 471 | // |
| 472 | // If the parent is a for-await-of loop (i.e. parent.await === true), the |
| 473 | // parentheses aren't strictly needed, but we add them anyway because |
| 474 | // some tools (including earlier Babel versions) can't parse |
| 475 | // `for await (async of [])` without them. |
| 476 | return ( |
| 477 | node.name === "async" && isForOfStatement(parent) && node === parent.left |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | // Walk up the print stack to determine if our node can come first |
| 482 | // in a particular context. |
| 483 | function isFirstInContext( |
| 484 | printStack: Array<t.Node>, |
| 485 | checkParam: CheckParam, |
| 486 | ): boolean { |
| 487 | const expressionStatement = checkParam & CheckParam.expressionStatement; |
| 488 | const arrowBody = checkParam & CheckParam.arrowBody; |
| 489 | const exportDefault = checkParam & CheckParam.exportDefault; |
| 490 | const forHead = checkParam & CheckParam.forHead; |
| 491 | const forInHead = checkParam & CheckParam.forInHead; |
| 492 | const forOfHead = checkParam & CheckParam.forOfHead; |
| 493 | |
| 494 | let i = printStack.length - 1; |
| 495 | if (i <= 0) return; |
| 496 | let node = printStack[i]; |
| 497 | i--; |
| 498 | let parent = printStack[i]; |
| 499 | while (i >= 0) { |
| 500 | if ( |
| 501 | (expressionStatement && |
| 502 | isExpressionStatement(parent, { expression: node })) || |
| 503 | (exportDefault && |
| 504 | isExportDefaultDeclaration(parent, { declaration: node })) || |
| 505 | (arrowBody && isArrowFunctionExpression(parent, { body: node })) || |
| 506 | (forHead && isForStatement(parent, { init: node })) || |
| 507 | (forInHead && isForInStatement(parent, { left: node })) || |
| 508 | (forOfHead && isForOfStatement(parent, { left: node })) |
| 509 | ) { |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | if ( |
| 514 | i > 0 && |
| 515 | ((hasPostfixPart(node, parent) && !isNewExpression(parent)) || |
| 516 | (isSequenceExpression(parent) && parent.expressions[0] === node) || |
| 517 | (isUpdateExpression(parent) && !parent.prefix) || |
| 518 | isConditional(parent, { test: node }) || |
| 519 | isBinary(parent, { left: node }) || |
| 520 | isAssignmentExpression(parent, { left: node })) |
| 521 | ) { |
| 522 | node = parent; |
| 523 | i--; |
| 524 | parent = printStack[i]; |
| 525 | } else { |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return false; |
| 531 | } |