Skip to content

Commit

Permalink
fix(es/typescript): Handle shebang with jsx pragma (#8318)
Browse files Browse the repository at this point in the history
**Description:**

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

**Related issue:**

denoland/deno#21258
  • Loading branch information
dsherret committed Nov 21, 2023
1 parent 85350d3 commit c25601d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/swc_ecma_transforms_typescript/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @jsx h */ import html, { h } from "example";
serve((_req)=>html({
body: /*#__PURE__*/ h("div", null, "Hello World!")
}));
Original file line number Diff line number Diff line change
@@ -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!")
}));
64 changes: 63 additions & 1 deletion crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c25601d

Please sign in to comment.