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

Don't ignore whitespace in expressions #930

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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: 0 additions & 5 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,6 @@ func (p *parser) addText(text string) {
return
}

// Inside of expressions we can skip whitespace
if p.top().Expression && strings.TrimSpace(text) == "" {
return
}

t := p.top()
if n := t.LastChild; n != nil && n.Type == TextNode {
n.Data += text
Expand Down
6 changes: 5 additions & 1 deletion internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ func render1(p *printer, n *Node, opts RenderOptions) {

// Tip! Comment this block out to debug expressions
if n.Expression {
if n.FirstChild == nil {
clean := ""
if n.FirstChild != nil {
clean = strings.TrimSpace(n.FirstChild.Data)
}
if n.FirstChild == nil || clean == "" {
Comment on lines +319 to +323
Copy link
Member Author

Choose a reason for hiding this comment

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

We now need to check for whitespace too as the parser doesn't remove them in expressions anymore.

p.print("${(void 0)")
} else if expressionOnlyHasComment(n) {
// we do not print expressions that only contain comment blocks
Expand Down
3 changes: 0 additions & 3 deletions internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
p.addSourceMapping(n.Loc[0])
if n.FirstChild == nil {
p.print("{(void 0)")
} else if expressionOnlyHasComment(n) {
// we do not print expressions that only contain comment blocks
return
Comment on lines -233 to -235
Copy link
Member Author

@MoustaphaDev MoustaphaDev Dec 28, 2023

Choose a reason for hiding this comment

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

Don't ignore expressions with only comments in the TSX output

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, I wonder why Nate added that check initially?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure, I was wondering the same as that would unexpectedly remove TS directives like @ts-ignore in this case.
Pinging @natemoo-re, maybe he could provide some background

Copy link
Member

Choose a reason for hiding this comment

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

I honestly don't remember the context! Guessing it was just a copy + paste from the JS output which has special handling for this case.

} else {
p.print("{")
}
Expand Down
16 changes: 12 additions & 4 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ const groups = [[0, 1, 2], [3, 4, 5]];
items.map(item => {
return %s<li>${item}</li>%s;
})
}</ul>%s})}
}</ul>%s
})}
</div>`, "$$render"+BACKTICK, "$$render"+BACKTICK, BACKTICK, BACKTICK),
},
},
Expand Down Expand Up @@ -871,7 +872,7 @@ const groups = [[0, 1, 2], [3, 4, 5]];
name: "nested expressions IV",
source: `<div>{() => { if (value > 0.25) { return <span>Default</span> } else if (value > 0.5) { return <span>Another</span> } else if (value > 0.75) { return <span>Other</span> } return <span>Yet Other</span> }}</div>`,
want: want{
code: "${$$maybeRenderHead($$result)}<div>${() => { if (value > 0.25) { return $$render`<span>Default</span>`} else if (value > 0.5) { return $$render`<span>Another</span>`} else if (value > 0.75) { return $$render`<span>Other</span>`} return $$render`<span>Yet Other</span>`}}</div>",
code: "${$$maybeRenderHead($$result)}<div>${() => { if (value > 0.25) { return $$render`<span>Default</span>` } else if (value > 0.5) { return $$render`<span>Another</span>` } else if (value > 0.75) { return $$render`<span>Other</span>` } return $$render`<span>Yet Other</span>` }}</div>",
},
},
{
Expand Down Expand Up @@ -921,10 +922,10 @@ const items = ['red', 'yellow', 'blue'];
code: `${$$maybeRenderHead($$result)}<div>
${items.map((item) => (
// foo < > < }
$$render` + "`" + `<div${$$addAttribute(color, "id")}>color</div>` + "`" + `
$$render` + "`" + `<div${$$addAttribute(color, "id")}>color</div>` + "`" + `
))}
${items.map((item) => (
/* foo < > < } */$$render` + "`" + `<div${$$addAttribute(color, "id")}>color</div>` + "`" + `
/* foo < > < } */ $$render` + "`" + `<div${$$addAttribute(color, "id")}>color</div>` + "`" + `
))}
</div>`,
},
Expand Down Expand Up @@ -2311,6 +2312,13 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<body>(${(void 0)})</body>`,
},
},
{
name: "Empty expression with whitespace",
source: "<body>({ })</body>",
want: want{
code: `${$$maybeRenderHead($$result)}<body>(${(void 0) })</body>`,
},
},
{
name: "Empty attribute expression",
source: "<body attr={}></body>",
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/basic/expression-then-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('expression followed by node', () => {
result.code,
`yield '
';
}`,
}`,
'Expected output to properly handle expression!'
);
});
Expand Down
47 changes: 47 additions & 0 deletions packages/compiler/test/tsx/comment-whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

test('preverve whitespace around jsx comments', async () => {
const input = `{/* @ts-expect-error */}
<Component prop="value"></Component>

{
// @ts-expect-error
}
<Component prop="value"></Component>

{
/* @ts-expect-error */
<Component prop="value"></Component>
}

{
// @ts-expect-error
<Component prop="value"></Component>
}`;
const output = `<Fragment>
{/* @ts-expect-error */}
<Component prop="value"></Component>

{
// @ts-expect-error
}
<Component prop="value"></Component>

{
/* @ts-expect-error */
<Fragment><Component prop="value"></Component></Fragment>
}

{
// @ts-expect-error
<Fragment><Component prop="value"></Component></Fragment>
}
</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