-
-
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 3 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 { | ||
|
|
@@ -257,6 +258,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 +269,9 @@ impl SemanticAnalyzer { | |
| enum_id, | ||
| unresolved_ctxt, | ||
| record, | ||
| const_vars, | ||
| } | ||
| .compute(expr) | ||
| .compute(expr, EvalCtx::MEMBER) | ||
| }) | ||
| .filter(TsEnumRecordValue::has_value) | ||
| .unwrap_or_else(|| { | ||
|
|
@@ -536,6 +539,34 @@ impl Visit for SemanticAnalyzer { | |
| } | ||
| } | ||
|
|
||
| fn visit_var_decl(&mut self, node: &VarDecl) { | ||
| node.visit_children_with(self); | ||
|
|
||
| if self.skip_transform_info || node.declare || node.kind != VarDeclKind::Const { | ||
| return; | ||
| } | ||
|
|
||
| for decl in &node.decls { | ||
| let Pat::Ident(BindingIdent { id, type_ann: None }) = &decl.name else { | ||
| continue; | ||
| }; | ||
| let Some(init) = &decl.init else { continue }; | ||
|
|
||
| let empty = TsEnumRecord::default(); | ||
| let value = EnumValueComputer { | ||
| enum_id: &id.to_id(), | ||
| unresolved_ctxt: self.unresolved_ctxt, | ||
| record: &empty, | ||
|
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 contains an enum member reference, 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. Fixed in |
||
| const_vars: &self.info.const_vars, | ||
| } | ||
| .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 +597,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 +687,7 @@ mod tests { | |
| &id("E"), | ||
| &TsEnumRecordValue::Void, | ||
| &Default::default(), | ||
| &Default::default(), | ||
| SyntaxContext::empty(), | ||
| true, | ||
| ); | ||
|
|
@@ -672,6 +705,7 @@ mod tests { | |
| &id("E"), | ||
| &TsEnumRecordValue::from(2.0), | ||
| &Default::default(), | ||
| &Default::default(), | ||
| SyntaxContext::empty(), | ||
| false, | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| const s = "aThisIs"; | ||
| const n = 1; | ||
| const concat = "a" + "b"; | ||
| const chain = s; | ||
|
|
||
| enum StringEnum { | ||
| A = s, | ||
| B = "bThisIs", | ||
| } | ||
|
|
||
| enum NumericAutoIncrement { | ||
| A = n, | ||
| B, | ||
| C, | ||
| } | ||
|
|
||
| enum ConstantExpr { | ||
| A = concat, | ||
| B = chain, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| const s = "aThisIs"; | ||
| const n = 1; | ||
| const concat = "a" + "b"; | ||
| const chain = s; | ||
| var StringEnum = /*#__PURE__*/ function(StringEnum) { | ||
| StringEnum["A"] = "aThisIs"; | ||
| StringEnum["B"] = "bThisIs"; | ||
| return StringEnum; | ||
| }(StringEnum || {}); | ||
| var NumericAutoIncrement = /*#__PURE__*/ function(NumericAutoIncrement) { | ||
| NumericAutoIncrement[NumericAutoIncrement["A"] = 1] = "A"; | ||
| NumericAutoIncrement[NumericAutoIncrement["B"] = 2] = "B"; | ||
| NumericAutoIncrement[NumericAutoIncrement["C"] = 3] = "C"; | ||
| return NumericAutoIncrement; | ||
| }(NumericAutoIncrement || {}); | ||
| var ConstantExpr = /*#__PURE__*/ function(ConstantExpr) { | ||
| ConstantExpr["A"] = "ab"; | ||
| ConstantExpr["B"] = "aThisIs"; | ||
| return ConstantExpr; | ||
| }(ConstantExpr || {}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const annotated: string = "s"; | ||
| const asserted = "s" as string; | ||
| const constAsserted = "s" as const; | ||
| let mutable = "s"; | ||
| var hoisted = "s"; | ||
| const { destructured } = { destructured: "s" }; | ||
| const poisoned = "s"; | ||
|
|
||
| enum TypeAnnotation { | ||
| A = annotated, | ||
| B = "b", | ||
| } | ||
|
|
||
| enum TypeAssertion { | ||
| A = asserted, | ||
| B = constAsserted, | ||
| } | ||
|
|
||
| enum NotConstBinding { | ||
| A = mutable, | ||
| B = hoisted, | ||
| C = destructured, | ||
| } | ||
|
|
||
| enum AssertionOnReference { | ||
| A = poisoned as string, | ||
| B = "b", | ||
| } | ||
|
|
||
| enum ForwardReference { | ||
| A = declaredLater, | ||
| B = "b", | ||
| } | ||
|
|
||
| const declaredLater = "s"; |
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 a multi-declarator const like
const x = 1, y = (() => { enum E { A = x, B } })();, TypeScript folds the nested enum toA = 1and auto-incrementsB = 2. This visitor walksy's initializer before inserting the earlierxdeclarator intoconst_vars, so the nested enum is recorded as opaque and emitsA = xwithB = undefined; collect eligible preceding declarators before traversing later initializer bodies.Useful? React with 👍 / 👎.
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.
Fixed in
d547a9a. Each declarator is recorded before the next one is traversed. Verified againsttsc5.9.3; fixtureissue-11715-declarator-order, which fails onmain.