Skip to content

Commit

Permalink
Fix 1679 (#115)
Browse files Browse the repository at this point in the history
* fix: empty script hoist

* fix(#1679): empty style

* fix(1679): treat {1} as truthy attr

* chore: add changeset

* test: add test for multiple styles

* fix: missing comma

* chore: remove log

* Update internal/transform/utils.go

Co-authored-by: Jonathan Neal <[email protected]>

* chore: fix tests

Co-authored-by: Jonathan Neal <[email protected]>
  • Loading branch information
natemoo-re and Jonathan Neal authored Oct 27, 2021
1 parent 469ef30 commit 39298e4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-snakes-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix small bugs with script/style hoisting behavior
2 changes: 1 addition & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (p *printer) printComponentMetadata(doc *astro.Node, source []byte) {
src := astro.GetAttribute(node, "src")
if src != nil {
p.print(fmt.Sprintf("{ type: 'remote', src: '%s' }", escapeSingleQuote(src.Val)))
} else {
} else if node.FirstChild != nil {
p.print(fmt.Sprintf("{ type: 'inline', value: '%s' }", escapeSingleQuote(node.FirstChild.Data)))
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,22 @@ import * as $$module2 from '../components/ZComponent.jsx';`},
code: `<html><head></head><body><svg><style>path { fill: red; }</style></svg></body></html>`,
},
},
{
name: "Empty script",
source: `<script hoist></script>`,
want: want{
scripts: []string{`{props:{"hoist":true}}`},
code: `<html><head></head><body></body></html>`,
},
},
{
name: "Empty style",
source: `<style define:vars={{ color: "Gainsboro" }}></style>`,
want: want{
styles: []string{`{props:{"define:vars":({ color: "Gainsboro" }),"data-astro-id":"7HAAVZPE"}}`},
code: `<html class="astro-7HAAVZPE"><head></head><body></body></html>`,
},
},
}

for _, tt := range tests {
Expand Down
5 changes: 5 additions & 0 deletions internal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func TestBasic(t *testing.T) {
`<svg><style><div>:root { color: red; }</style></svg>`,
[]TokenType{StartTagToken, StartTagToken, TextToken, EndTagToken, EndTagToken},
},
{
"multiple scoped :global",
`<style>:global(test-2) {}</style><style>test-1{}</style>`,
[]TokenType{StartTagToken, TextToken, EndTagToken, StartTagToken, TextToken, EndTagToken},
},
}

runTokenTypeTest(t, Basic)
Expand Down
3 changes: 3 additions & 0 deletions internal/transform/scope-css.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ outer:
Key: "data-astro-id",
Val: opts.Scope,
})
if n.FirstChild == nil {
continue
}
p := css.NewParser(bytes.NewBufferString(n.FirstChild.Data), false)
out := ""

Expand Down
1 change: 0 additions & 1 deletion internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func ExtractStyles(doc *tycho.Node) {
case a.Style:
// Do not extract <style> inside of SVGs
if n.Parent != nil && n.Parent.DataAtom == atom.Svg {
fmt.Println(n.FirstChild.Data)
return
}
doc.Styles = append(doc.Styles, n)
Expand Down
9 changes: 9 additions & 0 deletions internal/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func TestTransformScoping(t *testing.T) {
`,
want: `<div class="astro-XXXXXX"></div>`,
},
{
name: "multiple scoped :global",
source: `
<style>:global(test-2) {}</style>
<style>:global(test-1) {}</style>
<div />
`,
want: `<div class="astro-XXXXXX"></div>`,
},
}
var b strings.Builder
for _, tt := range tests {
Expand Down

0 comments on commit 39298e4

Please sign in to comment.