From 325d3c374afbf9c631edc5387c8358f72d2387c7 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 7 Jul 2023 15:39:10 +0200 Subject: [PATCH] Fix usage of fragments in TSX (#783) * test: add test for fragment in TSX * fix(#782): fix compiler crash on tsx fragment * chore: add changeset --------- Co-authored-by: Nate Moore --- .changeset/rotten-elephants-refuse.md | 5 +++++ internal/printer/print-to-tsx.go | 5 ++++- packages/compiler/test/tsx/basic.ts | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changeset/rotten-elephants-refuse.md diff --git a/.changeset/rotten-elephants-refuse.md b/.changeset/rotten-elephants-refuse.md new file mode 100644 index 000000000..825141fea --- /dev/null +++ b/.changeset/rotten-elephants-refuse.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +[TSX] fix compiler crash when file only contains an unamed fragment diff --git a/internal/printer/print-to-tsx.go b/internal/printer/print-to-tsx.go index 1d9d88c8d..9ed5ceb02 100644 --- a/internal/printer/print-to-tsx.go +++ b/internal/printer/print-to-tsx.go @@ -421,12 +421,15 @@ declare const Astro: Readonly>`, 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 == '>' { diff --git a/packages/compiler/test/tsx/basic.ts b/packages/compiler/test/tsx/basic.ts index 8ad19002e..57e3d6b62 100644 --- a/packages/compiler/test/tsx/basic.ts +++ b/packages/compiler/test/tsx/basic.ts @@ -204,4 +204,14 @@ export default function __AstroComponent_(_props: Record): any {}\n assert.snapshot(code, output, `expected code to match snapshot`); }); +test('fragment with no name', async () => { + const input = '<>+0123456789'; + const output = ` +<>+0123456789 + +export default function __AstroComponent_(_props: Record): any {}\n`; + const { code } = await convertToTSX(input, { sourcemap: 'external' }); + assert.snapshot(code, output, `expected code to match snapshot`); +}); + test.run();