Skip to content

Add "ignoreUninitialized" option to "@babel/proposal-class-properties"#9141

Closed
nicolo-ribaudo wants to merge 1 commit intobabel:masterfrom
nicolo-ribaudo:class-fields-ignoreUninitialized
Closed

Add "ignoreUninitialized" option to "@babel/proposal-class-properties"#9141
nicolo-ribaudo wants to merge 1 commit intobabel:masterfrom
nicolo-ribaudo:class-fields-ignoreUninitialized

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 7, 2018

Q                       A
Fixed Issues? Fixes #9105, fixes #8417, closes #8584, fixes #7233
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR introduces a new option to the @babel/plugin-proposal-class-properties plugin: ignoreUninitialized. When it is enabled, foo; and foo: type; are removed instead of being (correctly) transpiled as if they were foo = undefined;.

This is needed to support the non-standard behavior of Flow and TypeScript, which use uninitialized fields as build-time-only type annotations instead of undefined fields (as mandated by the proposal). This bug has already been reported to those projects (microsoft/TypeScript#12437, facebook/flow#6811).

When documenting this option we must add a warning that if you are using this option, your code will be incompatible with fields natively implemented by JS engines. We should propose this as an option to make the Flow/TypeScript -> Babel migration easer, but we should encurage users to refactor their code to follow the current proposal.

cc @babel/typescript @babel/flow

When this option is set to true, Babel ignores public class fields
without an initializer. e.g. This code:

    class A { x; }

becomes

    class A {}

This behavior, althought it diverges from the class fields proposal and
thus isn't reccomended, matches Flow and TypeScript's behavior.
@nicolo-ribaudo nicolo-ribaudo force-pushed the class-fields-ignoreUninitialized branch from 9778059 to f4f7a62 Compare December 13, 2018 13:52
@babel-bot
Copy link
Copy Markdown
Collaborator

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

@vbraun
Copy link
Copy Markdown

vbraun commented Jan 5, 2019

Is this going to be merged? Whether or not typescript changes their uninitialized field behavior, it will take quite some time for libraries to be updated for the breaking change. So to make @babel/typescript feasible today there needs to be a way to match tsc.

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

I'm closing this plugin since we are not going to diverge from the spec on pourpouse.
This can be implemented as a separate unofficial plugin that strips away class fields before that proposal-class-properties runs.

@nicolo-ribaudo nicolo-ribaudo deleted the class-fields-ignoreUninitialized branch January 31, 2021 23:50
@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 May 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: flow area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Class Fields

Projects

None yet

3 participants