From da602a5bee0819f1fdfda744fe33e6d85824fc56 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 1 Oct 2023 20:12:12 -0400 Subject: [PATCH] fix: actually fix var_decl_imports with jsx automatic (#159) --- src/transpiling/mod.rs | 2 +- src/transpiling/transforms.rs | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/transpiling/mod.rs b/src/transpiling/mod.rs index e899e0c..89b7771 100644 --- a/src/transpiling/mod.rs +++ b/src/transpiling/mod.rs @@ -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, {}) diff --git a/src/transpiling/transforms.rs b/src/transpiling/transforms.rs index 2893776..fda8ac8 100644 --- a/src/transpiling/transforms.rs +++ b/src/transpiling/transforms.rs @@ -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() { @@ -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()), }) @@ -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 { @@ -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, })), })