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

Question about union/intersection types AST #9020

Closed
fisker opened this issue Mar 7, 2023 · 3 comments
Closed

Question about union/intersection types AST #9020

fisker opened this issue Mar 7, 2023 · 3 comments
Assignees

Comments

@fisker
Copy link

fisker commented Mar 7, 2023

Flow version: 0.201.0

Currently, Prettier print UnionTypeAnnotation and IntersectionTypeAnnotation differently from TypeScript parser. It's caused by differences between Flow AST and typescript-eslint AST.

  1. The range
// typescript-eslint
type A = & B & C
         ^^^^^^^ Range of `TSIntersectionType`

// Flow
type A = & B & C
           ^^^^^ Range of `IntersectionTypeAnnotation`
  1. Single type
// typescript-eslint
type A = | B
         ^^^ TSUnionType
           ^ TSTypeReference

// Flow
type A = | B
           ^ GenericTypeAnnotation
           ^ Identifier

Personally, I think the AST shape of typescript-eslint contains more information. Do you think we can align with it?

I'm working on this part, if we decide not to change, we'll have to make TS AST align with Flow (before print), because it's not possible to do the opposite, we don't have information of the leading | and & (unless we use tokens, which we havn't used yet).

@fisker
Copy link
Author

fisker commented Mar 14, 2023

Hi, I saw this assigned to @mroch. Are we going to change the AST?

@mroch
Copy link
Contributor

mroch commented Mar 19, 2023

yes, we'll change this.

I also noticed that (B) | C is missing the leading paren in the union's range (similar for intersections)

@mroch
Copy link
Contributor

mroch commented Mar 19, 2023

actually, the single type would be a much more involved change because it would mean there can be single-member unions or intersections, which we'd then have to handle throughout the type system. neither Babel nor Flow currently produce union or intersection ASTs in this case.

i think we'll skip changing that one for now.

facebook-github-bot pushed a commit that referenced this issue Mar 20, 2023
Summary:
ref #9020

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D44204757

fbshipit-source-id: f35c0dbbbef529335ec9720c43a67f90f74f3df8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants