Skip to content
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

Manually constructed nullish coalescing AST is missing parentheses when printed #61369

Open
mmalerba opened this issue Mar 7, 2025 · 0 comments Β· May be fixed by #61372
Open

Manually constructed nullish coalescing AST is missing parentheses when printed #61369

mmalerba opened this issue Mar 7, 2025 · 0 comments Β· May be fixed by #61372

Comments

@mmalerba
Copy link

mmalerba commented Mar 7, 2025

πŸ”Ž Search Terms

nullish coalescing parentheses ast

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing

⏯ Playground Link

https://stackblitz.com/edit/vitejs-vite-letuvqes?file=src%2Fmain.ts

πŸ’» Code

import * as ts from 'typescript';

const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const src = ts.createSourceFile('test.ts', '', ts.ScriptTarget.Latest);

const nullishNode = ts.factory.createExpressionStatement(
  ts.factory.createBinaryExpression(
    ts.factory.createConditionalExpression(
      ts.factory.createIdentifier('a'),
      ts.factory.createToken(ts.SyntaxKind.QuestionToken),
      ts.factory.createIdentifier('b'),
      ts.factory.createToken(ts.SyntaxKind.ColonToken),
      ts.factory.createIdentifier('c')
    ),
    ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
    ts.factory.createIdentifier('d')
  )
);

const orNode = ts.factory.createExpressionStatement(
  ts.factory.createBinaryExpression(
    ts.factory.createConditionalExpression(
      ts.factory.createIdentifier('a'),
      ts.factory.createToken(ts.SyntaxKind.QuestionToken),
      ts.factory.createIdentifier('b'),
      ts.factory.createToken(ts.SyntaxKind.ColonToken),
      ts.factory.createIdentifier('c')
    ),
    ts.factory.createToken(ts.SyntaxKind.BarBarToken),
    ts.factory.createIdentifier('d')
  )
);

// a ? b : c ?? d
const nullish = printer.printNode(ts.EmitHint.Unspecified, nullishNode, src);
// (a ? b : c) || d
const or = printer.printNode(ts.EmitHint.Unspecified, orNode, src);

πŸ™ Actual behavior

When I manually create AST for the expression (a ? b : c) ?? d but leave the parentheses out of the AST, the generated code does not add the parens back in and therefore does not match the structure of the AST.

When I do this same thing with || or && it behaves as expected, inserting the parens back in to the generated code.

πŸ™‚ Expected behavior

The nullishNode should be printed in the same way the orNode is in the example, adding back in the parentheses so that the code matches the structure of the AST

Additional information about the issue

No response

@Andarist Andarist linked a pull request Mar 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant