Skip to content

Fix parenthesizer rules for manually constructed binary expressions with ?? and ||/&& mix #62311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #62307

@Copilot Copilot AI review requested due to automatic review settings August 21, 2025 09:42
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Aug 21, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 21, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in TypeScript's printer parenthesizer rules for manually constructed binary expressions that mix the nullish coalescing operator (??) with logical operators (||/&&). The fix ensures proper parenthesization when these operators are combined to maintain correct evaluation order and avoid syntax errors.

Key changes:

  • Added logic to detect mixing of ?? with ||/&& operators and force parenthesization
  • Added comprehensive unit tests to verify correct printing behavior for all operator combinations
  • Updated test baselines to reflect the corrected parenthesization output

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 8 comments.

File Description
src/compiler/factory/parenthesizerRules.ts Core fix adding mixingBinaryOperatorsRequiresParentheses function and early parenthesization check
src/testRunner/unittests/printer.ts Comprehensive unit tests for all binary operator combinations with ??
tests/baselines/reference/printerApi/*.js Test baseline files showing expected parenthesized output for various operator combinations

@@ -121,6 +131,10 @@ export function createParenthesizerRules(factory: NodeFactory): ParenthesizerRul
* BinaryExpression.
*/
function binaryOperandNeedsParentheses(binaryOperator: SyntaxKind, operand: Expression, isLeftSideOfBinary: boolean, leftOperand: Expression | undefined) {
const emittedOperand = skipPartiallyEmittedExpressions(operand);
if (isBinaryExpression(emittedOperand) && mixingBinaryOperatorsRequiresParentheses(binaryOperator, emittedOperand.operatorToken.kind)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixing them without parentheses is a special grammar-based syntax error, so one can't simply handle both of those cases using precedence and associativity alone, and for that reason I'm also specialcasing this here

Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems right to me. @weswigham confirm / check Corsa behavior?

@github-project-automation github-project-automation bot moved this from Not started to Needs merge in PR Backlog Aug 21, 2025
@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Needs merge
Development

Successfully merging this pull request may close these issues.

Manually constructed nullish coalescing + OR AST is missing parentheses when printed
3 participants