Skip to content

Commit

Permalink
fix(#935): set transform to literal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Jan 11, 2024
1 parent 3909ab4 commit 9c918ad
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-plums-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': minor
---

Fixes an edge case that caused `html` and `body` tags with attributes to be ignored when they were wrapped in a component.
6 changes: 3 additions & 3 deletions cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func Parse() any {
h := handler.NewHandler(source, parseOptions.Filename)

var doc *astro.Node
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h), astro.ParseOptionEnableLiteral(true))
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
h.AppendError(err)
}
Expand All @@ -250,7 +250,7 @@ func ConvertToTSX() any {
h := handler.NewHandler(source, transformOptions.Filename)

var doc *astro.Node
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h), astro.ParseOptionEnableLiteral(true))
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
h.AppendError(err)
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func Transform() any {
}
}()

doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h))
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
reject.Invoke(wasm_utils.ErrorToJSError(h, err))
return
Expand Down
15 changes: 14 additions & 1 deletion internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,14 @@ const name = 'named';
code: "${cond && $$render`<meta charset=\"utf8\">`}${cond && $$render`<title>My title</title>`}",
},
},
{
name: "top-level component does not drop body attributes",
source: `<Base><body class="foobar"><slot /></body></Base>`,
only: true,
want: want{
code: "${$$renderComponent($$result,'Base',Base,{},{\"default\": () => $$render`${$$maybeRenderHead($$result)}<body class=\"foobar\">${$$renderSlot($$result,$$slots[\"default\"])}</body>`,})}",
},
},
{
name: "custom elements",
source: `---
Expand Down Expand Up @@ -3227,8 +3235,8 @@ const items = ["Dog", "Cat", "Platipus"];
// transform output from source
code := test_utils.Dedent(tt.source)

doc, err := astro.Parse(strings.NewReader(code))
h := handler.NewHandler(code, "<stdin>")
doc, err := astro.ParseWithOptions(strings.NewReader(code), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))

if err != nil {
t.Error(err)
Expand Down Expand Up @@ -3481,6 +3489,11 @@ const c = '\''
source: `<main id=` + BACKTICK + `gotcha />`,
want: []ASTNode{{Type: "element", Name: "main", Attributes: []ASTNode{{Type: "attribute", Kind: "template-literal", Name: "id", Value: "gotcha", Raw: "`gotcha"}}}},
},
{
name: "top-level component does not drop body attributes",
source: `<Base><body class="foobar"><slot /></body></Base>`,
want: []ASTNode{{Type: "component", Name: "Base", Attributes: []ASTNode{}, Children: []ASTNode{{Type: "element", Name: "body", Attributes: []ASTNode{{Type: "attribute", Kind: "quoted", Name: "class", Value: "foobar", Raw: "\"foobar\""}}, Children: []ASTNode{{Type: "element", Name: "slot"}}}}}},
},
}

for _, tt := range tests {
Expand Down
3 changes: 1 addition & 2 deletions internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TrimTrailingSpace(doc *astro.Node) {
return
}

if doc.LastChild.Type == astro.TextNode {
if doc.LastChild.Type == astro.TextNode && len(doc.LastChild.Data) < len(strings.TrimRightFunc(doc.LastChild.Data, unicode.IsSpace)) {
doc.LastChild.Data = strings.TrimRightFunc(doc.LastChild.Data, unicode.IsSpace)
return
}
Expand All @@ -246,7 +246,6 @@ func TrimTrailingSpace(doc *astro.Node) {
n = n.LastChild
continue
} else {
n = nil
break
}
}
Expand Down
18 changes: 12 additions & 6 deletions internal/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ func TestFullTransform(t *testing.T) {
},
{
name: "Component before html I",
source: `<Navigation /><html><body><h1>Astro</h1></body></html>`,
want: `<Navigation></Navigation><h1>Astro</h1>`,
source: `<Navigation /><html lang="en"><body><h1>Astro</h1></body></html>`,
want: `<Navigation></Navigation><html lang="en"><body><h1>Astro</h1></body></html>`,
},
{
name: "Component before html II",
source: `<MainHead title={title} description={description} /><html lang="en"><body><slot /></body></html>`,
want: `<MainHead title={title} description={description}></MainHead><slot></slot>`,
want: `<MainHead title={title} description={description}></MainHead><html lang="en"><body><slot></slot></body></html>`,
},
{
name: "respects explicitly authored elements",
Expand All @@ -281,6 +281,11 @@ func TestFullTransform(t *testing.T) {
source: `<Component />`,
want: `<Component></Component>`,
},
{
name: "top-level component does not drop body attributes",
source: `<Base><body class="foobar"><slot /></body></Base>`,
want: `<Base><body class="foobar"><slot></slot></body></Base>`,
},
{
name: "works with nested components",
source: `<style></style><A><div><B /></div></A>`,
Expand All @@ -302,7 +307,7 @@ func TestFullTransform(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.Reset()
doc, err := astro.Parse(strings.NewReader(tt.source))
doc, err := astro.ParseWithOptions(strings.NewReader(tt.source), astro.ParseOptionEnableLiteral(true))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -513,11 +518,12 @@ func TestAnnotation(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.Reset()
doc, err := astro.Parse(strings.NewReader(tt.source))

h := handler.NewHandler(tt.source, "/src/pages/index.astro")
doc, err := astro.ParseWithOptions(strings.NewReader(tt.source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
t.Error(err)
}
h := handler.NewHandler(tt.source, "/src/pages/index.astro")
Transform(doc, TransformOptions{
AnnotateSourceFile: true,
Filename: "/src/pages/index.astro",
Expand Down
24 changes: 24 additions & 0 deletions packages/compiler/test/basic/body-in-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { transform } from '@astrojs/compiler';

const FIXTURE = `
---
let value = 'world';
---
<Base><body class="foobar"><slot /></body></Base>
`;

let result;
test.before(async () => {
result = await transform(FIXTURE);
});

test('top-level component does not drop body attributes', () => {
console.log(result.code);
assert.match(result.code, "${$$renderComponent($$result,'Base',Base,{},{\"default\": () => $$render`${$$maybeRenderHead($$result)}<body class=\"foobar\">${$$renderSlot($$result,$$slots[\"default\"])}</body>`,})}", `Expected body to be included!`);
});


test.run();
2 changes: 1 addition & 1 deletion packages/compiler/test/basic/trailing-spaces-ii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.before(async () => {
});

test('trailing space', () => {
assert.ok(result.code, 'Expected to compiler');
assert.ok(result.code, 'Expected to compile');
assert.match(
result.code,
`<span class="spoiler astro-bqati2k5">
Expand Down

0 comments on commit 9c918ad

Please sign in to comment.