Skip to content

[Babel 8] Remove TSParenthesizedType breaks generator #17715

@sxzz

Description

@sxzz

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

import { generate } from '@babel/generator'
import { parse } from '@babel/parser'

const { program } = parse(
  `type MaybePromise<T> = Promise<T> | (T extends any ? Promise<T> : never)`,
  { plugins: ['typescript'] },
)
const { code } = generate(program)
console.log(code)
// type MaybePromise<T> = Promise<T> | T extends any ? Promise<T> : never;

// it's different if re-parse

Playground: https://ast-explorer.dev/#eNqFUMsKgzAQ/JUlpxY89Sh9fEGhB29ND1G3xRI3IYlSUf+9G60glNIcspOZzeywvdAiFU/VKl+4ygaRCMtErnLUjAvGobMIZ9XleHGmrjzusyMcIIMBVsTABL4CUulBUQentZgCYYtO0pfX7ofZ5r/bVhInNJywlwQghTeNKzDjEVKk/K5N2WjGySxb3Twq8lG7RgaWEsUYbN7Apz+eyXfpKMP09a60x4UfZ3CLha+R87Schzi6GN/DIGu4

Configuration file name

No response

Configuration

N/A

Current and expected behavior

Current:

type MaybePromise<T> = Promise<T> | T extends any ? Promise<T> : never;

Expected:

type MaybePromise<T> = Promise<T> | (T extends any ? Promise<T> : never);

Environment

N/A

Possible solution

Add brackets in generated code.

Additional context

downstream issue: sxzz/rolldown-plugin-dts#167
relate: #9546

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions