Transform class static block#12143
Transform class static block#12143nicolo-ribaudo merged 18 commits intobabel:feat-7.12.0/class-static-blockfrom
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/29876/ |
|
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 4154ffa:
|
|
What about this? class Foo {
static {
this.foo = 42;
}
} class Foo {
#_ = (() => {
this.foo = 42;
})();
}It is completely self-contained (it doesn't leak |
4ce6931 to
ca389fb
Compare
|
@nicolo-ribaudo The class element initializer does not have access to the initialized class name binding. class Foo {
#_ = (() => {
Foo.foo = 42; // <-- Foo here is undefined
})();
} |
|
My mistake, I meant the private field to be static: class Foo {
static #_ = (() => {
this.foo = 42;
console.log(Foo);
})();
}In chromium this works, I'm now checking the spec to understand where the binding is initialized. EDIT: https://tc39.es/proposal-static-class-features/#runtime-semantics-class-definition-evaluation - The binding is initialized at step 30.a, and the static field is evaluated at step 34.a. |
| export default declare(({ types: t, template, assertVersion }) => { | ||
| // todo remove this check after Babel 7.12.0 is published | ||
| if (process.env.NODE_ENV !== "test") { | ||
| assertVersion("^7.12.0"); |
There was a problem hiding this comment.
As a new package it requires @babel/core 7.12.0. It depends on @babel/type 7.12.0 and @babel/parser 7.12.0.
Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
8523873 to
ffa4a78
Compare
474cc03 to
cf10e41
Compare
|
We should also test how this behaves with the private fields transform (plugin ordering 😛). |
|
(If we detect the wrong order, we can just throw an error asking users to swap the two plugins) |
@nicolo-ribaudo Yes that is how I am gonna fix "you should specify a test" error on node 10. |
adc83d8 to
bc467f5
Compare
bc467f5 to
bed0bd4
Compare
This comment has been minimized.
This comment has been minimized.
packages/babel-helper-create-class-features-plugin/src/index.js
Outdated
Show resolved
Hide resolved
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
d6d40d9 to
09d1f8a
Compare
packages/babel-helper-create-class-features-plugin/src/index.js
Outdated
Show resolved
Hide resolved
Co-authored-by: Brian Ng <bng412@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> Co-authored-by: Brian Ng <bng412@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> Co-authored-by: Brian Ng <bng412@gmail.com>
This PR implements Stage 2 Class static block proposal. It is an early draft as more test cases are required.
This idea is to transform
static { }to a private static field initializer. Sobabel/packages/babel-plugin-proposal-class-static-block/test/fixtures/class-static-block/class-declaration/input.js
Lines 1 to 6 in 8523873
will be transformed as
babel/packages/babel-plugin-proposal-class-static-block/test/fixtures/class-static-block/class-declaration/output.js
Lines 1 to 6 in 8523873
You can also try out at Babel REPL.
/cc @rbuckton This approach is slightly different to the "Motivation" section of the proposal, where the static block statements are inserted after class definitions. I think this approach will be more convenient for implementors. They can reuse static methods which already provide proper access to private elements and super properties.
I didn't try to implement this feature in @babel/plugin-helper-create-class-features-plugin because I think the class static block features can be polyfilled by static class methods mentioned above. So it does not rely on how other class features (properties, private methods) are transformed.