Skip to content

Commit c58412e

Browse files
authored
chore: enforce curly braces for control statements (#8133)
* chore: eslint force curly braces * docs: update example component
1 parent eb8eeb5 commit c58412e

File tree

13 files changed

+88
-26
lines changed

13 files changed

+88
-26
lines changed

apps/site/pages/en/learn/asynchronous-work/asynchronous-flow-control.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ function getSong() {
9898
}
9999

100100
function singSong(_song) {
101-
if (!_song) throw new Error("song is '' empty, FEED ME A SONG!");
101+
if (!_song) {
102+
throw new Error("song is '' empty, FEED ME A SONG!");
103+
}
104+
102105
console.log(_song);
103106
}
104107

@@ -128,7 +131,10 @@ function getSong() {
128131
}
129132

130133
function singSong(_song) {
131-
if (!_song) throw new Error("song is '' empty, FEED ME A SONG!");
134+
if (!_song) {
135+
throw new Error("song is '' empty, FEED ME A SONG!");
136+
}
137+
132138
console.log(_song);
133139
}
134140

@@ -161,7 +167,10 @@ function executeFunctionWithArgs(operation, callback) {
161167
}
162168

163169
function serialProcedure(operation) {
164-
if (!operation) process.exit(0); // finished
170+
if (!operation) {
171+
process.exit(0); // finished
172+
}
173+
165174
executeFunctionWithArgs(operation, function (result) {
166175
// continue AFTER callback
167176
serialProcedure(operations.shift());
@@ -195,12 +204,20 @@ function dispatch(recipient, callback) {
195204

196205
function sendOneMillionEmailsOnly() {
197206
getListOfTenMillionGreatEmails(function (err, bigList) {
198-
if (err) throw err;
207+
if (err) {
208+
throw err;
209+
}
199210

200211
function serial(recipient) {
201-
if (!recipient || successCount >= 1000000) return final();
212+
if (!recipient || successCount >= 1000000) {
213+
return final();
214+
}
215+
202216
dispatch(recipient, function (_err) {
203-
if (!_err) successCount += 1;
217+
if (!_err) {
218+
successCount += 1;
219+
}
220+
204221
serial(bigList.pop());
205222
});
206223
}
@@ -241,9 +258,10 @@ function dispatch(recipient, callback) {
241258
function final(result) {
242259
console.log(`Result: ${result.count} attempts \
243260
& ${result.success} succeeded emails`);
244-
if (result.failed.length)
261+
if (result.failed.length) {
245262
console.log(`Failed to send to: \
246263
\n${result.failed.join('\n')}\n`);
264+
}
247265
}
248266

249267
recipients.forEach(function (recipient) {

apps/site/pages/en/learn/asynchronous-work/dont-block-the-event-loop.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ For a simple example, suppose you want to compute the average of the numbers `1`
318318
Example 1: Un-partitioned average, costs `O(n)`
319319

320320
```js
321-
for (let i = 0; i < n; i++) sum += i;
321+
for (let i = 0; i < n; i++) {
322+
sum += i;
323+
}
324+
322325
const avg = sum / n;
323326
console.log('avg: ' + avg);
324327
```

apps/site/pages/en/learn/asynchronous-work/event-loop-timers-and-nexttick.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ it doesn't have to be. Take this code snippet for example:
310310

311311
```js
312312
function apiCall(arg, callback) {
313-
if (typeof arg !== 'string')
313+
if (typeof arg !== 'string') {
314314
return process.nextTick(
315315
callback,
316316
new TypeError('argument should be string')
317317
);
318+
}
318319
}
319320
```
320321

apps/site/pages/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ And here is an equivalent **asynchronous** example:
5151
const fs = require('node:fs');
5252

5353
fs.readFile('/file.md', (err, data) => {
54-
if (err) throw err;
54+
if (err) {
55+
throw err;
56+
}
5557
});
5658
```
5759

@@ -78,7 +80,10 @@ And here is a similar, but not equivalent asynchronous example:
7880
const fs = require('node:fs');
7981

8082
fs.readFile('/file.md', (err, data) => {
81-
if (err) throw err;
83+
if (err) {
84+
throw err;
85+
}
86+
8287
console.log(data);
8388
});
8489
moreWork(); // will run before console.log
@@ -117,7 +122,10 @@ at an example:
117122
const fs = require('node:fs');
118123

119124
fs.readFile('/file.md', (err, data) => {
120-
if (err) throw err;
125+
if (err) {
126+
throw err;
127+
}
128+
121129
console.log(data);
122130
});
123131
fs.unlinkSync('/file.md');
@@ -132,10 +140,16 @@ execute in the correct order is:
132140
const fs = require('node:fs');
133141

134142
fs.readFile('/file.md', (readFileErr, data) => {
135-
if (readFileErr) throw readFileErr;
143+
if (readFileErr) {
144+
throw readFileErr;
145+
}
146+
136147
console.log(data);
148+
137149
fs.unlink('/file.md', unlinkErr => {
138-
if (unlinkErr) throw unlinkErr;
150+
if (unlinkErr) {
151+
throw unlinkErr;
152+
}
139153
});
140154
});
141155
```

apps/site/pages/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ async function example() {
7777
console.log(filehandle.fd);
7878
console.log(await filehandle.readFile({ encoding: 'utf8' }));
7979
} finally {
80-
if (filehandle) await filehandle.close();
80+
if (filehandle) {
81+
await filehandle.close();
82+
}
8183
}
8284
}
8385
example();
@@ -92,7 +94,9 @@ try {
9294
console.log(filehandle.fd);
9395
console.log(await filehandle.readFile({ encoding: 'utf8' }));
9496
} finally {
95-
if (filehandle) await filehandle.close();
97+
if (filehandle) {
98+
await filehandle.close();
99+
}
96100
}
97101
```
98102

apps/site/pages/en/learn/modules/backpressuring-in-streams.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,23 @@ However, when we want to use a [`Writable`][] directly, we must respect the
658658
// there is a great chance multiple callbacks will be called.
659659
class MyWritable extends Writable {
660660
_write(chunk, encoding, callback) {
661-
if (chunk.toString().indexOf('a') >= 0) callback();
662-
else if (chunk.toString().indexOf('b') >= 0) callback();
661+
if (chunk.toString().indexOf('a') >= 0) {
662+
callback();
663+
} else if (chunk.toString().indexOf('b') >= 0) {
664+
callback();
665+
}
663666
callback();
664667
}
665668
}
666669

667670
// The proper way to write this would be:
668-
if (chunk.contains('a')) return callback();
669-
if (chunk.contains('b')) return callback();
671+
if (chunk.contains('a')) {
672+
return callback();
673+
}
674+
675+
if (chunk.contains('b')) {
676+
return callback();
677+
}
670678
callback();
671679
```
672680

apps/site/pages/en/learn/test-runner/mocking.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ import { db } from 'db';
9191
export function read(key, all = false) {
9292
validate(key, val);
9393

94-
if (all) return db.getAll(key);
94+
if (all) {
95+
return db.getAll(key);
96+
}
9597

9698
return db.getOne(key);
9799
}

apps/site/pages/en/learn/typescript/introduction.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ import * as fs from 'fs';
7575

7676
fs.readFile('example.txt', 'foo', (err, data) => {
7777
// ^^^ Argument of type '"foo"' is not assignable to parameter of type …
78-
if (err) throw err;
78+
if (err) {
79+
throw err;
80+
}
7981
console.log(data);
8082
});
8183
```

docs/code-style.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ type MyComponentProps = {
129129
};
130130

131131
const MyComponent: FC<MyComponentProps> = ({ title, isVisible = true }) => {
132-
if (!isVisible) return null;
132+
if (!isVisible) {
133+
return null;
134+
}
133135

134136
return (
135137
<div className={styles.myComponent}>

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export default tseslint.config(
7979
],
8080

8181
'object-shorthand': 'error',
82+
curly: ['error', 'all'],
8283
},
8384
}
8485
);

0 commit comments

Comments
 (0)