mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 14:16:12 +03:00
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:
parent
85350d31ae
commit
c25601dec2
@ -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,
|
||||
|
@ -0,0 +1,4 @@
|
||||
/** @jsx h */ import html, { h } from "example";
|
||||
serve((_req)=>html({
|
||||
body: /*#__PURE__*/ h("div", null, "Hello World!")
|
||||
}));
|
@ -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!")
|
||||
}));
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user