Skip to content

Commit 75dd7cc

Browse files
authored
TSX: fix spread attribute printing/mapping (#580)
* fix(tsx): spread attribute mapping * chore: add changeset * fix(tsx): print spread with high fidelity Co-authored-by: Nate Moore <[email protected]>
1 parent aac8c89 commit 75dd7cc

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed
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 spread attribute mappings

internal/printer/print-to-tsx.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
240240
continue
241241
}
242242
offset := 1
243-
if a.Type != astro.ShorthandAttribute {
243+
if a.Type != astro.ShorthandAttribute && a.Type != astro.SpreadAttribute {
244244
p.addSourceMapping(loc.Loc{Start: a.KeyLoc.Start - offset})
245245
}
246246
p.print(" ")
247247
eqStart := a.KeyLoc.Start + strings.IndexRune(p.sourcetext[a.KeyLoc.Start:], '=')
248-
if a.Type != astro.ShorthandAttribute {
248+
if a.Type != astro.ShorthandAttribute && a.Type != astro.SpreadAttribute {
249249
p.addSourceMapping(a.KeyLoc)
250250
}
251251
if a.Namespace != "" {
@@ -282,13 +282,14 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
282282
p.print(`}`)
283283
endLoc = eqStart + len(a.Val) + 2
284284
case astro.SpreadAttribute:
285-
p.print(a.Key)
286-
p.addSourceMapping(loc.Loc{Start: eqStart})
287-
p.print(`=`)
288-
p.addSourceMapping(loc.Loc{Start: eqStart + 1})
289-
p.addSourceMapping(a.ValLoc)
290-
p.print(fmt.Sprintf(`{...%s}`, a.Val))
291-
endLoc = a.ValLoc.Start + len(a.Val) + 2
285+
p.addSourceMapping(loc.Loc{Start: a.KeyLoc.Start - 4})
286+
p.print("{")
287+
p.addSourceMapping(loc.Loc{Start: a.KeyLoc.Start - 3})
288+
p.print("...")
289+
p.printTextWithSourcemap(a.Key, a.KeyLoc)
290+
p.addSourceMapping(loc.Loc{Start: a.KeyLoc.Start + len(a.Key)})
291+
p.print("}")
292+
endLoc = a.KeyLoc.Start + len(a.Key) + 1
292293
case astro.ShorthandAttribute:
293294
withoutComments, _ := removeComments(a.Key)
294295
if len(withoutComments) == 0 {

packages/compiler/test/tsx/basic.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,14 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}`;
162162
assert.snapshot(code, output, `expected code to match snapshot`);
163163
});
164164

165+
test('spread object', async () => {
166+
const input = `<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" />`;
167+
const output = `<Fragment>
168+
<DocSearch {...{ lang, labels: { modal, placeholder } }} client:only="preact" ></DocSearch>
169+
</Fragment>
170+
export default function __AstroComponent_(_props: Record<string, any>): any {}`;
171+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
172+
assert.snapshot(code, output, `expected code to match snapshot`);
173+
});
174+
165175
test.run();

0 commit comments

Comments
 (0)