Skip to content

Commit 203a355

Browse files
committedMar 16, 2024··
fix: allow empty statement bodies
1 parent 0b1656f commit 203a355

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed
 

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import typescript from "prettier/parser-typescript";
44

55
import { preprocess } from "./preprocess.js";
66

7-
export const parsers: Record<string, Parser> = {
7+
export const parsers = {
88
babel: {
99
...babel.parsers.babel,
1010
preprocess,
@@ -13,4 +13,4 @@ export const parsers: Record<string, Parser> = {
1313
...typescript.parsers.typescript,
1414
preprocess,
1515
},
16-
};
16+
} satisfies Record<string, Parser>;

‎src/modifyNodeIfMissingBrackets.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ function wrapInBlock(node: Statement): BlockStatement {
1010
};
1111
}
1212

13+
const allowedBodyNodeTypes = new Set(["BlockStatement", "EmptyStatement"]);
14+
const allowedIfAlternateNodeTypes = new Set(["BlockStatement", "IfStatement"]);
15+
1316
export function modifyNodeIfMissingBrackets(node: CollectibleNode) {
1417
switch (node.type) {
1518
case "DoWhileStatement":
1619
case "ForStatement":
1720
case "ForInStatement":
1821
case "ForOfStatement":
1922
case "WhileStatement":
20-
if (node.body.type !== "BlockStatement") {
23+
if (!allowedBodyNodeTypes.has(node.body.type)) {
2124
node.body = wrapInBlock(node.body);
2225
return true;
2326
}
@@ -27,14 +30,14 @@ export function modifyNodeIfMissingBrackets(node: CollectibleNode) {
2730
case "IfStatement": {
2831
let modified: true | undefined;
2932

30-
if (node.consequent.type !== "BlockStatement") {
33+
if (!allowedBodyNodeTypes.has(node.consequent.type)) {
3134
node.consequent = wrapInBlock(node.consequent);
3235
modified = true;
3336
}
3437

3538
if (
3639
node.alternate &&
37-
!["BlockStatement", "IfStatement"].includes(node.alternate.type)
40+
!allowedIfAlternateNodeTypes.has(node.alternate.type)
3841
) {
3942
node.alternate = wrapInBlock(node.alternate);
4043
modified = true;

‎src/preprocess.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ do{//a
5050
b}while(c);
5151
`,
5252
],
53+
[`for (; ; ) ;`, `for (; ; ) ;`],
5354
[`for (; ; ) d;`, `for(;;){d}`],
5455
[`for (; ; ) { d; }`, `for (; ; ) { d; }`],
5556
[`for (a; b; c) d;`, `for(a;b;c){d}`],
@@ -95,6 +96,7 @@ c}`,
9596
[`await 1;`, `await 1;`],
9697
[`super;`, `super;`],
9798
[`export { foo };`, `export { foo };`],
99+
[`while (a) ;`, `while (a) ;`],
98100
[`while (a) b;`, `while(a){b}`],
99101
[`while (a) { b; }`, `while (a) { b; }`],
100102
[`while (a) <b>c;`, `while(a){(<b>c)}`],

0 commit comments

Comments
 (0)
Please sign in to comment.