Skip to content

Commit

Permalink
fix attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jul 15, 2024
1 parent 470a909 commit 6682fa5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ const prettify = async (code: string) =>
.replace(/\s*\*\//g, "*/")
// replace the newlines after comments
.replace(/\*\/\s*/g, "*/\n")
// replace empty lines
.replace(/\n\n+/g, "\n")
// remove ts ignore comments
.replace(/\/\/\s*@ts-ignore/g, "")
// replace empty lines
.replace(/\n\n+/g, "\n")
.trim(),
{
parser: "typescript",
Expand Down
44 changes: 20 additions & 24 deletions packages/yak-swc/yak_swc/src/yak_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,27 @@ impl YakImportVisitor {
if !self.is_using_next_yak() {
return None;
}
// styled.button`color: red;`
// keyframes`from { color: red; }`
// css`color: red;`
// styled.button.attrs({})`color: red;`
return match &*n.tag {
Expr::Ident(Ident { sym, .. }) => self.yak_library_imports.get(&sym.to_string()).cloned(),
Expr::Member(MemberExpr { obj, .. }) => {
if let Expr::Ident(Ident { sym, .. }) = &**obj {
self.yak_library_imports.get(&sym.to_string()).cloned()
} else {
None
}
}
Expr::Call(CallExpr { callee, .. }) => match callee {
Callee::Expr(expr) => {
if let Expr::Ident(Ident { sym, .. }) = &**expr {
self.yak_library_imports.get(&sym.to_string()).cloned()
} else {
None
}
}

fn get_root_ident(expr: &Expr) -> Option<&Ident> {
match expr {
Expr::Ident(ident) => Some(ident),
Expr::Member(MemberExpr { obj, .. }) => get_root_ident(obj),
Expr::Call(CallExpr { callee, .. }) => match callee {
Callee::Expr(expr) => get_root_ident(expr),
_ => None,
},
_ => None,
},
_ => None,
};
}
}

if let Some(ident) = get_root_ident(&n.tag) {
self
.yak_library_imports
.get(&ident.sym.to_string())
.cloned()
} else {
None
}
}

/// Returns the utility function identifier
Expand Down
11 changes: 7 additions & 4 deletions packages/yak-swc/yak_swc/tests/fixture/attrs/output.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { styled } from "next-yak/internal";
export const Button = styled.button.attrs({
type: "button"
})`
import __styleYak from "./input.yak.module.css!=!./input?./input.yak.module.css";
export const Button = /*YAK Extracted CSS:
.Button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
Expand All @@ -13,4 +13,7 @@ export const Button = styled.button.attrs({
&:hover {
background-color: #0056b3;
}
`;
}
*/ /*#__PURE__*/ styled.button.attrs({
type: "button"
})(__styleYak.Button);

0 comments on commit 6682fa5

Please sign in to comment.