Skip to content
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

fix(es/transforms): handle member obj in missing else branch #7027

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crates/swc_ecma_transforms_optimization/src/const_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ impl VisitMut for ConstModules {
}
}
Expr::Member(MemberExpr { obj, prop, .. }) if obj.is_ident() => {
let member_obj = obj.as_ident().unwrap();

if self.scope.namespace.contains(&member_obj.to_id()) {
let module_name = &member_obj.sym;

if let Some(module_name) = obj
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not manage to move this if statement to the match arm.

.as_ident()
.filter(|member_obj| self.scope.namespace.contains(&member_obj.to_id()))
.map(|member_obj| &member_obj.sym)
{
let imported_name = match prop {
MemberProp::Ident(ref id) => &id.sym,
MemberProp::Computed(ref p) => match &*p.expr {
Expand All @@ -188,10 +188,12 @@ impl VisitMut for ConstModules {
});

*n = (**value).clone();
} else {
n.visit_mut_children_with(self);
}
}
e => {
e.visit_mut_children_with(self);
_ => {
n.visit_mut_children_with(self);
}
};
}
Expand Down
18 changes: 18 additions & 0 deletions crates/swc_ecma_transforms_optimization/tests/const_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ console.log(foo["bar"])
console.log(true);
"#
);

test!(
::swc_ecma_parser::Syntax::default(),
|tester| tr(
tester,
&[("testModule", &[("testMap", "{ 'var': 'value' }")])]
),
issue_7025,
r#"
import { testMap } from "testModule";
testMap['var'];
"#,
r#"
({
'var': 'value'
})['var'];
"#
);