Skip to content

fix: Missing parentheses when printing a TS arrow function type in a union#17125

Merged
JLHwung merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-17123
Feb 12, 2025
Merged

fix: Missing parentheses when printing a TS arrow function type in a union#17125
JLHwung merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-17123

Conversation

@liuxingbaoyu
Copy link
Member

Q                       A
Fixed Issues? Fixes #17123
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass?
Documentation PR Link
Any Dependency Changes?
License MIT

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator area: typescript i: regression labels Feb 10, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Feb 10, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58702

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release and removed PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Feb 10, 2025
@nicolo-ribaudo
Copy link
Member

Can you also add other test cases for arrow types, and check that they work?

type t = (() => a) extends b ? c : d;
type t = a extends (() => b) ? c : d;
type t = a extends b ? () => c : d;
type t = a extends b ? c : () => d;
type t = (() => a)[];
type t = (() => a)["name"];

@liuxingbaoyu
Copy link
Member Author

Amazing!
In addition to these, I also discovered a few others, and even suspected that there might be.🤦‍♂️

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Essentially we should handle everything descended from tsParseType all the way until tsParseNonArrayType such that the TSFunctionalType may be created at the start position of the tsParseType.

tsParseType: TSConditionalType (checkType, extendsType)
  tsParseNonConditionalType:
    tsParseUnionTypeOrHigher: TSUnionType (n)
      tsParseIntersectionTypeOrHigher: TSIntersectionType (n)
        tsParseTypeOperatorOrHigher: 
          tsParseArrayTypeOrHigher: TSArrayType, TSIndexedAccessType (objectType)
            tsParseNonArrayType

n: Extra rules also forbid b | () => c and a & () => c though they are perfectly unambiguous.

This route is also required because it does not cross any tsParseType boundary and the optional type is a postfix operator such that TSFunctionalType may be created at the start:

tsParseNonArrayType:
  tsParseTupleType:
    tsParseTupleElementType: TSOptionalType
      tsParseNonArrayType

So we should further check the objectType key for TSIndexedAccessType although T[() => string] does not have much practical usage.

(parent.checkType === node || parent.extendsType === node))
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

We should handle TSConstructorType such as new () => Date or abstract new () => Date as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

image
It seems like this doesn't need the parentheses?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well in this case the assignment expression lhs = rhs must not be a TSType so () => void = x is not a TSType, therefore parentheses is not required here. But we definitely need to disambiguate between abstract new () => number[] and (abstract new () => number)[], as well as optional type, intersection, ... etc.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

#17125 (comment) has been done, right?

(I'm eager to release this, because it's being very annoying in my experiments with publishing to JSR as I currently have to manually edit the build output 😛)

parentType === "TSUnionType" ||
parentType === "TSOptionalType" ||
parentType === "TSArrayType" ||
parentType === "TSConstructorType" ||
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this check is required, the following examples are equivalent:

type T = () => abstract new () => string
type T = () => (abstract new () => string)

@JLHwung JLHwung merged commit 99d89fc into babel:main Feb 12, 2025
55 checks passed
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Feb 12, 2025
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 15, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: typescript i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing parentheses when printing a TS arrow function type in a union

4 participants