Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/typescript): Ignore jsx element names #6911

Merged
merged 6 commits into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 41 additions & 15 deletions crates/swc_ecma_transforms_typescript/src/strip.rs
Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -1697,6 +1708,8 @@ where
}
}
}

fn visit_ts_type_element(&mut self, _: &TsTypeElement) {}
}

fn is_decl_concrete(d: &Decl) -> bool {
Expand Down Expand Up @@ -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
Expand Down
@@ -0,0 +1,18 @@
import path, { dirname } from "node:path";

export default function IndexPage(props: { abc: string }) {
return (
<div>
abc: {props.abc}
<svg viewBox="0 -85 600 600"></svg>
</div>
);
}

export function getServerSideProps() {
return {
props: {
abc: dirname("/abc/def"),
},
};
}
@@ -0,0 +1,17 @@
import { dirname } from "node:path";
export default function IndexPage(props) {
return <div>

abc: {props.abc}

<svg viewBox="0 -85 600 600"></svg>

</div>;
}
export function getServerSideProps() {
return {
props: {
abc: dirname("/abc/def")
}
};
}
@@ -0,0 +1,24 @@
import path, { dirname } from "node:path";

export default function IndexPage(props: { abc: string }) {
return (
<div>
abc: {props.abc}
<svg viewBox="0 -85 600 600">
<path
fillRule="evenodd"
d="M513 256.5C513 398.161 398.161 513 256.5 513C114.839 513 0 398.161 0 256.5C0 114.839 114.839 0 256.5 0C398.161 0 513 114.839 513 256.5ZM211.146 305.243L369.885 145L412 185.878L253.26 346.122L211.146 387L101 275.811L143.115 234.932L211.146 305.243Z"
fill="#fff"
/>
</svg>
</div>
);
}

export function getServerSideProps() {
return {
props: {
abc: dirname("/abc/def"),
},
};
}
@@ -0,0 +1,21 @@
import { dirname } from "node:path";
export default function IndexPage(props) {
return <div>

abc: {props.abc}

<svg viewBox="0 -85 600 600">

<path fillRule="evenodd" d="M513 256.5C513 398.161 398.161 513 256.5 513C114.839 513 0 398.161 0 256.5C0 114.839 114.839 0 256.5 0C398.161 0 513 114.839 513 256.5ZM211.146 305.243L369.885 145L412 185.878L253.26 346.122L211.146 387L101 275.811L143.115 234.932L211.146 305.243Z" fill="#fff"/>

</svg>

</div>;
}
export function getServerSideProps() {
return {
props: {
abc: dirname("/abc/def")
}
};
}