Skip to content

Commit 15e3ff8

Browse files
authored
Fix orphan slot panic (#370)
* fix(#365): slot/head panic * test: add failing test * test: improve tests
1 parent 049dadf commit 15e3ff8

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

.changeset/light-days-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
Fix panic when using a `<slot />` in `head`

internal/parser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ func inHeadIM(p *parser) bool {
775775
return true
776776
case a.Slot:
777777
p.addElement()
778-
p.setOriginalIM()
778+
if p.originalIM == nil {
779+
p.setOriginalIM()
780+
}
779781
if p.hasSelfClosingToken {
780782
p.addLoc()
781783
p.oe.pop()

internal/printer/printer_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ func TestPrinter(t *testing.T) {
113113
code: `<html><head>${$$renderSlot($$result,$$slots["default"])}` + RENDER_HEAD_RESULT + `</head><body class="a"></body></html>`,
114114
},
115115
},
116+
{
117+
name: "head slot III",
118+
source: `<html><head><slot name="baseHeadExtension"><meta property="test2" content="test2"/></slot></head>`,
119+
want: want{
120+
code: `<html><head>${$$renderSlot($$result,$$slots["baseHeadExtension"],$$render` + BACKTICK + `<meta property="test2" content="test2">` + BACKTICK + `)}` + RENDER_HEAD_RESULT + `</head></html>`,
121+
},
122+
},
123+
{
124+
name: "orphan slot",
125+
source: `<slot />`,
126+
want: want{
127+
code: `${$$renderSlot($$result,$$slots["default"])}`,
128+
},
129+
},
116130
{
117131
name: "basic (frontmatter)",
118132
source: `---

packages/compiler/deno/astro.wasm

4 Bytes
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import { transform } from '@astrojs/compiler';
4+
5+
const FIXTURE = `
6+
---
7+
import { Code, Markdown } from 'astro/components';
8+
9+
const {isRequired, description, example} = Astro.props;
10+
---
11+
12+
<slot />
13+
{isRequired && <p class="mt-16 badge badge-info">Required</p>}
14+
{description?.trim() && <Markdown content={description} />}
15+
{example && <Code code={example} lang='yaml' />}
16+
`;
17+
18+
let result;
19+
test.before(async () => {
20+
result = await transform(FIXTURE);
21+
});
22+
23+
test('orphan slot', () => {
24+
assert.ok(result.code, 'able to parse');
25+
console.log(result.code);
26+
});
27+
28+
test.run();

0 commit comments

Comments
 (0)