-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(es/typescript): Treat const variable references as enum constants #12101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3fee92c
c093ba8
0851e3e
d547a9a
dd1035a
f6f9b27
8eeebc5
2e8749f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| swc_core: patch | ||
| swc_ecma_transforms_typescript: patch | ||
| --- | ||
| fix(es/typescript): Treat const variable references as enum constants |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| //// [constEnum2.ts] | ||
| var D, D1 = ((D = D1 || {})[D.e = 199 * Math.floor(1000 * Math.random())] = "e", D[D.f = 10 - 100 * Math.floor(Math.random() % 8)] = "f", D[D.g = 0] = "g", D); | ||
| var D, D1 = ((D = D1 || {})[D.e = 199 * Math.floor(1000 * Math.random())] = "e", D[D.f = 10 - 100 * Math.floor(Math.random() % 8)] = "f", D); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; | |
| use crate::{ | ||
| retain::{should_retain_decl, IsConcrete}, | ||
| shared::{enum_member_name, get_module_ident}, | ||
| ts_enum::{EnumValueComputer, TsEnumRecord, TsEnumRecordKey, TsEnumRecordValue}, | ||
| ts_enum::{EnumValueComputer, EvalCtx, TsEnumRecord, TsEnumRecordKey, TsEnumRecordValue}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Default)] | ||
|
|
@@ -19,6 +19,7 @@ pub(crate) struct SemanticInfo { | |
| pub enum_record: TsEnumRecord, | ||
| pub const_enum: FxHashSet<Id>, | ||
| pub namespace_import_equals_usage: FxHashSet<Span>, | ||
| pub const_vars: FxHashMap<Id, TsEnumRecordValue>, | ||
| } | ||
|
|
||
| impl SemanticInfo { | ||
|
|
@@ -48,6 +49,7 @@ pub(crate) fn analyze_program( | |
| unresolved_mark: Mark, | ||
| seed_usage: FxHashSet<Id>, | ||
| flow_syntax: bool, | ||
| ts_enum_is_mutable: bool, | ||
| ) -> SemanticInfo { | ||
| let mut analyzer = SemanticAnalyzer { | ||
| unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), | ||
|
|
@@ -60,6 +62,7 @@ pub(crate) fn analyze_program( | |
| namespace_id: None, | ||
| skip_transform_info: false, | ||
| flow_syntax, | ||
| ts_enum_is_mutable, | ||
| }; | ||
|
|
||
| program.visit_with(&mut analyzer); | ||
|
|
@@ -75,6 +78,7 @@ struct SemanticAnalyzer { | |
| namespace_id: Option<Id>, | ||
| skip_transform_info: bool, | ||
| flow_syntax: bool, | ||
| ts_enum_is_mutable: bool, | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
|
|
@@ -257,6 +261,7 @@ impl SemanticAnalyzer { | |
| enum_id: &Id, | ||
| default_init: &TsEnumRecordValue, | ||
| record: &TsEnumRecord, | ||
| const_vars: &FxHashMap<Id, TsEnumRecordValue>, | ||
| unresolved_ctxt: SyntaxContext, | ||
| flow_syntax: bool, | ||
| ) -> TsEnumRecordValue { | ||
|
|
@@ -267,8 +272,10 @@ impl SemanticAnalyzer { | |
| enum_id, | ||
| unresolved_ctxt, | ||
| record, | ||
| const_vars, | ||
| const_enum_only: None, | ||
| } | ||
| .compute(expr) | ||
| .compute(expr, EvalCtx::MEMBER) | ||
| }) | ||
| .filter(TsEnumRecordValue::has_value) | ||
| .unwrap_or_else(|| { | ||
|
|
@@ -536,6 +543,40 @@ impl Visit for SemanticAnalyzer { | |
| } | ||
| } | ||
|
|
||
| fn visit_var_decl(&mut self, node: &VarDecl) { | ||
| let track = !self.skip_transform_info && !node.declare && node.kind == VarDeclKind::Const; | ||
|
|
||
| for decl in &node.decls { | ||
| decl.visit_with(self); | ||
|
|
||
| if !track { | ||
| continue; | ||
| } | ||
|
|
||
| let Pat::Ident(BindingIdent { id, type_ann: None }) = &decl.name else { | ||
| continue; | ||
| }; | ||
| let Some(init) = &decl.init else { continue }; | ||
|
|
||
| if !EnumValueComputer::can_fold_shape(init) { | ||
| continue; | ||
| } | ||
|
|
||
| let value = EnumValueComputer { | ||
| enum_id: &id.to_id(), | ||
| unresolved_ctxt: self.unresolved_ctxt, | ||
| record: &self.info.enum_record, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — this was a real regression, fixed in The option is now threaded into the collection pass and mirrors the guard in Separately, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a const initializer reads an ambient enum member, e.g. Useful? React with 👍 / 👎. |
||
| const_vars: &self.info.const_vars, | ||
| const_enum_only: self.ts_enum_is_mutable.then_some(&self.info.const_enum), | ||
| } | ||
| .compute(init.clone(), EvalCtx::CONST_INIT); | ||
|
|
||
| if value.is_const() { | ||
| self.info.const_vars.insert(id.to_id(), value); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the const is exported from a namespace, e.g. Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed against |
||
| } | ||
| } | ||
| } | ||
|
|
||
| fn visit_ts_enum_decl(&mut self, node: &TsEnumDecl) { | ||
| node.visit_children_with(self); | ||
|
|
||
|
|
@@ -566,6 +607,7 @@ impl Visit for SemanticAnalyzer { | |
| &id.to_id(), | ||
| &default_init, | ||
| &self.info.enum_record, | ||
| &self.info.const_vars, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an enum is inside a function-like body that appears before an outer const declaration, e.g. Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed against |
||
| self.unresolved_ctxt, | ||
| self.flow_syntax, | ||
| ); | ||
|
|
@@ -655,6 +697,7 @@ mod tests { | |
| &id("E"), | ||
| &TsEnumRecordValue::Void, | ||
| &Default::default(), | ||
| &Default::default(), | ||
| SyntaxContext::empty(), | ||
| true, | ||
| ); | ||
|
|
@@ -672,6 +715,7 @@ mod tests { | |
| &id("E"), | ||
| &TsEnumRecordValue::from(2.0), | ||
| &Default::default(), | ||
| &Default::default(), | ||
| SyntaxContext::empty(), | ||
| false, | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ambient literal const declarations such as
declare const x = 1; enum E { A = x, B }, TypeScript erases the declaration but still classifiesxas a constant enum expression and emitsA = 1/B = 2. Becausetrackrejects everydeclarevar decl here, the declaration is stripped whilexis never added toconst_vars, so the enum is emitted as a runtime read of a non-emitted binding and the following member becomesundefined; allow ambientdeclare constdeclarations with constant initializers to populate the semantic map without retaining them.Useful? React with 👍 / 👎.