From ee3a1071a5dbe0f2b718dd4f2e9959076cfa337c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 7 Feb 2023 17:06:37 +0900 Subject: [PATCH] fix(es/typescript): Ignore jsx element names (#6911) **Related issue:** - https://github.com/vercel/next.js/issues/45561. --- .../src/strip.rs | 56 ++++++++++++++----- .../tests/fixture/next-45561/1/input.tsx | 18 ++++++ .../tests/fixture/next-45561/1/output.js | 17 ++++++ .../tests/fixture/next-45561/2/input.tsx | 24 ++++++++ .../tests/fixture/next-45561/2/output.js | 21 +++++++ 5 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/input.tsx create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/output.js create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/input.tsx create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/output.js diff --git a/crates/swc_ecma_transforms_typescript/src/strip.rs b/crates/swc_ecma_transforms_typescript/src/strip.rs index 52f7c6be4c29..e551dbec6fff 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip.rs @@ -1517,6 +1517,12 @@ where } } + fn visit_class(&mut self, c: &Class) { + c.decorators.visit_with(self); + c.super_class.visit_with(self); + c.body.visit_with(self); + } + fn visit_decl(&mut self, n: &Decl) { self.handle_decl(n); @@ -1571,6 +1577,13 @@ where self.non_top_level = old; } + fn visit_expr(&mut self, n: &Expr) { + let old = self.in_var_pat; + self.in_var_pat = false; + n.visit_children_with(self); + self.in_var_pat = old; + } + fn visit_ident(&mut self, n: &Ident) { let entry = self.scope.referenced_idents.entry(n.to_id()).or_default(); if !self.is_type_only_export { @@ -1613,6 +1626,19 @@ where } } + fn visit_jsx_element_name(&mut self, n: &JSXElementName) { + match n { + JSXElementName::Ident(i) => { + if i.sym.starts_with(|c: char| c.is_ascii_uppercase()) { + n.visit_children_with(self); + } + } + _ => { + n.visit_children_with(self); + } + } + } + fn visit_module_items(&mut self, n: &[ModuleItem]) { let old = self.non_top_level; self.non_top_level = false; @@ -1642,26 +1668,11 @@ where self.non_top_level = old; } - fn visit_expr(&mut self, n: &Expr) { - let old = self.in_var_pat; - self.in_var_pat = false; - n.visit_children_with(self); - self.in_var_pat = old; - } - fn visit_ts_entity_name(&mut self, _: &TsEntityName) {} // these may contain expr fn visit_ts_expr_with_type_args(&mut self, _: &TsExprWithTypeArgs) {} - fn visit_ts_type_element(&mut self, _: &TsTypeElement) {} - - fn visit_class(&mut self, c: &Class) { - c.decorators.visit_with(self); - c.super_class.visit_with(self); - c.body.visit_with(self); - } - fn visit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) { match &n.module_ref { TsModuleRef::TsEntityName(name) => { @@ -1697,6 +1708,8 @@ where } } } + + fn visit_ts_type_element(&mut self, _: &TsTypeElement) {} } fn is_decl_concrete(d: &Decl) -> bool { @@ -2067,6 +2080,19 @@ where } } + fn visit_mut_jsx_element_name(&mut self, n: &mut JSXElementName) { + match n { + JSXElementName::Ident(i) => { + if i.sym.starts_with(|c: char| c.is_ascii_uppercase()) { + n.visit_mut_children_with(self); + } + } + _ => { + n.visit_mut_children_with(self); + } + } + } + fn visit_mut_module(&mut self, module: &mut Module) { let was_module = module .body diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/input.tsx b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/input.tsx new file mode 100644 index 000000000000..1ead316a81c8 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/input.tsx @@ -0,0 +1,18 @@ +import path, { dirname } from "node:path"; + +export default function IndexPage(props: { abc: string }) { + return ( +
+ abc: {props.abc} + +
+ ); +} + +export function getServerSideProps() { + return { + props: { + abc: dirname("/abc/def"), + }, + }; +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/output.js new file mode 100644 index 000000000000..03cd1bd466ae --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/1/output.js @@ -0,0 +1,17 @@ +import { dirname } from "node:path"; +export default function IndexPage(props) { + return
+ + abc: {props.abc} + + + +
; +} +export function getServerSideProps() { + return { + props: { + abc: dirname("/abc/def") + } + }; +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/input.tsx b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/input.tsx new file mode 100644 index 000000000000..57c4fc33ac4f --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/input.tsx @@ -0,0 +1,24 @@ +import path, { dirname } from "node:path"; + +export default function IndexPage(props: { abc: string }) { + return ( +
+ abc: {props.abc} + + + +
+ ); +} + +export function getServerSideProps() { + return { + props: { + abc: dirname("/abc/def"), + }, + }; +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/output.js new file mode 100644 index 000000000000..89bbf6278029 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/next-45561/2/output.js @@ -0,0 +1,21 @@ +import { dirname } from "node:path"; +export default function IndexPage(props) { + return
+ + abc: {props.abc} + + + + + + + +
; +} +export function getServerSideProps() { + return { + props: { + abc: dirname("/abc/def") + } + }; +}