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 object shorthand prop in const_modules #7022

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/swc_ecma_transforms_optimization/src/const_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,26 @@ impl VisitMut for ConstModules {
}
};
}

fn visit_mut_prop(&mut self, n: &mut Prop) {
match n {
Prop::Shorthand(id) => {
if let Some(value) = self.scope.imported.get(&id.sym) {
*n = Prop::KeyValue(KeyValueProp {
key: id.take().into(),
value: Box::new((**value).clone()),
});
return;
}

if let Some(..) = self.scope.namespace.get(&id.to_id()) {
panic!(
"The const_module namespace `{}` cannot be used without member accessor",
id.sym
)
}
}
_ => n.visit_mut_children_with(self),
}
}
}
13 changes: 13 additions & 0 deletions crates/swc_ecma_transforms_optimization/tests/const_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,16 @@ test!(
})['var'];
"#
);

test!(
::swc_ecma_parser::Syntax::default(),
|tester| tr(tester, &[("foo", &[("bar", "true")])]),
use_as_object_prop_shorthand,
r#"
import { bar } from 'foo';
console.log({ bar });
"#,
r#"
console.log({ bar: true });
"#
);