Update Class Fields to Stage 3 and change default behavior#6076
Update Class Fields to Stage 3 and change default behavior#6076
Conversation
|
i added the |
| `boolean`, defaults to `false`. | ||
|
|
||
| Class properties are compiled to use `Object.defineProperty`. Static fields are now defined even if they are not initialized. | ||
| Class properties are compiled use an assignment expression instead of `Object.defineProperty`. Static fields are now not defined if they are not initialized. |
There was a problem hiding this comment.
not sure how confident i am of the language here. does this capture the intended behavior?
There was a problem hiding this comment.
We can put the code example here to be more clear.
so an example of the defineProperty vs. this.x = a and also this.x = undefined
There was a problem hiding this comment.
added an example, though it may be a bit lengthy. let me know if you think we can trim it down a bit!
|
|
||
| // In non-spec mode, all properties without values are ignored. | ||
| // In loose mode, all properties without values are ignored. | ||
| // In spec mode, *static* properties without values are still defined (see below). |
There was a problem hiding this comment.
IIUC, in spec mode, all properties should be defined. This may also be a new change on the stage-3 version.
- https://tc39.github.io/proposal-class-fields/#runtime-semantics-class-field-definition-evaluation the Record is still created even if the Initializer is missing
- https://tc39.github.io/proposal-class-fields/#initialize-public-instance-fields InitializeInstanceFields does not care whether the record has an initializer
- https://tc39.github.io/proposal-class-fields/#sec-define-field DefineField will use an initValue of undefined if there is no initializer
There was a problem hiding this comment.
yep, looks like it to me too! i'll change that here.
There was a problem hiding this comment.
think i covered these now. if you've got a chance to take another look, let me know if anything is missing!
| @@ -36,7 +36,7 @@ export default function({ types: t }) { | |||
| VALUE: value ? value : t.identifier("undefined"), | |||
There was a problem hiding this comment.
Probably better to use t.unaryExpression('void', t.numericLiteral(0)) to avoid any potential shadowing issues (fix out of scope maybe?)
There was a problem hiding this comment.
we have a method for it path.scope.buildUndefinedNode( - btw this should be a linting rule in babel/run against babel?
There was a problem hiding this comment.
👍 i'll replace with buildUndefinedNode here
There was a problem hiding this comment.
@hzoo regarding linting, would the rule be to always disallow t.identifier("undefined") in favor of path.scope.buildUndefinedNode()?
There was a problem hiding this comment.
I think so at least for us (that can be another pr, can do an inline plugin in the babelrc.js if we wanted), not sure the best way
There was a problem hiding this comment.
gotcha. cool, maybe i'll take a swing at that after this PR
| `boolean`, defaults to `false`. | ||
|
|
||
| Class properties are compiled to use `Object.defineProperty`. Static fields are now defined even if they are not initialized. | ||
| Class properties are compiled use an assignment expression instead of `Object.defineProperty`. Static fields are now not defined if they are not initialized. |
There was a problem hiding this comment.
Why are static fields not initialized? They should be undefined, too.
There was a problem hiding this comment.
i think it was never added for the default mode (which is now changing to loose)
There was a problem hiding this comment.
yeah, i just kept the behavior that existed as default for the new "loose" mode -- should this be changed?
…ior to always define both static and nonstatic class properties regardless of spec/loose mode; update tests
|
Amazing work @kedromelon! |
|
😄 thanks for the help everyone! |
|
Was a pretty big refactor! Also unforunately all the PRs will conflict since we are changing 3 different plugins to stage 2/3 |
|
@kedromelon also I noticed you were branching off of 7.0? I would suggest making a new branch for each PR in case you have multiple, and easier to manage |
|
@hzoo yeah, i probably will do that in the future! just didn't bother yet since i haven't had more than one PR going |
transform-class-propertiesfrom stage 2 to 3.