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

fix transform-typescript logic to remove definite fields #12149

Merged
merged 1 commit into from Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/babel-plugin-transform-typescript/src/index.js
Expand Up @@ -69,17 +69,26 @@ export default declare(
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`,
);
}
if (node.definite || node.declare) {
if (node.declare) {
if (node.value) {
throw path.buildCodeFrameError(
`Definitely assigned fields and fields with the 'declare' modifier cannot` +
` be initialized here, but only in the constructor`,
`Fields with the 'declare' modifier cannot be initialized here, but only in the constructor`,
);
}

if (!node.decorators) {
path.remove();
}
} else if (node.definite) {
if (node.value) {
throw path.buildCodeFrameError(
`Definitely assigned fields cannot be initialized here, but only in the constructor`,
);
}
// keep the definitely assigned fields only when `allowDeclareFields` (equivalent of
// Typescript's `useDefineForClassFields`) is true
if (!allowDeclareFields && !node.decorators) {
path.remove();
}
} else if (
!allowDeclareFields &&
!node.value &&
Expand Down
@@ -0,0 +1,3 @@
class A {
x!;
}
@@ -0,0 +1,3 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": false }]]
}
@@ -0,0 +1 @@
class A {}
@@ -1,3 +1,3 @@
class A {
x!;
}
}
@@ -1 +1,3 @@
class A {}
class A {
x;
}