Skip to content

Commit

Permalink
Merge 544d1bf into acdc6fa
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 9, 2017
2 parents acdc6fa + 544d1bf commit 42793da
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/rules/catch-error-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ try {
}
```

```js
try {
doSomething();
} catch (_) {
// `_` is allowed when the error is not used
console.log(foo);
}
```


## Options

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"xo"
],
"dependencies": {
"eslint-ast-utils": "^1.0.0",
"import-modules": "^1.1.0",
"lodash.camelcase": "^4.1.1",
"lodash.kebabcase": "^4.0.1",
Expand Down
6 changes: 6 additions & 0 deletions rules/catch-error-name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const astUtils = require('eslint-ast-utils');

// Matches someObj.then([FunctionExpression | ArrowFunctionExpression])
function isLintablePromiseCatch(node) {
Expand Down Expand Up @@ -58,6 +59,11 @@ const create = context => {
}
},
CatchClause: node => {
if (node.param.name === '_') {
push(!astUtils.someContainIdentifier('_', node.body.body));
return;
}

push(node.param.name === name);
},
'CatchClause:exit': node => {
Expand Down
3 changes: 3 additions & 0 deletions test/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function testCase(code, name, error) {
ruleTester.run('catch-error-name', rule, {
valid: [
testCase('try {} catch (err) {}'),
testCase('try {} catch (_) {}'),
testCase('try {} catch (_) { console.log(foo); }'),
testCase('try {} catch (error) {}', 'error'),
testCase('try {} catch (outerError) { try {} catch (innerError) {} }'),
testCase('obj.catch(err => {})'),
Expand All @@ -37,6 +39,7 @@ ruleTester.run('catch-error-name', rule, {
invalid: [
testCase('try {} catch (error) {}', null, true),
testCase('try {} catch (err) {}', 'error', true),
testCase('try {} catch (_) { console.log(_); }', null, true),
testCase('try {} catch (outerError) {}', null, true),
testCase('try {} catch (innerError) {}', null, true),
testCase('obj.catch(error => {})', null, true),
Expand Down

0 comments on commit 42793da

Please sign in to comment.