Skip to content

Commit

Permalink
Don't ignore whitespace in expressions (#930)
Browse files Browse the repository at this point in the history
* add tests

* do print expressions with comments only

* don't remove whitespace in expressions

* fix whitespace in tests

* test to ignore empty expressions with whitespace

* ignore empty expressions with whitespace

* remove test relating to an unaffected code path

* chore: changeset
  • Loading branch information
MoustaphaDev authored Jan 2, 2024
1 parent 9ff6342 commit 4de359b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-cheetahs-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Preserve whitespace in expressions
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 == "" {
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 @@ -262,9 +262,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
} 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();

0 comments on commit 4de359b

Please sign in to comment.