Skip to content

Commit

Permalink
fix: correctly handle blockstatement (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
so1ve authored and JoshuaKGoldberg committed Sep 5, 2023
1 parent fe0cd38 commit 622c259
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/preprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe("preprocess", () => {
[`for (const a of b) { c; }`, `for (const a of b) { c; }`],
[`if (a) b;`, `if (a) { b; }`],
[`if (a) { b; }`, `if (a) { b; }`],
[`if (a) b; else c;`, `if (a) { b; } else { c; }`],
[`if (a) b; else { c; }`, `if (a) { b; } else { c; }`],
[`if (a) b; else if (b) c;`, `if (a) { b; } else if (b) { c; }`],
["let a; let a;", "let a; let a;"],
["foo; import a from 'bar'", "foo; import a from 'bar'"],
["return;", "return;"],
Expand Down
15 changes: 13 additions & 2 deletions src/printNodeWithBrackets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ export function printNodeWithBrackets(code: string, node: CollectibleNode) {
return [
code.slice(node.start!, node.test.end!),
") { ",
code.slice(node.consequent.start!, node.end!),
code.slice(node.consequent.start!, node.consequent.end!),
" }",
].join("");
node.alternate && [
" else ",
node.alternate.type !== "IfStatement" && [
node.alternate.type !== "BlockStatement" && "{ ",
code.slice(node.alternate.start!, node.alternate.end!),
node.alternate.type !== "BlockStatement" && " }",
],
],
]
.flat(Infinity)
.filter(Boolean)
.join("");

case "WhileStatement":
return [
Expand Down

0 comments on commit 622c259

Please sign in to comment.