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

[AST] shape of Param, Pat and VarDeclarator #5416

Open
magic-akari opened this issue Aug 8, 2022 · 3 comments
Open

[AST] shape of Param, Pat and VarDeclarator #5416

magic-akari opened this issue Aug 8, 2022 · 3 comments
Labels
Milestone

Comments

@magic-akari
Copy link
Member

Describe the bug

I don't think the typeAnnotation should be attached on pat.

1. VarDeclarator

The typeAnnotation should always be attached on the VarDeclarator, not the pat.

const a: number = 1, [b]: [number] = 2, { c }: { c: number } = { c: 3 };

Expect:

VarDeclarator {
    name: Pat,
    init: Expr,
    type_ann: TsTypeAnn,
};

Actual:
the type_ann is in the name.

2. Param

There should be a type_ann in the Param like VarDeclarator.

3. init in the Param

function foo(a = 1) { }

function bar(a: number = 1) { }

Expect :

Param {
    pat: Pat,
    init: Expr,
    type_ann: TsTypeAnn
}

Actual:
got AssignPat

image


It's better to remove typeAnnotation from Pat and add it to other AST struct like Param, VarDeclarator.

Input code

No response

Config

No response

Playground link

No response

Expected behavior

/

Actual behavior

No response

Version

N/A

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Aug 8, 2022

Not sure if it will work.
AST of swc is designed after one of the babel

@magic-akari
Copy link
Member Author

Yes, I checked the Babel AST.

function foo(a = 1) { }

It is parsed as AssignmentPattern as well.

image

In TypeScript, however, there is an initializer field in the Parameter.

image

Rome is also designed in this way.

image

I cannot tell which is better.

@magic-akari
Copy link
Member Author

magic-akari commented Aug 8, 2022

After some investigating, I found out that babel's AST design is related to flow.js.
In older versions of flow.js(<v0.176.0), it was indeed possible to write the type into the pattern.

const [a: number, b: string, c: boolean] = ['a', true, 1];
a.toLocaleLowerCase(); // a === string
b - 1 // b === boolean
c.toFixed(); // c === number

const arr: string[] = [];
const [d: number] = arr;
d.toLocaleLowerCase();

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjOA7AzgFzAG0BDALjGwFcBbAIwFMAnAGjDooKYEtsBzNhgp04cGAxLYAumAC8xAOQkFbfEyoM2ARikBuVCQB0+OABk4GEuPMJmAYRK4GACgCUusMGBgSc2fK5ePlQ6MABaMC1Pb1D-eRExCWxMYzgAMW4ADwYAEzcPLzAMP3lqemZ0LDxCEiYmTjUgohl5Zv0qgmIcijLGJhafOv0c1PNLa0R7Rxd3VCA
(from babel/babel#9545 (comment))

But in the new version of flow.js(>=0.176.0), it has behaved like TS and does not allow this syntax anymore.

Babel is also trying to fix it.

@kdy1 kdy1 added this to the Planned milestone Aug 8, 2022
@magic-akari magic-akari changed the title AST shape of Param, Pat and VarDeclarator [AST] shape of Param, Pat and VarDeclarator Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants