Skip to content

Commit

Permalink
fix(transpile): var_decl_imports should work with jsx automatic (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 1, 2023
1 parent 03ba614 commit 313d55d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
43 changes: 39 additions & 4 deletions src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,6 @@ pub fn fold_program(

let unresolved_mark = Mark::new();
let mut passes = chain!(
Optional::new(
transforms::ImportDeclsToVarDeclsFolder,
options.var_decl_imports
),
Optional::new(transforms::StripExportsFolder, options.var_decl_imports),
resolver(unresolved_mark, top_level_mark, true),
proposal::decorators::decorators(proposal::decorators::Config {
Expand Down Expand Up @@ -344,6 +340,10 @@ pub fn fold_program(
),
options.transform_jsx
),
Optional::new(
transforms::ImportDeclsToVarDeclsFolder,
options.var_decl_imports
),
fixer(Some(comments)),
hygiene(),
);
Expand Down Expand Up @@ -818,6 +818,41 @@ function App() {
assert_eq!(&code[..expected.len()], expected);
}

#[test]
fn test_transpile_jsx_import_source_pragma_var_decl_imports() {
let specifier =
ModuleSpecifier::parse("https://deno.land/x/mod.tsx").unwrap();
let source = r#"
/** @jsxImportSource jsx_lib */
function App() {
return (
<div><></></div>
);
}"#;
let module = parse_module(ParseParams {
specifier: specifier.as_str().to_string(),
text_info: SourceTextInfo::from_string(source.to_string()),
media_type: MediaType::Jsx,
capture_tokens: false,
maybe_syntax: None,
scope_analysis: true,
})
.unwrap();
let emit_options = EmitOptions {
var_decl_imports: true,
..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");
function App() {
return /*#__PURE__*/ _jsx("div", {
children: /*#__PURE__*/ _jsx(_Fragment, {})
});
"#;
assert_eq!(&code[..expected.len()], expected);
}

#[test]
fn test_transpile_decorators() {
let specifier =
Expand Down
20 changes: 7 additions & 13 deletions src/transpiling/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Fold for ImportDeclsToVarDeclsFolder {
},
specifier.local.sym.to_string(),
),
None => create_assignment(specifier.local.sym.to_string()),
None => create_assignment(specifier.local.clone()),
})
}
ImportSpecifier::Namespace(_) => None,
Expand All @@ -69,9 +69,10 @@ impl Fold for ImportDeclsToVarDeclsFolder {
.specifiers
.iter()
.find_map(|specifier| match specifier {
ImportSpecifier::Namespace(specifier) => {
Some(create_binding_ident(specifier.local.sym.to_string()))
}
ImportSpecifier::Namespace(specifier) => Some(BindingIdent {
id: specifier.local.clone(),
type_ann: None,
}),
_ => None,
});

Expand Down Expand Up @@ -190,13 +191,6 @@ fn create_empty_stmt() -> swc_ast::ModuleItem {
ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }))
}

fn create_binding_ident(name: String) -> swc_ast::BindingIdent {
swc_ast::BindingIdent {
id: create_ident(name),
type_ann: None,
}
}

fn create_ident(name: String) -> swc_ast::Ident {
swc_ast::Ident {
span: DUMMY_SP,
Expand Down Expand Up @@ -269,10 +263,10 @@ fn create_await_import_expr(
}))
}

fn create_assignment(key: String) -> swc_ast::ObjectPatProp {
fn create_assignment(key: swc_ast::Ident) -> swc_ast::ObjectPatProp {
swc_ast::ObjectPatProp::Assign(swc_ast::AssignPatProp {
span: DUMMY_SP,
key: create_ident(key),
key,
value: None,
})
}
Expand Down

0 comments on commit 313d55d

Please sign in to comment.