Skip to content

Support multiple static blocks#12738

Merged
nicolo-ribaudo merged 5 commits intobabel:mainfrom
JLHwung:multiple-static-blocks
Mar 12, 2021
Merged

Support multiple static blocks#12738
nicolo-ribaudo merged 5 commits intobabel:mainfrom
JLHwung:multiple-static-blocks

Conversation

@JLHwung
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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

@JLHwung JLHwung force-pushed the multiple-static-blocks branch from ddbc630 to 0c990d4 Compare February 2, 2021 16:13
@JLHwung JLHwung force-pushed the multiple-static-blocks branch from 0c990d4 to d661b37 Compare March 8, 2021 15:24
@JLHwung JLHwung marked this pull request as ready for review March 8, 2021 15:57
Copy link
Copy Markdown
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.


export type ParseClassMemberState = {|
hadConstructor: boolean,
hadStaticBlock: boolean,
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

Copy link
Copy Markdown
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?

@JLHwung JLHwung force-pushed the multiple-static-blocks branch from d661b37 to e002f53 Compare March 12, 2021 22:12
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