Skip to content

Commit d7f3288

Browse files
authored
TSX: improve self-closing tag handling (#581)
* fix(tsx): improve self-closing tag behavior and mappings * chore: add changeset Co-authored-by: Nate Moore <[email protected]>
1 parent 75dd7cc commit d7f3288

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

.changeset/loud-pigs-fold.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+
TSX: Improve self-closing tag behavior and mappings

internal/printer/print-to-tsx.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
388388
for i := 0; i < len(p.sourcetext[tmpLoc:]); i++ {
389389
c := p.sourcetext[endLoc : endLoc+1][0]
390390
if c == '/' && p.sourcetext[endLoc+1:][0] == '>' {
391-
p.addSourceMapping(loc.Loc{Start: endLoc})
392391
isSelfClosing = true
393392
break
394393
} else if c == '>' {
@@ -404,9 +403,12 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
404403
p.print("/>")
405404
return
406405
}
407-
if isSelfClosing && len(n.Attr) > 0 {
406+
if isSelfClosing && n.FirstChild == nil {
407+
p.addSourceMapping(loc.Loc{Start: endLoc - 1})
408408
p.print(" ")
409409
p.addSourceMapping(loc.Loc{Start: endLoc})
410+
p.print("/>")
411+
return
410412
}
411413
p.print(">")
412414

packages/compiler/test/tsx/basic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}`;
165165
test('spread object', async () => {
166166
const input = `<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" />`;
167167
const output = `<Fragment>
168-
<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" ></DocSearch>
168+
<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" />
169169
</Fragment>
170170
export default function __AstroComponent_(_props: Record<string, any>): any {}`;
171171
const { code } = await convertToTSX(input, { sourcemap: 'external' });

packages/compiler/test/tsx/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ import SvelteOptionalProps from './SvelteOptionalProps.svelte';
248248
import SvelteOptionalProps from './SvelteOptionalProps.svelte';
249249
250250
<Fragment>
251-
<SvelteOptionalProps></SvelteOptionalProps>
251+
<SvelteOptionalProps />
252252
253253
</Fragment>
254254
export default function __AstroComponent_(_props: Record<string, any>): any {}`;

0 commit comments

Comments
 (0)