Skip to content

Commit

Permalink
Fix template literal attributes on components (#918)
Browse files Browse the repository at this point in the history
* add test

* fix

* add test case

* chore: changeset
  • Loading branch information
MoustaphaDev authored Dec 21, 2023
1 parent 4f74c05 commit cad2606
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit cad2606

Please sign in to comment.