Skip to content

Commit 0f8d5b4

Browse files
MrHensindresorhus
authored andcommitted
catch-error-name: Use underscore naming pattern (#306)
1 parent 7fbadf1 commit 0f8d5b4

File tree

3 files changed

+71
-50
lines changed

3 files changed

+71
-50
lines changed

docs/rules/catch-error-name.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ try {
5959

6060
```js
6161
const handleError = error => {
62-
const error2 = new Error('🦄');
62+
const error_ = new Error('🦄');
6363

64-
obj.catch(error3 => {
65-
// `error3` is allowed because of shadowed variables
64+
obj.catch(error__ => {
65+
// `error__` is allowed because of shadowed variables
6666
});
6767
}
6868
```

rules/catch-error-name.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const astUtils = require('eslint-ast-utils');
3+
const avoidCapture = require('./utils/avoid-capture');
34
const getDocsUrl = require('./utils/get-docs-url');
45

56
// Matches `someObj.then([FunctionExpression | ArrowFunctionExpression])`
@@ -25,19 +26,11 @@ function isLintablePromiseCatch(node) {
2526
return arg0.type === 'FunctionExpression' || arg0.type === 'ArrowFunctionExpression';
2627
}
2728

28-
// TODO: Use `./utils/avoid-capture.js` instead
29-
function indexifyName(name, scope) {
30-
const variables = scope.variableScope.set;
31-
32-
let index = 1;
33-
while (variables.has(index === 1 ? name : name + index)) {
34-
index++;
35-
}
36-
37-
return name + (index === 1 ? '' : index);
38-
}
39-
4029
const create = context => {
30+
const {
31+
ecmaVersion
32+
} = context.parserOptions;
33+
4134
const options = {
4235
name: 'error',
4336
caughtErrorsIgnorePattern: '^_$',
@@ -96,7 +89,8 @@ const create = context => {
9689
return;
9790
}
9891

99-
const errorName = indexifyName(name, context.getScope());
92+
const scope = context.getScope();
93+
const errorName = avoidCapture(name, [scope.variableScope], ecmaVersion);
10094
push(params.length === 0 || params[0].name === errorName || errorName);
10195
}
10296
},
@@ -118,7 +112,8 @@ const create = context => {
118112
return;
119113
}
120114

121-
const errName = indexifyName(name, context.getScope());
115+
const scope = context.getScope();
116+
const errName = avoidCapture(name, [scope.variableScope], ecmaVersion);
122117
push(node.param.name === errName || errName);
123118
},
124119
'CatchClause:exit': node => {

test/catch-error-name.js

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,62 @@ ruleTester.run('catch-error-name', rule, {
2828
const handleError = error => {
2929
try {
3030
doSomething();
31-
} catch (error2) {
32-
console.log(error2);
31+
} catch (error_) {
32+
console.log(error_);
3333
}
3434
}
3535
`),
3636
testCase(`
3737
const handleError = err => {
3838
try {
3939
doSomething();
40-
} catch (err2) {
41-
console.log(err2);
40+
} catch (err_) {
41+
console.log(err_);
4242
}
4343
}
4444
`, 'err'),
4545
testCase(`
4646
const handleError = error => {
47-
const error2 = new Error('🦄');
47+
const error_ = new Error('🦄');
4848
4949
try {
5050
doSomething();
51-
} catch (error3) {
52-
console.log(error3);
51+
} catch (error__) {
52+
console.log(error__);
5353
}
5454
}
5555
`),
5656
testCase('obj.catch(error => {})'),
5757
testCase(`
5858
const handleError = error => {
59-
obj.catch(error2 => { });
59+
obj.catch(error_ => { });
6060
}
6161
`),
6262
testCase(`
6363
const handleError = err => {
64-
obj.catch(err2 => { });
64+
obj.catch(err_ => { });
6565
}
6666
`, 'err'),
6767
testCase(`
6868
const handleError = error => {
69-
const error2 = new Error('foo bar');
69+
const error_ = new Error('foo bar');
7070
71-
obj.catch(error3 => { });
71+
obj.catch(error__ => { });
7272
}
7373
`),
7474
testCase(`
7575
const handleError = error => {
76-
const error2 = new Error('foo bar');
77-
const error3 = new Error('foo bar');
78-
const error4 = new Error('foo bar');
79-
const error5 = new Error('foo bar');
80-
const error6 = new Error('foo bar');
81-
const error7 = new Error('foo bar');
82-
const error8 = new Error('foo bar');
83-
const error9 = new Error('foo bar');
84-
const error10 = new Error('foo bar');
76+
const error_ = new Error('foo bar');
77+
const error__ = new Error('foo bar');
78+
const error___ = new Error('foo bar');
79+
const error____ = new Error('foo bar');
80+
const error_____ = new Error('foo bar');
81+
const error______ = new Error('foo bar');
82+
const error_______ = new Error('foo bar');
83+
const error________ = new Error('foo bar');
84+
const error_________ = new Error('foo bar');
8585
86-
obj.catch(error11 => { });
86+
obj.catch(error__________ => { });
8787
}
8888
`),
8989
testCase('obj.catch(() => {})'),
@@ -166,6 +166,23 @@ ruleTester.run('catch-error-name', rule, {
166166
}
167167
`,
168168
output: `
169+
const handleError = error => {
170+
try {
171+
doSomething();
172+
} catch (error_) {
173+
console.log(error_);
174+
}
175+
}
176+
`,
177+
errors: [
178+
{
179+
ruleId: 'catch-error-name',
180+
message: 'The catch parameter should be named `error_`.'
181+
}
182+
]
183+
},
184+
{
185+
code: `
169186
const handleError = error => {
170187
try {
171188
doSomething();
@@ -174,10 +191,19 @@ ruleTester.run('catch-error-name', rule, {
174191
}
175192
}
176193
`,
194+
output: `
195+
const handleError = error => {
196+
try {
197+
doSomething();
198+
} catch (error_) {
199+
console.log(error_);
200+
}
201+
}
202+
`,
177203
errors: [
178204
{
179205
ruleId: 'catch-error-name',
180-
message: 'The catch parameter should be named `error2`.'
206+
message: 'The catch parameter should be named `error_`.'
181207
}
182208
]
183209
},
@@ -199,59 +225,59 @@ ruleTester.run('catch-error-name', rule, {
199225
200226
try {
201227
doSomething();
202-
} catch (error2) {
203-
console.log(error2);
228+
} catch (error_) {
229+
console.log(error_);
204230
}
205231
}
206232
`,
207233
errors: [
208234
{
209235
ruleId: 'catch-error-name',
210-
message: 'The catch parameter should be named `error2`.'
236+
message: 'The catch parameter should be named `error_`.'
211237
}
212238
]
213239
},
214240
{
215241
code: `
216242
const handleError = error => {
217-
const error2 = new Error('foo bar');
243+
const error_ = new Error('foo bar');
218244
219245
obj.catch(foo => { });
220246
}
221247
`,
222248
output: `
223249
const handleError = error => {
224-
const error2 = new Error('foo bar');
250+
const error_ = new Error('foo bar');
225251
226-
obj.catch(error3 => { });
252+
obj.catch(error__ => { });
227253
}
228254
`,
229255
errors: [
230256
{
231257
ruleId: 'catch-error-name',
232-
message: 'The catch parameter should be named `error3`.'
258+
message: 'The catch parameter should be named `error__`.'
233259
}
234260
]
235261
},
236262
{
237263
code: `
238264
const handleError = error => {
239-
const error2 = new Error('foo bar');
265+
const error_ = new Error('foo bar');
240266
241267
obj.catch(foo => { });
242268
}
243269
`,
244270
output: `
245271
const handleError = error => {
246-
const error2 = new Error('foo bar');
272+
const error_ = new Error('foo bar');
247273
248-
obj.catch(error3 => { });
274+
obj.catch(error__ => { });
249275
}
250276
`,
251277
errors: [
252278
{
253279
ruleId: 'catch-error-name',
254-
message: 'The catch parameter should be named `error3`.'
280+
message: 'The catch parameter should be named `error__`.'
255281
}
256282
],
257283
options: [

0 commit comments

Comments
 (0)