fix(es/typescript): Handle shebang with jsx pragma (#8318)

**Description:**

This handles a shebang with a jsx prag. Previously `h` would be removed as an import in this case.

**Related issue:**

https://github.com/denoland/deno/issues/21258
This commit is contained in:
David Sherret 2023-11-21 04:19:40 -05:00 committed by GitHub
parent 85350d31ae
commit c25601dec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 2 deletions

View File

@ -183,7 +183,12 @@ where
}
if !self.config.verbatim_module_syntax {
let span = n.span;
let span = if n.shebang.is_some() {
n.span
.with_lo(n.body.first().map(|s| s.span_lo()).unwrap_or(n.span.lo))
} else {
n.span
};
let JsxDirectives {
pragma,

View File

@ -0,0 +1,4 @@
/** @jsx h */ import html, { h } from "example";
serve((_req)=>html({
body: /*#__PURE__*/ h("div", null, "Hello World!")
}));

View File

@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A
/** @jsx h */ import html, { h } from "example";
serve((_req)=>html({
body: /*#__PURE__*/ h("div", null, "Hello World!")
}));

View File

@ -13,7 +13,7 @@ use swc_ecma_transforms_compat::{
use swc_ecma_transforms_proposal::decorators;
use swc_ecma_transforms_testing::{test, test_exec, test_fixture, Tester};
use swc_ecma_transforms_typescript::{
typescript, ImportsNotUsedAsValues, TsImportExportAssignConfig,
tsx, typescript, ImportsNotUsedAsValues, TsImportExportAssignConfig, TsxConfig,
};
use swc_ecma_visit::Fold;
@ -45,6 +45,33 @@ fn tr_config(
)
}
fn tsxr(t: &Tester) -> impl Fold {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
chain!(
resolver(unresolved_mark, top_level_mark, false),
tsx(
t.cm.clone(),
typescript::Config {
no_empty_export: true,
import_not_used_as_values: ImportsNotUsedAsValues::Remove,
..Default::default()
},
TsxConfig::default(),
t.comments.clone(),
top_level_mark,
),
swc_ecma_transforms_react::jsx(
t.cm.clone(),
Some(t.comments.clone()),
swc_ecma_transforms_react::Options::default(),
top_level_mark,
unresolved_mark
),
)
}
fn properties(t: &Tester, loose: bool) -> impl Fold {
let static_blocks_mark = Mark::new();
let unresolved_mark = Mark::new();
@ -1764,6 +1791,41 @@ test!(
"
);
test!(
Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
}),
|t| tsxr(t),
imports_not_used_as_values_jsx_prag,
r#"/** @jsx h */
import html, { h } from "example";
serve((_req) =>
html({
body: <div>Hello World!</div>,
})
);
"#
);
test!(
Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
}),
|t| tsxr(t),
imports_not_used_as_values_shebang_jsx_prag,
r#"#!/usr/bin/env -S deno run -A
/** @jsx h */
import html, { h } from "example";
serve((_req) =>
html({
body: <div>Hello World!</div>,
})
);
"#
);
test!(
Syntax::Typescript(TsConfig {
decorators: true,