fix transform-typescript logic to remove definite fields (#12149)
This commit is contained in:
parent
e6669988d0
commit
f6bd7493a8
@ -69,17 +69,26 @@ export default declare(
|
|||||||
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`,
|
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (node.definite || node.declare) {
|
if (node.declare) {
|
||||||
if (node.value) {
|
if (node.value) {
|
||||||
throw path.buildCodeFrameError(
|
throw path.buildCodeFrameError(
|
||||||
`Definitely assigned fields and fields with the 'declare' modifier cannot` +
|
`Fields with the 'declare' modifier cannot be initialized here, but only in the constructor`,
|
||||||
` be initialized here, but only in the constructor`,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!node.decorators) {
|
if (!node.decorators) {
|
||||||
path.remove();
|
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 (
|
} else if (
|
||||||
!allowDeclareFields &&
|
!allowDeclareFields &&
|
||||||
!node.value &&
|
!node.value &&
|
||||||
|
|||||||
@ -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 {
|
class A {
|
||||||
x!;
|
x!;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1 +1,3 @@
|
|||||||
class A {}
|
class A {
|
||||||
|
x;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user