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(next-swc/styled-jsx): Fix nth #32358

Merged
merged 6 commits into from Dec 10, 2021
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
38 changes: 37 additions & 1 deletion packages/next-swc/crates/core/src/styled_jsx/transform_css.rs
Expand Up @@ -7,9 +7,11 @@ use swc_css::codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
CodeGenerator, CodegenConfig, Emit,
};
use swc_css::parser::parser::input::ParserInput;
use swc_css::parser::{parse_str, parse_tokens, parser::ParserConfig};
use swc_css::visit::{VisitMut, VisitMutWith};
use swc_ecmascript::ast::{Expr, Str, StrKind, Tpl, TplElement};
use swc_ecmascript::parser::StringInput;
use swc_ecmascript::utils::HANDLER;
use swc_stylis::prefixer::prefixer;
use tracing::{debug, trace};
Expand Down Expand Up @@ -230,12 +232,16 @@ impl Namespacer {
span: node.span,
tokens: vec![],
};
let mut arg_tokens;

for (i, selector) in node.subclass_selectors.iter().enumerate() {
let (name, args) = match selector {
SubclassSelector::PseudoClass(PseudoClassSelector { name, children, .. }) => {
match children {
Some(PseudoSelectorChildren::Nth(_)) => todo!("nth"),
Some(PseudoSelectorChildren::Nth(v)) => {
arg_tokens = nth_to_tokens(&v);
(name, &arg_tokens)
}
Some(PseudoSelectorChildren::Tokens(v)) => (name, v),
None => (name, &empty_tokens),
}
Expand Down Expand Up @@ -480,3 +486,33 @@ fn get_block_tokens(selector_tokens: &Tokens) -> Vec<TokenAndSpan> {
},
]
}

fn nth_to_tokens(nth: &Nth) -> Tokens {
let mut s = String::new();
{
let mut wr = BasicCssWriter::new(&mut s, BasicCssWriterConfig { indent: " " });
let mut gen = CodeGenerator::new(&mut wr, CodegenConfig { minify: true });

gen.emit(&nth).unwrap();
}

let mut lexer = swc_css::parser::lexer::Lexer::new(
StringInput::new(&s, nth.span.lo, nth.span.hi),
ParserConfig {
parse_values: false,
allow_wrong_line_comments: true,
..Default::default()
},
);

let mut tokens = vec![];

while let Ok(t) = lexer.next() {
tokens.push(t);
}

Tokens {
span: Span::new(nth.span.lo, nth.span.hi, Default::default()),
tokens,
}
}
@@ -0,0 +1,10 @@
export default () => (
<div>
<p>test</p>
<style jsx>{`
li:nth-child(2) {
color: lime;
}
`}</style>
</div>
)
@@ -0,0 +1,9 @@
import _JSXStyle from "styled-jsx/style";
export default (()=><div className={"jsx-938ca197692ef624"}>

<p className={"jsx-938ca197692ef624"}>test</p>

<_JSXStyle id={"938ca197692ef624"}>{"li.jsx-938ca197692ef624:nth-child(2){color:lime}"}</_JSXStyle>

</div>
);