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 multiple static blocks #12738

Merged
merged 5 commits into from Mar 12, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Feb 2, 2021

Q                       A
Fixed Issues? Implements tc39/proposal-class-static-block#38, closes #12979
Patch: Bug Fix? Y
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I will not mark this PR as ready until the upstream PR is merged.

The integration test about new.target is disabled because of #12737. Yet the test of static-blocks is still valid because we transform static blocks to static private field initializers.

@JLHwung JLHwung added PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Class Static Block labels Feb 2, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Feb 2, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 2, 2021

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 e002f53:

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

Copy link
Contributor Author

@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.

It's been a while and I complete forget the context of this PR.

This PR looks good to me except that we should avoid shadowing upper private identifiers.

@@ -1528,6 +1528,5 @@ export type ParseSubscriptState = {

export type ParseClassMemberState = {|
hadConstructor: boolean,
hadStaticBlock: boolean,
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 parser types are not exported, so we are free to refactor here.

return;
for (const path of body) {
if (!path.isStaticBlock()) continue;
const staticBlockPrivateId = generateUid(scope, privateNames);
Copy link
Contributor Author

@JLHwung JLHwung Mar 8, 2021

Choose a reason for hiding this comment

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

The inserted unique staticBlockPrivateId may accidentally shadow private id defined on upper levels:

class C {
  static #_;
  constructor() {
    class D {
      static {
        C.#_ = 42;
      }
    }
  }
}

The injected #_ = AIIFE(static block) will shadow #_ defined on C. Consider reuse the privateNameVisitorFactory in @babel/helper-create-class-features-plugin.

Copy link
Member

Choose a reason for hiding this comment

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

Why don't we just use the filename + source location to generate a private name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does not really solve the mentioned issue, just makes it way less likely to happen. I don't think this is a blocker and we can address that in another PR.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably consider tracking private identifiers in @babel/traverse's generateUid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or we provide a new generateUniquePrivateKeyAPI since plain identifier does not conflict with private identifiers.

@nicolo-ribaudo
Copy link
Member

Let's do #12738 (comment) in a separate PR, since it happens regardless of multiple static blocks.

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.

Can you add an input/output test with multiple static blocks?

This was referenced Mar 16, 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 Jun 12, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Spec Compliance 👓 A type of pull request used for our changelog categories Spec: Class Static Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow multiple class static block, and follow the document order
4 participants