Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain whitespace before tag close #822

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-moons-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

[TSX] maintain trailing whitespace before an element is closed, fixing TypeScript completion in some cases
16 changes: 9 additions & 7 deletions internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,13 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
p.print(`:`)
p.addSourceMapping(loc.Loc{Start: eqStart + 1})
p.print(`"` + encodeDoubleQuote(a.Val) + `"`)
endLoc = eqStart + 1 + len(a.Val) + 2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out these would mess up the endLoc because they could be out of order

case astro.EmptyAttribute:
p.print(a.Key)
p.print(`"`)
p.addNilSourceMapping()
p.print(`:`)
p.addSourceMapping(a.KeyLoc)
p.print(`true`)
endLoc = a.KeyLoc.Start + len(a.Key)
case astro.ExpressionAttribute:
p.print(a.Key)
p.print(`"`)
Expand All @@ -394,7 +392,6 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
p.printTextWithSourcemap(a.Val, loc.Loc{Start: eqStart + 2})
p.addSourceMapping(loc.Loc{Start: eqStart + 2 + len(a.Val)})
p.print(`)`)
endLoc = eqStart + len(a.Val) + 2
case astro.SpreadAttribute:
// noop
case astro.ShorthandAttribute:
Expand All @@ -404,14 +401,12 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
}
p.addSourceMapping(a.KeyLoc)
p.print(a.Key)
endLoc = a.KeyLoc.Start + len(a.Key)
case astro.TemplateLiteralAttribute:
p.addSourceMapping(a.KeyLoc)
p.print(a.Key)
p.print(`":`)
p.addSourceMapping(a.ValLoc)
p.print(fmt.Sprintf("`%s`", a.Val))
endLoc = a.ValLoc.Start + len(a.Val) + 2
}
if i == len(invalidTSXAttributes)-1 {
p.addNilSourceMapping()
Expand All @@ -425,6 +420,7 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
endLoc = 0
}
isSelfClosing := false
hasLeadingSpace := false
tmpLoc := endLoc
if len(p.sourcetext) > tmpLoc {
for i := 0; i < len(p.sourcetext[tmpLoc:]); i++ {
Expand All @@ -436,6 +432,9 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
p.addSourceMapping(loc.Loc{Start: endLoc})
endLoc++
break
} else if unicode.IsSpace(rune(c)) {
hasLeadingSpace = true
endLoc++
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we find a space before the >, we flag it

} else {
endLoc++
}
Expand All @@ -444,13 +443,16 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
endLoc++
}

if hasLeadingSpace {
p.addSourceMapping(loc.Loc{Start: endLoc - 1})
p.print(" ")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if we have the flag, we print an extra " " character


if voidElements[n.Data] && n.FirstChild == nil {
p.print("/>")
return
}
if isSelfClosing && n.FirstChild == nil {
p.addSourceMapping(loc.Loc{Start: endLoc - 1})
p.print(" ")
p.addSourceMapping(loc.Loc{Start: endLoc})
p.print("/>")
return
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler/test/tsx/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,24 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
assert.snapshot(code, output, `expected code to match snapshot`);
});

test('preserves spaces in tag', async () => {
const input = '<Button ></Button>';
const output = `<Fragment>
<Button ></Button>
</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('preserves spaces after attributes in tag', async () => {
const input = '<Button a="b" ></Button>';
const output = `<Fragment>
<Button a="b" ></Button>
</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();
Loading