Skip to content

Commit

Permalink
fix: actually fix var_decl_imports with jsx automatic (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 2, 2023
1 parent a37fedf commit da602a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ function App() {
..Default::default()
};
let code = module.transpile(&emit_options).unwrap().text;
let expected = r#"/** @jsxImportSource jsx_lib */ const { "jsx": _jsx1, "Fragment": _Fragment1 } = await import("jsx_lib/jsx-runtime");
let expected = r#"/** @jsxImportSource jsx_lib */ const { "jsx": _jsx, "Fragment": _Fragment } = await import("jsx_lib/jsx-runtime");
function App() {
return /*#__PURE__*/ _jsx("div", {
children: /*#__PURE__*/ _jsx(_Fragment, {})
Expand Down
15 changes: 7 additions & 8 deletions src/transpiling/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Fold for ImportDeclsToVarDeclsFolder {
.filter_map(|specifier| match specifier {
ImportSpecifier::Default(specifier) => Some(create_key_value(
"default".to_string(),
specifier.local.sym.to_string(),
specifier.local.clone(),
)),
ImportSpecifier::Named(specifier) => {
Some(match specifier.imported.as_ref() {
Expand All @@ -56,7 +56,7 @@ impl Fold for ImportDeclsToVarDeclsFolder {
ModuleExportName::Ident(ident) => ident.sym.to_string(),
ModuleExportName::Str(str) => str.value.to_string(),
},
specifier.local.sym.to_string(),
specifier.local.clone(),
),
None => create_assignment(specifier.local.clone()),
})
Expand Down Expand Up @@ -199,7 +199,10 @@ fn create_ident(name: String) -> swc_ast::Ident {
}
}

fn create_key_value(key: String, value: String) -> swc_ast::ObjectPatProp {
fn create_key_value(
key: String,
value: swc_ast::Ident,
) -> swc_ast::ObjectPatProp {
swc_ast::ObjectPatProp::KeyValue(swc_ast::KeyValuePatProp {
// use a string literal because it will work in more scenarios than an identifier
key: swc_ast::PropName::Str(swc_ast::Str {
Expand All @@ -208,11 +211,7 @@ fn create_key_value(key: String, value: String) -> swc_ast::ObjectPatProp {
raw: None,
}),
value: Box::new(swc_ast::Pat::Ident(swc_ast::BindingIdent {
id: swc_ast::Ident {
span: DUMMY_SP,
sym: value.into(),
optional: false,
},
id: value,
type_ann: None,
})),
})
Expand Down

0 comments on commit da602a5

Please sign in to comment.