Skip to content

fix(styled-jsx): Fix handling of deeply nested & #478

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

Merged
merged 11 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/styled-jsx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-styled-jsx

## 8.0.6

### Patch Changes

- 8d4f2db: Fix handling of deeply nested `&`

## 8.0.5

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/styled-jsx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# @swc/plugin-styled-jsx

## 8.0.6

### Patch Changes

- 8d4f2db: Fix handling of deeply nested `&`

## 8.0.5

### Patch Changes
Expand Down
12 changes: 12 additions & 0 deletions packages/styled-jsx/__tests__/__snapshots__/wasm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,18 @@ const Test = ()=>/*#__PURE__*/ React.createElement("div", {
"
`;

exports[`Should load swc-confidential wasm plugin correctly > Should transform global-nested-scope-sel correctly 1`] = `
"import _JSXStyle from "styled-jsx/style";
const Test = ()=>/*#__PURE__*/ React.createElement("div", {
className: "jsx-b0a16d58836c444b"
}, /*#__PURE__*/ React.createElement("span", {
className: "jsx-b0a16d58836c444b"
}, "test"), /*#__PURE__*/ React.createElement(_JSXStyle, {
id: "b0a16d58836c444b"
}, ".parent.jsx-b0a16d58836c444b{}.parent.jsx-b0a16d58836c444b div{}.parent.jsx-b0a16d58836c444b div.child{background:orange}"));
"
`;

exports[`Should load swc-confidential wasm plugin correctly > Should transform global-redundant correctly 1`] = `
"import _JSXStyle from "styled-jsx/style";
export default function IndexPage() {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-jsx",
"version": "8.0.5",
"version": "8.0.6",
"description": "SWC plugin for styled-jsx",
"main": "swc_plugin_styled_jsx.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = { workspace = true }
name = "styled_jsx"
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.93.0"
version = "0.93.1"


[features]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,18 @@ where

match self.prev {
Some(Component::Combinator(..)) => self.next(),
// If the next component is a nesting selector (&), we should remove the
// whitespace because we are going to remove the (&)
Some(Component::Nesting) => self.next(),
_ => Some(Component::Combinator(Combinator::Descendant)),
}
}
// Handle nesting selectors (&) by removing whitespaces between & and the next
// component
Some(Component::Nesting) => {
self.prev = self.iter.next();
self.next()
}
Some(v) => Some(v),
_ => {
self.prev = self.iter.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Test = () => (
<div>
<span>test</span>
<style jsx>{`
.parent {
:global(div) {
:global(&.child) {
background: orange;
}
}
}
`}</style>
</div>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _JSXStyle from "styled-jsx/style";
const Test = ()=><div className={"jsx-b0a16d58836c444b"}>
<span className={"jsx-b0a16d58836c444b"}>test</span>
<_JSXStyle id={"b0a16d58836c444b"}>{".parent.jsx-b0a16d58836c444b div.child{background:orange}"}</_JSXStyle>
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _JSXStyle from "styled-jsx/style";
const Test = ()=><div className={"jsx-b0a16d58836c444b"}>
<span className={"jsx-b0a16d58836c444b"}>test</span>
<_JSXStyle id={"b0a16d58836c444b"}>{".parent.jsx-b0a16d58836c444b{}.parent.jsx-b0a16d58836c444b div{}.parent.jsx-b0a16d58836c444b div.child{background:orange}"}</_JSXStyle>
</div>;
Loading