Skip to content

Commit

Permalink
Merge ed30cbf into 49c4acf
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 6, 2020
2 parents 49c4acf + ed30cbf commit dd03a29
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 19 deletions.
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: '^_$|^error\\w+|^[A-Za-z]\\w*(e|E)rror$',
...context.options[0]
};

Expand Down
112 changes: 94 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,80 @@ ruleTester.run('catch-error-name', rule, {
} catch {
console.log('failed');
}
`)
`),
testCase('try {} catch (descriptiveError) {}'),
testCase('try {} catch (descriptive_error) {}'),
testCase('try {} catch (descriptiveerror) {}'),
testCase('try {} catch (errorFromNetwork) {}'),
testCase('try {} catch (error_from_network) {}'),
testCase('try {} catch (errorfromnetwork) {}')
],

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 +274,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 +369,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 +404,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 dd03a29

Please sign in to comment.