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 template literal attributes on components #918

Merged
merged 4 commits into from
Dec 21, 2023
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/perfect-falcons-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fixes an issue where components with template literal attributes were printed with the name of the attribute as value.
2 changes: 1 addition & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (p *printer) printAttributesToObject(n *astro.Node) {
p.addSourceMapping(a.KeyLoc)
p.printf(`"%s"`, strings.TrimSpace(a.Key))
p.print(":")
p.print("`" + strings.TrimSpace(a.Key) + "`")
p.print("`" + strings.TrimSpace(a.Val) + "`")
}
}
p.print("}")
Expand Down
14 changes: 14 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,20 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$renderComponent($$result,'Fragment',Fragment,{},{"default": () => $$render` + BACKTICK + `${` + BACKTICK + `${content}` + BACKTICK + `}` + BACKTICK + `,})}`,
},
},
{
name: "template literal attribute on component",
source: `<Component class=` + BACKTICK + `red` + BACKTICK + ` />`,
want: want{
code: `${$$renderComponent($$result,'Component',Component,{"class":` + BACKTICK + `red` + BACKTICK + `})}`,
},
},
{
name: "template literal attribute with variable on component",
source: `<Component class=` + BACKTICK + `${color}` + BACKTICK + ` />`,
want: want{
code: `${$$renderComponent($$result,'Component',Component,{"class":` + BACKTICK + `${color}` + BACKTICK + `})}`,
},
},
{
name: "define:vars on style",
source: "<style>h1{color:green;}</style><style define:vars={{color:'green'}}>h1{color:var(--color)}</style><h1>testing</h1>",
Expand Down
Loading