Skip to content

Commit

Permalink
fix(es/transforms): Handle object shorthand prop in const_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Mar 7, 2023
1 parent e93c79b commit 7edf30e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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 });
"#
);

0 comments on commit 7edf30e

Please sign in to comment.