Skip to content

Manually constructed nullish coalescing + OR AST is missing parentheses when printedΒ #62307

@JeanMeche

Description

@JeanMeche

πŸ”Ž Search Terms

  • nullish coalescing
  • boolean operation
  • parenthesis

πŸ•— Version & Regression Information

⏯ Playground Link

https://stackblitz.com/edit/vitejs-vite-a11yrnds?file=package.json,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 nullishOrNode = ts.factory.createExpressionStatement(
  ts.factory.createBinaryExpression(
    ts.factory.createBinaryExpression(
      ts.factory.createIdentifier('a'),
      ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
      ts.factory.createIdentifier('b')
    ),
    ts.factory.createToken(ts.SyntaxKind.BarBarToken),
    ts.factory.createIdentifier('d')
  )
);

// (a ? b : c) || d
const nullishOr = printer.printNode(
  ts.EmitHint.Unspecified,
  nullishOrNode,
  src
);

πŸ™ Actual behavior

To output: a ?? b || d;

πŸ™‚ Expected behavior

To output: (a ?? b) || d;

Additional information about the issue

This change as a breaking change by an Angular user after updating to TS 5.9 angular/angular#63287

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions