mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
8c936018d1
Fixes an isolated declarations diagnostic mismatch for enum initializers. TypeScript allows runtime enum initializers under `--isolatedDeclarations` for regular enums and emits the declaration without initializer values: [TS playground](https://www.typescriptlang.org/play/?isolatedDeclarations=true#code/KYDwDg9gTgLgBMAdgVwLZwEIEMAmBRFdAbwCg44BBOAXjgFksYALAOii0RwlQAoBKADRlMNSnADUcAIxCAviRKhIsOAGMIiAM7wkaTLgDCG7QT2lyVWg2ZsOXXoOEZRVSTJLygA) ```ts export enum BadEnum { A = Math.random(), B = A + 1, } ``` ```ts export declare enum BadEnum { A, B } ``` Oxc already emitted the same declaration shape, but also reported TS9020. This PR narrows that diagnostic behavior so regular enum runtime initializers are omitted without an isolated declarations error. For `const enum`, TypeScript still reports `TS2474` because const enum initializers must be constant expressions: [TS playground](https://www.typescriptlang.org/play/?isolatedDeclarations=true#code/KYDwDg9gTgLgBMAdgVwLZwEIEMAmBRFdAbwCg44BBOAXjgFksYALAOii0RwlQAoBKADRlMNSnADUcAIxCAviRKhIsOAGMIiAM7wkaTLgDCG7QT2lyVWg2ZsOXXoOEZRVSTJLygA) ```ts export const enum BadConstEnum { A = Math.random(), B = A + 1, } ``` This PR keeps that error behavior aligned by reporting `TS2474` for non-constant `const enum` initializers.
38 lines
712 B
Plaintext
38 lines
712 B
Plaintext
---
|
|
source: crates/oxc_isolated_declarations/tests/mod.rs
|
|
input_file: crates/oxc_isolated_declarations/tests/fixtures/enum-runtime-initializer.ts
|
|
---
|
|
```
|
|
==================== .D.TS ====================
|
|
|
|
export declare enum BadEnum {
|
|
A,
|
|
B
|
|
}
|
|
export declare const enum BadConstEnum {
|
|
A,
|
|
B
|
|
}
|
|
|
|
|
|
==================== Errors ====================
|
|
|
|
x TS2474: const enum member initializers must be constant expressions.
|
|
,-[7:7]
|
|
6 | export const enum BadConstEnum {
|
|
7 | A = Math.random(),
|
|
: ^^^^^^^^^^^^^
|
|
8 | B = A + 1,
|
|
`----
|
|
|
|
x TS2474: const enum member initializers must be constant expressions.
|
|
,-[8:7]
|
|
7 | A = Math.random(),
|
|
8 | B = A + 1,
|
|
: ^^^^^
|
|
9 | }
|
|
`----
|
|
|
|
|
|
```
|