Skip to content

Commit

Permalink
Fix usage of fragments in TSX (#783)
Browse files Browse the repository at this point in the history
* test: add test for fragment in TSX

* fix(#782): fix compiler crash on tsx fragment

* chore: add changeset

---------

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
Princesseuh and natemoo-re authored Jul 7, 2023
1 parent 35ccd5e commit 325d3c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rotten-elephants-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

[TSX] fix compiler crash when file only contains an unamed fragment
5 changes: 4 additions & 1 deletion internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,15 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
if len(n.Attr) == 0 {
endLoc = n.Loc[0].Start + len(n.Data) - 1
}
if endLoc == -1 {
endLoc = 0
}
isSelfClosing := false
tmpLoc := endLoc
if len(p.sourcetext) > tmpLoc {
for i := 0; i < len(p.sourcetext[tmpLoc:]); i++ {
c := p.sourcetext[endLoc : endLoc+1][0]
if c == '/' && p.sourcetext[endLoc+1:][0] == '>' {
if c == '/' && len(p.sourcetext) > endLoc+1 && p.sourcetext[endLoc+1:][0] == '>' {
isSelfClosing = true
break
} else if c == '>' {
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler/test/tsx/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,14 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
assert.snapshot(code, output, `expected code to match snapshot`);
});

test('fragment with no name', async () => {
const input = '<>+0123456789</>';
const output = `<Fragment>
<>+0123456789</>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});

test.run();

0 comments on commit 325d3c3

Please sign in to comment.