Unverified Commit 7e539126 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

Fix identifier handling of`styled-jsx` (#29986)

Co-authored-by: 's avatarkdy1 <kdy1@users.noreply.github.com>
parent 2a2c1c9e
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher; use std::hash::Hasher;
use swc_common::DUMMY_SP; use swc_common::DUMMY_SP;
use swc_ecmascript::{ast::*, utils::drop_span}; use swc_ecmascript::ast::*;
use super::{ExternalStyle, JSXStyle, LocalStyle}; use super::{ExternalStyle, JSXStyle, LocalStyle};
...@@ -39,7 +39,7 @@ pub fn compute_class_names( ...@@ -39,7 +39,7 @@ pub fn compute_class_names(
} }
JSXStyle::External(external) => { JSXStyle::External(external) => {
if !external.is_global { if !external.is_global {
external_styles.push(drop_span(external.expr.clone())); external_styles.push(external.expr.clone());
} }
} }
} }
......
...@@ -7,7 +7,7 @@ use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark, Span, ...@@ -7,7 +7,7 @@ use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark, Span,
use swc_ecma_transforms_testing::{test, test_fixture}; use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecmascript::{ use swc_ecmascript::{
parser::{EsConfig, Syntax}, parser::{EsConfig, Syntax},
transforms::react::jsx, transforms::{react::jsx, resolver},
visit::as_folder, visit::as_folder,
}; };
use testing::fixture; use testing::fixture;
...@@ -76,7 +76,12 @@ fn next_ssg_fixture(input: PathBuf) { ...@@ -76,7 +76,12 @@ fn next_ssg_fixture(input: PathBuf) {
#[fixture("tests/fixture/styled-jsx/**/input.js")] #[fixture("tests/fixture/styled-jsx/**/input.js")]
fn styled_jsx_fixture(input: PathBuf) { fn styled_jsx_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js"); let output = input.parent().unwrap().join("output.js");
test_fixture(syntax(), &|_tr| styled_jsx(), &input, &output); test_fixture(
syntax(),
&|_tr| chain!(resolver(), styled_jsx()),
&input,
&output,
);
} }
pub struct DropSpan; pub struct DropSpan;
...@@ -92,7 +97,18 @@ fn styled_jsx_span_should_not_affect_hash(input: PathBuf) { ...@@ -92,7 +97,18 @@ fn styled_jsx_span_should_not_affect_hash(input: PathBuf) {
let output = input.parent().unwrap().join("output.js"); let output = input.parent().unwrap().join("output.js");
test_fixture( test_fixture(
syntax(), syntax(),
&|_tr| chain!(as_folder(DropSpan), styled_jsx()), &|_tr| {
// `resolver` uses `Mark` which is stored in a thread-local storage (namely
// swc_common::GLOBALS), and this loop will make `Mark` to be different from the
// invokcation above.
//
// 1000 is used because in future I (kdy1) may optimize logic of resolver.
for _ in 0..1000 {
let _mark = Mark::fresh(Mark::root());
}
chain!(as_folder(DropSpan), resolver(), styled_jsx())
},
&input, &input,
&output, &output,
); );
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "styled-jsx/style";
function NavigationItem({ active , className }) { function NavigationItem({ active , className: className1 }) {
return <span className={"jsx-2342aae4628612c6" + " " + (cn({ return <span className={"jsx-2342aae4628612c6" + " " + (cn({
active active
}, className, "navigation-item") || "")}> }, className1, "navigation-item") || "")}>
<_JSXStyle id={"2342aae4628612c6"}>{".navigation-item.jsx-2342aae4628612c6 a::after {content:attr(data-text);\ncontent: attr(data-text) / ''}"}</_JSXStyle> <_JSXStyle id={"2342aae4628612c6"}>{".navigation-item.jsx-2342aae4628612c6 a::after {content:attr(data-text);\ncontent: attr(data-text) / ''}"}</_JSXStyle>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "styled-jsx/style";
export default (({ children })=><div className={`jsx-${styles.__hash}`}> export default (({ children })=><div className={`jsx-${styles.__hash}`}>
<p className={`jsx-${styles.__hash}`}>{children}</p> <p className={`jsx-${styles.__hash}`}>{children}</p>
<_JSXStyle id={styles.__hash}>{styles}</_JSXStyle> <_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>
</div> </div>
); );
const styles = new String("p.jsx-556239d258b6d66a {color:red}"); const styles = new String("p.jsx-556239d258b6d66a {color:red}");
...@@ -9,8 +12,11 @@ styles.__hash = "556239d258b6d66a"; ...@@ -9,8 +12,11 @@ styles.__hash = "556239d258b6d66a";
class Test extends React.Component { class Test extends React.Component {
render() { render() {
return <div className={`jsx-${styles.__hash}`}> return <div className={`jsx-${styles.__hash}`}>
<p className={`jsx-${styles.__hash}`}>{this.props.children}</p> <p className={`jsx-${styles.__hash}`}>{this.props.children}</p>
<_JSXStyle id={styles.__hash}>{styles}</_JSXStyle> <_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>
</div>; </div>;
} }
} }
\ No newline at end of file
...@@ -3,20 +3,34 @@ import styles from "./styles"; ...@@ -3,20 +3,34 @@ import styles from "./styles";
const styles2 = require("./styles2"); const styles2 = require("./styles2");
import { foo as styles3 } from "./styles"; import { foo as styles3 } from "./styles";
export default (()=><div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}> export default (()=><div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}>
<p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}` + " " + "foo"}>test</p> <p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}` + " " + "foo"}>test</p>
<p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}>woot</p> <p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}>woot</p>
<_JSXStyle id={styles2.__hash}>{styles2}</_JSXStyle> <_JSXStyle id={styles2.__hash}>{styles2}</_JSXStyle>
<_JSXStyle id={styles3.__hash}>{styles3}</_JSXStyle> <_JSXStyle id={styles3.__hash}>{styles3}</_JSXStyle>
<div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}>woot</div> <div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles.__hash} jsx-${styles3.__hash}`}>woot</div>
<_JSXStyle id={"bee92b62eadf8a14"}>{"p.jsx-bee92b62eadf8a14 {color:red}\ndiv.jsx-bee92b62eadf8a14 {color:green}"}</_JSXStyle> <_JSXStyle id={"bee92b62eadf8a14"}>{"p.jsx-bee92b62eadf8a14 {color:red}\ndiv.jsx-bee92b62eadf8a14 {color:green}"}</_JSXStyle>
<_JSXStyle id={styles.__hash}>{styles}</_JSXStyle> <_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>
</div> </div>
); );
export const Test = ()=><div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}> export const Test = ()=><div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}>
<p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}` + " " + "foo"}>test</p> <p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}` + " " + "foo"}>test</p>
<p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}>woot</p> <p className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}>woot</p>
<_JSXStyle id={styles3.__hash}>{styles3}</_JSXStyle> <_JSXStyle id={styles3.__hash}>{styles3}</_JSXStyle>
<div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}>woot</div> <div className={"jsx-bee92b62eadf8a14 " + `jsx-${styles3.__hash}`}>woot</div>
<_JSXStyle id={"bee92b62eadf8a14"}>{"p.jsx-bee92b62eadf8a14 {color:red}\ndiv.jsx-bee92b62eadf8a14 {color:green}"}</_JSXStyle> <_JSXStyle id={"bee92b62eadf8a14"}>{"p.jsx-bee92b62eadf8a14 {color:red}\ndiv.jsx-bee92b62eadf8a14 {color:green}"}</_JSXStyle>
</div> </div>
; ;
\ No newline at end of file
...@@ -38,7 +38,7 @@ a.__jsx-style-dynamic-selector {color:red}`}</_JSXStyle>, ...@@ -38,7 +38,7 @@ a.__jsx-style-dynamic-selector {color:red}`}</_JSXStyle>,
] ]
]) ])
}; };
const dynamic = (colors)=>{ const dynamic1 = (colors)=>{
const b = { const b = {
styles: <_JSXStyle id={"79d79305fa1611e"} dynamic={[ styles: <_JSXStyle id={"79d79305fa1611e"} dynamic={[
colors.green.light colors.green.light
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment