mirror of
https://github.com/swc-project/swc.git
synced 2024-10-04 12:18:08 +03:00
fix(es/compat): Fix destructuring handling of block_scoping
(#7425)
**Related issue:** - Closes #7418.
This commit is contained in:
parent
344a6ea7be
commit
66d52ec849
@ -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);
|
||||
|
@ -0,0 +1,11 @@
|
||||
const truc = { as: "OK" }
|
||||
|
||||
function x(as) {
|
||||
return function g() {
|
||||
const { as } = truc
|
||||
console.log(as)
|
||||
}
|
||||
as
|
||||
}
|
||||
|
||||
x()();
|
@ -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()();
|
||||
"###
|
||||
);
|
||||
|
@ -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)]
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user