diff --git a/crates/swc_ecma_transforms_typescript/src/typescript.rs b/crates/swc_ecma_transforms_typescript/src/typescript.rs index dc6fc07cd416..4b0d15f71695 100644 --- a/crates/swc_ecma_transforms_typescript/src/typescript.rs +++ b/crates/swc_ecma_transforms_typescript/src/typescript.rs @@ -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, diff --git a/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_jsx_prag.js b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_jsx_prag.js new file mode 100644 index 000000000000..1374ba9de71b --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_jsx_prag.js @@ -0,0 +1,4 @@ +/** @jsx h */ import html, { h } from "example"; +serve((_req)=>html({ + body: /*#__PURE__*/ h("div", null, "Hello World!") +})); diff --git a/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_shebang_jsx_prag.js b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_shebang_jsx_prag.js new file mode 100644 index 000000000000..2c38af8d6e2e --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/__swc_snapshots__/tests/strip.rs/imports_not_used_as_values_shebang_jsx_prag.js @@ -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!") +})); diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index af74e111a2b8..8f548df1f597 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -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:
Hello World!
, + }) +); +"# +); + +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:
Hello World!
, + }) +); +"# +); + test!( Syntax::Typescript(TsConfig { decorators: true,