Skip to content

Commit

Permalink
fix transform-typescript logic to remove definite fields (#12149)
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Oct 8, 2020
1 parent e666998 commit f6bd749
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
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;
}

0 comments on commit f6bd749

Please sign in to comment.