Skip to content

Commit 7d0b8a1

Browse files
committed
fix(transformer/typescript): panic occurs when declare property and definite property that has initializer (#13785)
Remove two incorrect assertions, which cause panic in the #13766. The `declare` property with an initializer is allowed in [TypeScript](https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=7&noCheck=false&stripInternal=true&ts=5.9.2#code/MYGwhgzhAEAiCmowCd4FEAeYC2AHE80A3gFDTQAmi4q0qYFA9gHYgCe0ARitALzQAiAC7wIQgQG4ylaikL0mrDgDNGjPtACMUgL4kSSKHHjKAls1MjMOfIVLk68Bi3ZcUAQg3DR4qQ4UuKmqe-NokekA) ```ts class DeclareExample { declare readonly bar = "test"; declare readonly foo = 1; } ``` The `!` (definite) with an initializer isn't allowed in [TypeScript](https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=7&noCheck=false&stripInternal=true&ts=5.9.2#code/MYGwhgzhAEAiCmowCd4FEAeYC2AHE80A3gFDTQAmi4q0qYFA9gHYgCe0ARitALzQAiAC7wIQgQG4ylaikL0mrDgDNGjPtACMUgL5A), but this is a recoverable error, so the AST can have such. ```ts class DefiniteExample { readonly bar! = "test"; readonly foo! = 1; } ```
1 parent 26af302 commit 7d0b8a1

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,6 @@ impl<'a> Traverse<'a, TransformState<'a>> for TypeScriptAnnotations<'a, '_> {
343343
def: &mut PropertyDefinition<'a>,
344344
_ctx: &mut TraverseCtx<'a>,
345345
) {
346-
assert!(
347-
!(def.declare && def.value.is_some()),
348-
"Fields with the 'declare' modifier cannot be initialized here, but only in the constructor"
349-
);
350-
351-
assert!(
352-
!(def.definite && def.value.is_some()),
353-
"Definitely assigned fields cannot be initialized here, but only in the constructor"
354-
);
355-
356346
def.accessibility = None;
357347
def.definite = false;
358348
def.r#override = false;

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 41d96516
22

3-
Passed: 186/313
3+
Passed: 187/314
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -63,7 +63,7 @@ after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), R
6363
rebuilt : SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), ReferenceId(10)]
6464

6565

66-
# babel-plugin-transform-typescript (5/27)
66+
# babel-plugin-transform-typescript (6/28)
6767
* allow-declare-fields-false/input.ts
6868
Unresolved references mismatch:
6969
after transform: ["dce"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test declare fields with initializers vs definite assignment assertions
2+
class DeclareExample {
3+
declare readonly bar = "test";
4+
declare readonly foo = 1;
5+
}
6+
7+
class DefiniteExample {
8+
readonly bar! = "test";
9+
readonly foo! = 1;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DeclareExample {}
2+
class DefiniteExample {
3+
bar = "test";
4+
foo = 1;
5+
}

0 commit comments

Comments
 (0)