Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/quiet-emotion-spans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/plugin-emotion": patch
---

Handle React Compiler output with synthetic spans.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@swc/core": "^1.15.40",
"@swc/core": "^1.15.43",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed so the wasm test actually exercises reactCompiler; 1.15.40 rejects the option before it reaches the plugin.

"@taplo/cli": "^0.7.0",
"@types/node": "^22.13.9",
"husky": "^9.0.11",
Expand Down
33 changes: 33 additions & 0 deletions packages/emotion/__tests__/wasm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,36 @@ test("Should keep plain keyframes label for namespace imports", async () => {
);
expect(output.code).not.toContain("label:pulse");
});

test("Should transform css prop output from React Compiler", async () => {
const output = await transform(
`
import { css } from "@emotion/react";

export function App() {
return <div css={css\`width:120px;\`} />;
}
`,
{
envName: "development",
jsc: {
parser: {
syntax: "ecmascript",
jsx: true,
},
transform: {
react: {
runtime: "automatic",
importSource: "@emotion/react",
},
reactCompiler: true,
},
experimental: {
plugins: [[pluginPath, {}]],
},
},
} satisfies Options,
);

expect(output.code).toContain('css("width:120px;", "")');
});
30 changes: 21 additions & 9 deletions packages/emotion/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ impl<'a, C: Comments> EmotionTransformer<'a, C> {
label
}

fn add_pure_comment(&self, pos: BytePos) {
// React Compiler can synthesize Emotion calls with dummy spans.
// SWC cannot attach comments at BytePos(0), so skip the hint there.
if pos != BytePos(0) {
self.comments.add_pure_comment(pos);
}
}

fn create_call_label_arg(&self, kind: ExprKind) -> ExprOrSpread {
self.create_runtime_label(kind, false).as_arg()
}
Expand All @@ -267,6 +275,10 @@ impl<'a, C: Comments> EmotionTransformer<'a, C> {
}

fn create_sourcemap(&mut self, pos: BytePos) -> Option<String> {
if pos == BytePos(0) {
return None;
}

if self.options.sourcemap.unwrap_or(false) {
let loc = self.cm.get_code_map().lookup_char_pos(pos);
let filename = self.filepath.to_str().map(BytesStr::from_str_slice);
Expand Down Expand Up @@ -527,7 +539,7 @@ impl<'a, C: Comments> EmotionTransformer<'a, C> {
args.push(cm.as_arg());
}

self.comments.add_pure_comment(expr_pos);
self.add_pure_comment(expr_pos);
let call_span = Span::new(expr_pos, expr_pos);

let callee = match &css_callee {
Expand Down Expand Up @@ -582,7 +594,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
if matches!(*kind, ExprKind::Css | ExprKind::Keyframes)
&& !self.in_jsx_element
{
self.comments.add_pure_comment(expr.span.lo());
self.add_pure_comment(expr.span.lo());
if self.options.auto_label.unwrap_or(false) {
expr.args.push(self.create_call_label_arg(*kind));
}
Expand All @@ -608,7 +620,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
if !c.args.is_empty() {
let mut args_props = Vec::with_capacity(2);
args_props.push(self.create_label_prop_node("target"));
self.comments.add_pure_comment(expr.span.lo());
self.add_pure_comment(expr.span.lo());
if self.options.auto_label.unwrap_or(false) {
args_props.push(PropOrSpread::Prop(Box::new(
Prop::KeyValue(KeyValueProp {
Expand Down Expand Up @@ -678,7 +690,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
args_props.push(self.create_label_prop_node("target"));
let mut args = vec![prop.sym.as_ref().as_arg()];
if !self.in_jsx_element {
self.comments.add_pure_comment(expr.span.lo());
self.add_pure_comment(expr.span.lo());
if self.options.auto_label.unwrap_or(false) {
args_props.push(PropOrSpread::Prop(Box::new(
Prop::KeyValue(KeyValueProp {
Expand Down Expand Up @@ -724,7 +736,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
.iter()
.find_map(|item| match_runtime_export(item, &m.prop))
{
self.comments.add_pure_comment(expr.span.lo());
self.add_pure_comment(expr.span.lo());
if self.options.auto_label.unwrap_or(false) {
expr.args.push(self.create_call_label_arg(kind));
}
Expand Down Expand Up @@ -787,7 +799,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
let mut callee = call.take();
let mut object_props = Vec::with_capacity(2);
object_props.push(self.create_label_prop_node("target"));
self.comments.add_pure_comment(callee.span.lo());
self.add_pure_comment(callee.span.lo());
if self.options.auto_label.unwrap_or(false) {
object_props.push(PropOrSpread::Prop(Box::new(
Prop::KeyValue(KeyValueProp {
Expand Down Expand Up @@ -864,7 +876,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
if matches!(*kind, ExprKind::Css | ExprKind::Keyframes) {
let mut args = self.create_args_from_tagged_tpl(&mut tagged_tpl.tpl);
if !self.in_jsx_element {
self.comments.add_pure_comment(i.span.lo());
self.add_pure_comment(i.span.lo());
if self.options.auto_label.unwrap_or(false) {
args.push(self.create_tagged_tpl_label_arg(*kind));
}
Expand Down Expand Up @@ -911,7 +923,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
args.push(cm.as_arg());
}

self.comments.add_pure_comment(member_expr.span.lo());
self.add_pure_comment(member_expr.span.lo());
return Expr::Call(CallExpr {
callee: CallExpr {
callee: i.take().as_callee(),
Expand All @@ -935,7 +947,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
if let Some(kind) = c.exported_names.iter().find_map(|item| {
match_runtime_export(item, &member_expr.prop)
}) {
self.comments.add_pure_comment(member_expr.span.lo());
self.add_pure_comment(member_expr.span.lo());
return Expr::Call(CallExpr {
callee: member_expr.take().as_callee(),
args: {
Expand Down
Loading
Loading