Skip to content

Commit 83c2158

Browse files
scr2emmorgante
andauthored
feat: add a rule to enforce braces for if/do/for/while statements. (#180)
Co-authored-by: Morgante Pell <[email protected]>
1 parent a592b9e commit 83c2158

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

.grit/patterns/js/curly.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Enforces braces for if/for/do/while statements.
3+
tags: [good-practice]
4+
---
5+
This matches the [eslint rule](https://eslint.org/docs/latest/rules/curly).
6+
7+
8+
```grit
9+
engine marzano(1.0)
10+
language js
11+
12+
or {
13+
`if($_) $body`,
14+
for_statement($body),
15+
while_statement($body),
16+
do_statement($body),
17+
} where {
18+
$body <: not statement_block(),
19+
$body => `{$body}`,
20+
}
21+
22+
```
23+
24+
Code examples:
25+
## if
26+
```js
27+
if (x > 0)
28+
doStuff();
29+
```
30+
will become
31+
```js
32+
if (x > 0) {
33+
doStuff();
34+
}
35+
```
36+
## for
37+
```js
38+
for (var i = 0; i < 10; i++)
39+
doStuff();
40+
```
41+
will become
42+
```js
43+
for (var i = 0; i < 10; i++) {
44+
doStuff();
45+
}
46+
```
47+
## while
48+
```js
49+
while (x > 0)
50+
doStuff();
51+
```
52+
will become
53+
```js
54+
while (x > 0) {
55+
doStuff();
56+
}
57+
```
58+
## do
59+
```js
60+
do
61+
doStuff();
62+
while (x > 0);
63+
```
64+
will become
65+
```js
66+
do {
67+
doStuff();
68+
} while (x > 0);
69+
```

0 commit comments

Comments
 (0)