diff --git a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/operator.rs b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/operator.rs index 906e53805b3..0e4e2d99e85 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/operator.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/operator.rs @@ -1,4 +1,4 @@ -use swc_common::{collections::AHashMap, SyntaxContext}; +use swc_common::{collections::AHashMap, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use swc_trace_macro::swc_trace; @@ -26,6 +26,51 @@ impl VisitMut for Rename { } } + fn visit_mut_member_prop(&mut self, n: &mut MemberProp) { + if let MemberProp::Computed(n) = n { + n.visit_mut_with(self); + } + } + + fn visit_mut_object_pat_prop(&mut self, i: &mut ObjectPatProp) { + match i { + ObjectPatProp::Assign(p) => { + p.value.visit_mut_with(self); + + let orig = p.key.clone(); + p.key.visit_mut_with(self); + + if orig.to_id() == p.key.to_id() { + return; + } + + match p.value.take() { + Some(default) => { + *i = ObjectPatProp::KeyValue(KeyValuePatProp { + key: PropName::Ident(orig), + value: Box::new(Pat::Assign(AssignPat { + span: DUMMY_SP, + left: Box::new(Pat::Ident(p.key.clone().into())), + right: default, + type_ann: Default::default(), + })), + }); + } + None => { + *i = ObjectPatProp::KeyValue(KeyValuePatProp { + key: PropName::Ident(orig), + value: Box::new(Pat::Ident(p.key.clone().into())), + }); + } + } + } + + _ => { + i.visit_mut_children_with(self); + } + } + } + fn visit_mut_prop(&mut self, n: &mut Prop) { if let Prop::Shorthand(ident) = n { if let Some(id) = self.map.get(&ident.to_id()) { @@ -42,12 +87,6 @@ impl VisitMut for Rename { n.visit_mut_children_with(self); } - fn visit_mut_member_prop(&mut self, n: &mut MemberProp) { - if let MemberProp::Computed(n) = n { - n.visit_mut_with(self); - } - } - fn visit_mut_prop_name(&mut self, n: &mut PropName) { if let PropName::Computed(n) = n { n.visit_mut_with(self); diff --git a/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-7418/1/exec.js b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-7418/1/exec.js new file mode 100644 index 00000000000..2ae84181bbd --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-7418/1/exec.js @@ -0,0 +1,11 @@ +const truc = { as: "OK" } + +function x(as) { + return function g() { + const { as } = truc + console.log(as) + } + as +} + +x()(); \ No newline at end of file diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs index 3b010b46638..53294454709 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs @@ -2280,3 +2280,22 @@ test!( "let [] = [...[1, 2, 3]];", "let _ref = [...[1, 2, 3]];" ); + +test_exec!( + syntax(), + |_| tr(), + issue_7418, + r###" + const truc = { as: "OK"} + + function x(as) { + return function g() { + const { as } = truc + console.log(as) + } + as + } + + x()(); + "### +); diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index df0fbd412f4..1db5aea3f00 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -2922,6 +2922,71 @@ impl VisitMut for IdentRenamer<'_> { visit_mut_obj_and_computed!(); + fn visit_mut_export_named_specifier(&mut self, node: &mut ExportNamedSpecifier) { + if node.exported.is_some() { + node.orig.visit_mut_children_with(self); + return; + } + + match &mut node.orig { + ModuleExportName::Ident(orig) => { + if let Some(new) = self.map.get(&orig.to_id()) { + node.exported = Some(ModuleExportName::Ident(orig.clone())); + + orig.sym = new.0.clone(); + orig.span.ctxt = new.1; + } + } + ModuleExportName::Str(_) => {} + } + } + + fn visit_mut_ident(&mut self, node: &mut Ident) { + if let Some(new) = self.map.get(&node.to_id()) { + node.sym = new.0.clone(); + node.span.ctxt = new.1; + } + } + + fn visit_mut_object_pat_prop(&mut self, i: &mut ObjectPatProp) { + match i { + ObjectPatProp::Assign(p) => { + p.value.visit_mut_with(self); + + let orig = p.key.clone(); + p.key.visit_mut_with(self); + + if orig.to_id() == p.key.to_id() { + return; + } + + match p.value.take() { + Some(default) => { + *i = ObjectPatProp::KeyValue(KeyValuePatProp { + key: PropName::Ident(orig), + value: Box::new(Pat::Assign(AssignPat { + span: DUMMY_SP, + left: Box::new(Pat::Ident(p.key.clone().into())), + right: default, + type_ann: Default::default(), + })), + }); + } + None => { + *i = ObjectPatProp::KeyValue(KeyValuePatProp { + key: PropName::Ident(orig), + value: Box::new(Pat::Ident(p.key.clone().into())), + }); + } + } + } + + _ => { + i.visit_mut_children_with(self); + } + } + } + fn visit_mut_prop(&mut self, node: &mut Prop) { match node { Prop::Shorthand(i) => { @@ -2942,32 +3007,6 @@ impl VisitMut for IdentRenamer<'_> { } } } - - fn visit_mut_ident(&mut self, node: &mut Ident) { - if let Some(new) = self.map.get(&node.to_id()) { - node.sym = new.0.clone(); - node.span.ctxt = new.1; - } - } - - fn visit_mut_export_named_specifier(&mut self, node: &mut ExportNamedSpecifier) { - if node.exported.is_some() { - node.orig.visit_mut_children_with(self); - return; - } - - match &mut node.orig { - ModuleExportName::Ident(orig) => { - if let Some(new) = self.map.get(&orig.to_id()) { - node.exported = Some(ModuleExportName::Ident(orig.clone())); - - orig.sym = new.0.clone(); - orig.span.ctxt = new.1; - } - } - ModuleExportName::Str(_) => {} - } - } } #[cfg(test)] diff --git a/crates/swc_plugin_runner/src/transform_executor.rs b/crates/swc_plugin_runner/src/transform_executor.rs index fa023407485..7010758d9cb 100644 --- a/crates/swc_plugin_runner/src/transform_executor.rs +++ b/crates/swc_plugin_runner/src/transform_executor.rs @@ -297,7 +297,7 @@ impl TransformExecutor { let memory = instance.exports.get_memory("memory")?; import_object.define("env", "memory", memory.clone()); - let alloc = instance.exports.get_typed_function(&mut store, "__alloc")?; + let alloc = instance.exports.get_typed_function(&store, "__alloc")?; // Unlike wasmer@2, have to manually `import` memory / necessary functions from // the guest into env.