-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Description
Describe the bug
SWC does not treat a local const string variable used in a TypeScript enum initializer as a compile-time constant.
For the input below, TypeScript 5.9 emits a string enum shape (E["A"] = "aThisIs"), but SWC emits reverse-mapping style output (E[E["A"] = foo] = "A"), which changes runtime behavior and enum shape.
Input code
const foo = 'aThisIs'
enum E {
A = foo,
B = 'bThisIs'
}Config
{
"jsc": {
"parser": {
"syntax": "typescript"
}
}
}Link to the code that reproduces this issue
SWC Info output
$ npx -y -p @swc/cli@latest -p @swc/core@latest swc -V
@swc/cli: 0.8.0
@swc/core: 1.15.18
Expected behavior
SWC should match TypeScript enum lowering semantics for this case (string enum, no reverse mapping for A), e.g.:
"use strict";
const foo = 'aThisIs';
var E;
(function (E) {
E["A"] = "aThisIs";
E["B"] = "bThisIs";
})(E || (E = {}));Actual behavior
SWC currently emits:
var foo = 'aThisIs';
var E = function(E) {
E[E["A"] = foo] = "A";
E["B"] = "bThisIs";
return E;
}(E || {});Version
@swc/core 1.15.18 (@swc/cli 0.8.0)
Additional context
- Original downstream report: The generation of enum did not consider compile-time constant variables nodejs/amaro#394
- Related esbuild issue: Incorrect code for enums generated evanw/esbuild#4387
Reactions are currently unavailable