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 for class static initialization block #4249

Merged
merged 6 commits into from Nov 1, 2021

Conversation

dnalborczyk
Copy link
Contributor

@dnalborczyk dnalborczyk commented Oct 24, 2021

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
Resolves #4250

Description

this test is currently failing. acorn added support for class static initialization blocks in v8.5 (https://github.com/acornjs/acorn/blob/master/acorn/CHANGELOG.md#850-2021-09-06) which is currently being used by rollup.

Update: This now implements full support for static initialization blocks including tree-shaking

@lukastaegert
Copy link
Member

Thanks! Turns out there is actually a fallback missing + of course the AST nodes. I will add some commits to your PR.

@codecov
Copy link

codecov bot commented Oct 25, 2021

Codecov Report

Merging #4249 (b641407) into master (0b8d4c6) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4249      +/-   ##
==========================================
+ Coverage   98.37%   98.39%   +0.01%     
==========================================
  Files         203      204       +1     
  Lines        7272     7283      +11     
  Branches     2081     2079       -2     
==========================================
+ Hits         7154     7166      +12     
  Misses         58       58              
+ Partials       60       59       -1     
Impacted Files Coverage Δ
src/ast/nodes/ClassBody.ts 100.00% <ø> (ø)
src/ast/nodes/NodeType.ts 100.00% <ø> (ø)
src/ast/nodes/index.ts 100.00% <ø> (ø)
src/Module.ts 100.00% <100.00%> (ø)
src/ast/nodes/ArrowFunctionExpression.ts 100.00% <100.00%> (ø)
src/ast/nodes/BlockStatement.ts 100.00% <100.00%> (ø)
src/ast/nodes/CatchClause.ts 100.00% <100.00%> (+16.66%) ⬆️
src/ast/nodes/ClassDeclaration.ts 100.00% <100.00%> (ø)
src/ast/nodes/FunctionDeclaration.ts 100.00% <100.00%> (ø)
src/ast/nodes/IfStatement.ts 100.00% <100.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1494c68...b641407. Read the comment docs.

@dnalborczyk
Copy link
Contributor Author

I will add some commits to your PR.

@lukastaegert

sure. also, feel free to close this PR if you'd rather open a new PR, as I don't want to take any credit for your work.

@lukastaegert lukastaegert changed the title test: support for class static initialization block Support for class static initialization block Oct 27, 2021
@lukastaegert
Copy link
Member

This is now done from my side and includes full tree-shaking support. Please test!

@dnalborczyk
Copy link
Contributor Author

This is now done from my side and includes full tree-shaking support. Please test!

Oh, wow, awesome! Will do!

@dnalborczyk
Copy link
Contributor Author

dnalborczyk commented Oct 27, 2021

looks good! on some initial tests I found:

// original
class A {
  static foo = "bar";

  static someStatic() {
    return "foo";
  }

  static {
    A.foo = A.someStatic(); // not working with rollup
	// this.foo = A.someStatic(); // does work with rollup
  }
}

console.log(A.foo);
// rollup bundle
class A {
  static foo = "bar";

  static someStatic() {
    return "foo";
  }

  static {
    A.someStatic();
  }
}

console.log(A.foo);

returns:
original: 'foo'
rollup: 'bar'

I'll try to add a test.

@dnalborczyk
Copy link
Contributor Author

@lukastaegert I extended the static block tests with an additional class. I could not get it to fail with extending the existing Example class for some reason.

@lukastaegert
Copy link
Member

Thanks for the test, I did some really stupid copy-and-pasting there that you discovered... Should be working correctly now, please check again!

@github-actions
Copy link

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install dnalborczyk/rollup#static-block-test

or load it into the REPL:
https://rollupjs.org/repl/?pr=4249

@dnalborczyk
Copy link
Contributor Author

dnalborczyk commented Oct 29, 2021

Thanks for the test, I did some really stupid copy-and-pasting there that you discovered... Should be working correctly now, please check again!

yeah, looks good, it's working now! 👍

one thing I don't really know anything about the expectations of tree-shaking, e.g. with this the entire (unused) class would be removed:
https://rollupjs.org/repl/?pr=4249&shareable=JTd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GF0aWMoKSUzQiU1Q24lNUN0JTdEJTVDbiU1Q24lMjAlMjBzdGF0aWMlMjAlMjNzb21lU3RhdGljKCklMjAlN0IlNUNuJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTVDJTIyZm9vJTVDJTIyJTNCJTVDbiUyMCUyMCU3RCU1Q24lNUNuJTIwJTIwc3RhdGljJTIwJTdCJTVDbiU1Q3QlNUN0QS4lMjNmb28lMjAlM0QlMjBBLiUyM3NvbWVTdGF0aWMoKSUzQiU1Q24lNUN0JTdEJTVDbiU3RCUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=

@lukastaegert
Copy link
Member

lukastaegert commented Oct 30, 2021 via email

@dnalborczyk
Copy link
Contributor Author

dnalborczyk commented Oct 30, 2021

It is not the block but what happens inside the block. It does not know that accessing the private variable does not have side effects (and similar things)

makes sense. anything can potentially run, not just class init stuff.

@lukastaegert lukastaegert merged commit ce3de49 into rollup:master Nov 1, 2021
Copy link

@FAVE01 FAVE01 left a comment

Choose a reason for hiding this comment

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

🔫

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support for class static initialization blocks
3 participants