Skip to content

Commit

Permalink
fix: format expression (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyfaris authored Mar 23, 2022
1 parent 87b0508 commit 0ed661a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-months-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Fix: Format expression
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"format": "prettier -w ."
},
"dependencies": {
"@astrojs/compiler": "^0.12.1",
"@astrojs/compiler": "^0.13.1",
"make-synchronous": "0.1.1",
"prettier": "^2.4.1",
"sass-formatter": "^0.7.2"
Expand Down
24 changes: 10 additions & 14 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,20 +525,16 @@ function embed(
if (!node) return null;

if (node.type === 'expression') {
const textContent = getText(node, opts);

let content: Doc | string;

if (node.children.length === 1) {
content = textContent;
} else {
content = textToDoc(textContent, {
...opts,
parser: 'babel-ts',
semi: false,
});
content = stripTrailingHardline(content);
}
const textContent = printRaw(node);

let content: Doc;

content = textToDoc(forceIntoExpression(textContent), {
...opts,
parser: expressionParser,
semi: false,
});
content = stripTrailingHardline(content);

// if (node.children[0].value) {
// content = textToDoc(forceIntoExpression(textContent), { parser: expressionParser });
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ export function printRaw(
return '';
}

let raw = node.children.reduce((prev, curr) => prev + serialize(curr), '');
let raw = node.children.reduce(
(prev: string, curr: Node) => prev + serialize(curr),
''
);

if (!stripLeadingAndTrailingNewline) {
return raw;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/binary-expression/input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<h1>
{ 1 + 2}</h1>

3 changes: 3 additions & 0 deletions test/fixtures/binary-expression/output.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>
{1 + 2}
</h1>
2 changes: 2 additions & 0 deletions test/tests/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ test(
test('Format spread operator', 'spread-operator');

test('Can format nested comment', 'nested-comment');

test('format binary expressions', 'binary-expression');
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@astrojs/compiler@^0.12.1":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.12.1.tgz#163744036c2c2a278cced594a28a9631b69b7127"
integrity sha512-XpKZyJlnDhszulwwse/1uV+ZiyTpg6xNH5TrriYDVdzB8dUbQLdE7tX4Cxhdw5QrJpV57AaPrPXvJKi1jnIkxw==
"@astrojs/compiler@^0.13.1":
version "0.13.1"
resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.13.1.tgz#ec261e3be67765319b62f164f3e6022d52102203"
integrity sha512-iRAwvJ8/uVRtK/4ofSYKx4MTsuG8YjSgz8Lti7tPGoFaQzSNEU8J5cXvpTSV/F2SspSWB9EHjk2v7204pOxlrQ==
dependencies:
tsm "^2.2.1"
uvu "^0.5.3"
Expand Down

0 comments on commit 0ed661a

Please sign in to comment.