Skip to content

Commit 54738be

Browse files
evilebottnawisindresorhus
authored andcommitted
Add caughtErrorsIgnorePattern option to catch-error-name rule (#137)
1 parent b1beb61 commit 54738be

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

docs/rules/catch-error-name.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,39 @@ somePromise.catch(_ => {
7575

7676
## Options
7777

78+
### name
79+
7880
You can set the `name` option like this:
7981

8082
```js
8183
"unicorn/catch-error-name": ["error", {"name": "err"}]
8284
```
85+
86+
### caughtErrorsIgnorePattern
87+
88+
```js
89+
"unicorn/catch-error-name": ["error", {"caughtErrorsIgnorePattern": "^_$"}]
90+
```
91+
92+
The `caughtErrorsIgnorePattern` option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern.
93+
For example, variables whose names is a string `_`. Default is `^_$`.
94+
95+
## Fail
96+
97+
```js
98+
try {
99+
doSomething();
100+
} catch (skipErr) {
101+
//
102+
}
103+
```
104+
105+
## Pass
106+
107+
```js
108+
try {
109+
doSomething();
110+
} catch (_) {
111+
//
112+
}
113+
```

rules/catch-error-name.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ function indexifyName(name, scope) {
3737
}
3838

3939
const create = context => {
40-
const opts = context.options[0];
41-
const name = (opts && opts.name) || 'err';
40+
const opts = Object.assign({}, {name: 'err'}, context.options[0]);
41+
const name = opts.name;
42+
const caughtErrorsIgnorePattern = new RegExp(opts.caughtErrorsIgnorePattern || '^_$');
4243
const stack = [];
4344

4445
function push(value) {
@@ -52,6 +53,10 @@ const create = context => {
5253
function popAndReport(node) {
5354
const value = stack.pop();
5455

56+
if (node && caughtErrorsIgnorePattern.test(node.name)) {
57+
return;
58+
}
59+
5560
if (value !== true) {
5661
context.report({
5762
node,
@@ -99,6 +104,9 @@ const schema = [{
99104
properties: {
100105
name: {
101106
type: 'string'
107+
},
108+
caughtErrorsIgnorePattern: {
109+
type: 'string'
102110
}
103111
}
104112
}];

test/catch-error-name.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,45 @@ ruleTester.run('catch-error-name', rule, {
9595
testCase('obj.catch(function (error) {})', 'error'),
9696
testCase('obj.catch(function (outerError) { return obj2.catch(function (innerError) {}) })'),
9797
testCase('obj.catch()'),
98+
testCase('obj.catch(_ => { console.log(_); })'),
99+
testCase('obj.catch(function (_) { console.log(_); })'),
98100
testCase('foo(function (err) {})'),
99101
testCase('foo().then(function (err) {})'),
100-
testCase('foo().catch(function (err) {})')
102+
testCase('foo().catch(function (err) {})'),
103+
testCase('try {} catch (_) {}'),
104+
testCase('try {} catch (_) { try {} catch (_) {} }'),
105+
testCase('try {} catch (_) { console.log(_); }'),
106+
testCase(`
107+
const handleError = error => {
108+
try {
109+
doSomething();
110+
} catch (_) {
111+
console.log(_);
112+
}
113+
}
114+
`),
115+
testCase('obj.catch(_ => {})'),
116+
{
117+
code: 'try {} catch (skipErr) {}',
118+
options: [
119+
{
120+
caughtErrorsIgnorePattern: '^skip'
121+
}
122+
]
123+
}
101124
],
102125
invalid: [
103126
testCase('try {} catch (error) {}', null, true),
104127
testCase('try {} catch (err) {}', 'error', true),
105128
testCase('try {} catch ({message}) {}', null, true),
106-
testCase('try {} catch (_) { console.log(_); }', null, true),
107129
testCase('try {} catch (outerError) {}', null, true),
108130
testCase('try {} catch (innerError) {}', null, true),
109131
testCase('obj.catch(error => {})', null, true),
110132
testCase('obj.catch(err => {})', 'error', true),
111133
testCase('obj.catch(({message}) => {})', null, true),
112-
testCase('obj.catch(_ => { console.log(_); })', null, true),
113134
testCase('obj.catch(function (error) {})', null, true),
114135
testCase('obj.catch(function ({message}) {})', null, true),
115136
testCase('obj.catch(function (err) {})', 'error', true),
116-
testCase('obj.catch(function (_) { console.log(_); })', null, true),
117137
// Failing tests for #107
118138
// testCase(`
119139
// foo.then(() => {
@@ -205,6 +225,20 @@ ruleTester.run('catch-error-name', rule, {
205225
{ruleId: 'catch-error-name'},
206226
{ruleId: 'catch-error-name'}
207227
]
228+
},
229+
{
230+
code: 'try {} catch (_err) {}',
231+
errors: [
232+
{
233+
ruleId: 'catch-error-name',
234+
message: 'The catch parameter should be named `err`.'
235+
}
236+
],
237+
options: [
238+
{
239+
caughtErrorsIgnorePattern: '^skip'
240+
}
241+
]
208242
}
209243
]
210244
});

0 commit comments

Comments
 (0)