Skip to content

Commit

Permalink
Merge 02a553b into 761a3be
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 9, 2020
2 parents 761a3be + 02a553b commit adf618b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
11 changes: 6 additions & 5 deletions docs/rules/catch-error-name.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Enforce a specific parameter name in catch clauses

Applies to both `try/catch` clauses and `promise.catch(...)` handlers.
Applies to both `try/catch` clauses and `promise.catch()` handlers.

The desired name is configurable, but defaults to `error`.

This rule is fixable unless the reported code was destructuring an error.


## Fail

```js
Expand All @@ -21,7 +20,6 @@ try {
somePromise.catch(e => {})
```


## Pass

```js
Expand Down Expand Up @@ -74,7 +72,6 @@ somePromise.catch(_ => {
});
```


## Options

### name
Expand All @@ -101,7 +98,7 @@ You can set the `name` option like this:
]
```

This option lets you specify a regex pattern for matches to ignore. The default is `^_$`.
This option lets you specify a regex pattern for matches to ignore. The default allows `_` and descriptive names like `networkError`.

With `^unicorn$`, this would fail:

Expand All @@ -122,3 +119,7 @@ try {
//
}
```

## Tip

In order to avoid shadowing in nested catch clauses, the auto-fix rule appends underscores to the identifier name. Since this might be hard to read, the default setting for `caughtErrorsIgnorePattern` allows the use of descriptive names instead, for example, `fsError` or `authError`.
2 changes: 1 addition & 1 deletion rules/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const create = context => {

const options = {
name: 'error',
caughtErrorsIgnorePattern: '^_$',
caughtErrorsIgnorePattern: /^_$|^[\dA-Za-z]+(e|E)rror$/.source,
...context.options[0]
};

Expand Down
108 changes: 90 additions & 18 deletions test/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ ruleTester.run('catch-error-name', rule, {
}
}
`),
testCase(outdent`
testCase(
outdent`
const handleError = err => {
try {
doSomething();
} catch (err_) {
console.log(err_);
}
}
`, 'err'),
`,
'err'
),
testCase(outdent`
const handleError = error => {
const error_ = new Error('🦄');
Expand All @@ -60,11 +63,14 @@ ruleTester.run('catch-error-name', rule, {
obj.catch(error_ => { });
}
`),
testCase(outdent`
testCase(
outdent`
const handleError = err => {
obj.catch(err_ => { });
}
`, 'err'),
`,
'err'
),
testCase(outdent`
const handleError = error => {
const error_ = new Error('foo bar');
Expand All @@ -91,11 +97,15 @@ ruleTester.run('catch-error-name', rule, {
testCase('obj.catch((_) => {})'),
testCase('obj.catch((_) => { console.log(foo); })'),
testCase('obj.catch(err => {})', 'err'),
testCase('obj.catch(outerError => { return obj2.catch(innerError => {}) })'),
testCase(
'obj.catch(outerError => { return obj2.catch(innerError => {}) })'
),
testCase('obj.catch(function (error) {})'),
testCase('obj.catch(function () {})'),
testCase('obj.catch(function (err) {})', 'err'),
testCase('obj.catch(function (outerError) { return obj2.catch(function (innerError) {}) })'),
testCase(
'obj.catch(function (outerError) { return obj2.catch(function (innerError) {}) })'
),
testCase('obj.catch()'),
testCase('obj.catch(_ => { console.log(_); })'),
testCase('obj.catch(function (_) { console.log(_); })'),
Expand Down Expand Up @@ -129,21 +139,76 @@ ruleTester.run('catch-error-name', rule, {
} catch {
console.log('failed');
}
`)
`),
testCase('try {} catch (descriptiveError) {}'),
testCase('try {} catch (descriptiveerror) {}')
],

invalid: [
testCase('try {} catch (err) { console.log(err) }', null, true, 'try {} catch (error) { console.log(error) }'),
testCase('try {} catch (error) { console.log(error) }', 'err', true, 'try {} catch (err) { console.log(err) }'),
testCase(
'try {} catch (err) { console.log(err) }',
null,
true,
'try {} catch (error) { console.log(error) }'
),
testCase(
'try {} catch (error) { console.log(error) }',
'err',
true,
'try {} catch (err) { console.log(err) }'
),
testCase('try {} catch ({message}) {}', null, true),
testCase('try {} catch (outerError) {}', null, true, 'try {} catch (error) {}'),
testCase('try {} catch (innerError) {}', null, true, 'try {} catch (error) {}'),
{
code: 'try {} catch (outerError) {}',
output: 'try {} catch (error) {}',
errors: [
{
ruleId: 'catch-error-message',
message: 'The catch parameter should be named `error`.'
}
],
options: [
{
caughtErrorsIgnorePattern: '^_$'
}
]
},
{
code: 'try {} catch (innerError) {}',
output: 'try {} catch (error) {}',
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error`.'
}
],
options: [
{
caughtErrorsIgnorePattern: '^_$'
}
]
},
testCase('obj.catch(err => err)', null, true, 'obj.catch(error => error)'),
testCase('obj.catch(error => error.stack)', 'err', true, 'obj.catch(err => err.stack)'),
testCase(
'obj.catch(error => error.stack)',
'err',
true,
'obj.catch(err => err.stack)'
),
testCase('obj.catch(({message}) => {})', null, true),
testCase('obj.catch(function (err) { console.log(err) })', null, true, 'obj.catch(function (error) { console.log(error) })'),
testCase(
'obj.catch(function (err) { console.log(err) })',
null,
true,
'obj.catch(function (error) { console.log(error) })'
),
testCase('obj.catch(function ({message}) {})', null, true),
testCase('obj.catch(function (error) { console.log(error) })', 'err', true, 'obj.catch(function (err) { console.log(err) })'),
testCase(
'obj.catch(function (error) { console.log(error) })',
'err',
true,
'obj.catch(function (err) { console.log(err) })'
),
// Failing tests for #107
// testCase(outdent`
// foo.then(() => {
Expand Down Expand Up @@ -205,6 +270,11 @@ ruleTester.run('catch-error-name', rule, {
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error_`.'
}
],
options: [
{
caughtErrorsIgnorePattern: '^_$'
}
]
},
{
Expand Down Expand Up @@ -295,10 +365,7 @@ ruleTester.run('catch-error-name', rule, {
obj.catch(error => {});
obj.catch(error => {});
`,
errors: [
{ruleId: 'catch-error-name'},
{ruleId: 'catch-error-name'}
]
errors: [{ruleId: 'catch-error-name'}, {ruleId: 'catch-error-name'}]
},
{
code: 'try {} catch (_error) {}',
Expand Down Expand Up @@ -333,6 +400,11 @@ ruleTester.run('catch-error-name', rule, {
ruleId: 'catch-error-message',
message: 'The catch parameter should be named `error`.'
}
],
options: [
{
caughtErrorsIgnorePattern: '^_$'
}
]
}
]
Expand Down

0 comments on commit adf618b

Please sign in to comment.