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

Support Flow this parameter annotations #12234

Merged
merged 22 commits into from Feb 21, 2021
Merged

Conversation

dsainati1
Copy link
Contributor

@dsainati1 dsainati1 commented Oct 21, 2020

This adds support for this parameter annotations.

Example:

function foo (this: number) {}

This is already supported in the Flow parser: facebook/flow@8efdc8c

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

@babel-bot
Copy link
Collaborator

babel-bot commented Oct 21, 2020

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

@existentialism existentialism added area: flow PR: New Feature 🚀 A type of pull request used for our changelog categories labels Oct 21, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 21, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bd8d4df:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@kaicataldo
Copy link
Member

kaicataldo commented Oct 21, 2020

Looks like this is currently failing CI's linting step due to a Flow issue. Mind fixing the typing issue so that we can review? :)

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.

Can you add an integration test with transform-parameters like we did in #9714?

packages/babel-generator/src/generators/flow.js Outdated Show resolved Hide resolved
@@ -1461,6 +1524,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

node.params = tmp.params;
node.rest = tmp.rest;
node.this = tmp._this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also update

export type BodilessFunctionOrMethodBase = HasDecorators & {
? Since we have a new "this" property in the AST node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new "this" property is only present in the FunctionTypeAnnotation node. For regular function nodes the "this" is the first member of the parameter list (to match the Flow parser's behavior).

Copy link
Member

Choose a reason for hiding this comment

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

This also matches the AST we use for this type annotations in TS.

packages/babel-parser/src/plugins/flow.js Outdated Show resolved Hide resolved
@dsainati1
Copy link
Contributor Author

Not sure what the create react app failure is, seems unrelated to these changes.

@existentialism
Copy link
Member

@dsainati1 it's def unrelated, we're seeing it on all recent prs (terser/terser#854)

@dsainati1
Copy link
Contributor Author

Any updates on this?

@nicolo-ribaudo nicolo-ribaudo added this to the v7.13.0 milestone Nov 4, 2020
@nicolo-ribaudo
Copy link
Member

We'll release this in the next minor version.

@nicolo-ribaudo
Copy link
Member

@dsainati1 Could you rebase this, or enabling "Allow edit by maintainers" in the right sidebar?
I tried rebasing and pushing, but I got a permission error.

@dsainati1
Copy link
Contributor Author

I have the "Allow edit by maintainers" box checked.

@JLHwung
Copy link
Contributor

JLHwung commented Feb 5, 2021

@dsainati1 Although "Allow edit by maintainers" is checked, we can not push to your repo because the PR is from the default branch main. I have rebased on the upstream and pushed to https://github.com/JLHwung/babel/tree/dsainati1/main Can you reset to this ref and force push?

@dsainati1
Copy link
Contributor Author

OK I think I got it, but let me know if you still need something from me here.

@dsainati1
Copy link
Contributor Author

Hmmm it seems the estree-throws test is failing on this revision, but it is unclear to me what this means. All the other tests pass without issue. Any thoughts?

@JLHwung
Copy link
Contributor

JLHwung commented Feb 8, 2021

@dsainati1 If an estree-throws test throws, it means the related test case will fail when you enable both estree and flow plugin. The use case is that people could use @babel/eslint-parser on a Flow project.

For debug purpose you can add "estree" to options.json

{
  "plugins": ["estree", "flow"]
}

@dsainati1
Copy link
Contributor Author

Given that this test was not failing when I submitted this PR initially, I am not sure what would have changed between now and then that causes this test to fail. Are there any recent changes I should know about that would result in different behavior here?

@JLHwung
Copy link
Contributor

JLHwung commented Feb 9, 2021

The test was likely broken before you rebased, but it was not reported then because before 8478027 we never ensure that the parser tests throw same errors when estree plugin is added to the test options.

@@ -1160,6 +1182,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (kind === "get" || kind === "set") {
this.flowCheckGetterSetterParams(node);
}
if (node.key.name === "constructor" && node.value.this) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As you can see from https://astexplorer.net/#/gist/e4e0c3380467882e445d5ddd8193feac/8bf60fc83e55d1fff70cd5b0ad4d3255e9fb5691, when estree plugin is enabled, the AST is slightly different. node.value becomes a function expression. The error is thrown, very likely because node.value.this is no longer true when estree is enabled.

Copy link
Contributor

@JLHwung JLHwung Feb 9, 2021

Choose a reason for hiding this comment

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

Can you add a new test case on string literal constructor?

class A {
  "constructor"() {}
}

It is semantically equivalent to the identifier constructor.

This was referenced Mar 14, 2021
@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 24, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: flow outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants