fix(es/typescript): Support ts_enum_is_mutable with const enums (#8171)

This commit is contained in:
David Sherret 2023-10-23 16:40:16 -04:00 committed by GitHub
parent 7932111fb9
commit 02d0613a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 5 deletions

View File

@ -84,7 +84,7 @@ impl VisitMut for Transform {
fn visit_mut_program(&mut self, n: &mut Program) {
n.visit_mut_children_with(self);
if !self.ts_enum_is_mutable && !self.record.is_empty() {
if !self.record.is_empty() {
let record = mem::take(&mut self.record);
n.visit_mut_children_with(&mut InlineEnum::new(record));
}
@ -487,6 +487,12 @@ impl Transform {
let mut default_init = 0.0.into();
let mut member_list = vec![];
let mut local_record = if self.ts_enum_is_mutable && !is_const {
Some(TsEnumRecord::default())
} else {
None
};
let record = local_record.as_mut().unwrap_or(&mut self.record);
for m in members {
let span = m.span;
@ -496,7 +502,7 @@ impl Transform {
m,
&id.to_id(),
&default_init,
&self.record,
record,
self.top_level_mark,
);
@ -514,7 +520,7 @@ impl Transform {
TsEnumRecordValue::Void
};
self.record.insert(key, value);
record.insert(key, value);
}
let item = EnumMemberItem { span, name, value };

View File

@ -4567,19 +4567,57 @@ test!(
r#"
enum D {
A,
B,
B = 2,
}
(D as any).A = 5;
console.log(D.A);
const enum E {
A,
B,
}
console.log(E.B);
const enum F {
A = 2,
}
enum G {
A = F.A
}
console.log(G.A);
enum H {
A = 2,
}
const enum I {
A = H.A
}
console.log(I.A);
"#,
r#"
var D;
(function(D) {
D[D["A"] = 0] = "A";
D[D["B"] = 1] = "B";
D[D["B"] = 2] = "B";
})(D || (D = {}));
D.A = 5;
console.log(D.A);
var E;
console.log(1);
var F;
var G;
(function(G) {
G[G["A"] = 2] = "A";
})(G || (G = {}));
console.log(G.A);
var H;
(function(H) {
H[H["A"] = 2] = "A";
})(H || (H = {}));
var I;
(function(I) {
I[I["A"] = H.A] = "A";
})(I || (I = {}));
console.log(I.A);
"#
);