fix transform-typescript logic to remove definite fields (#12149)

This commit is contained in:
An Phi 2020-10-08 13:35:43 -04:00 committed by GitHub
parent e6669988d0
commit f6bd7493a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 6 deletions

View File

@ -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 &&

View File

@ -0,0 +1,3 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": false }]]
}