Skip to content

Commit

Permalink
Make the catch-error-name rule fixable in most cases (#209)
Browse files Browse the repository at this point in the history
Fixes #189
  • Loading branch information
futpib authored and sindresorhus committed Jan 22, 2019
1 parent fcee1c6 commit 3c66c0e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/rules/catch-error-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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

Expand Down
26 changes: 23 additions & 3 deletions rules/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,29 @@ const create = context => {
const value = stack.pop();

if (value !== true && !caughtErrorsIgnorePattern.test(node.name)) {
context.report({
const expectedName = value || name;
const problem = {
node,
message: `The catch parameter should be named \`${value || name}\`.`
});
message: `The catch parameter should be named \`${expectedName}\`.`
};

if (node.type === 'Identifier') {
problem.fix = fixer => {
const fixings = [fixer.replaceText(node, expectedName)];

const scope = context.getScope();
const variable = scope.set.get(node.name);
if (variable) {
for (const reference of variable.references) {
fixings.push(fixer.replaceText(reference.identifier, expectedName));
}
}

return fixings;
};
}

context.report(problem);
}
}

Expand Down Expand Up @@ -123,6 +142,7 @@ module.exports = {
docs: {
url: getDocsUrl(__filename)
},
fixable: 'code',
schema
}
};
59 changes: 50 additions & 9 deletions test/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const ruleTester = avaRuleTester(test, {
}
});

function testCase(code, name, error) {
function testCase(code, name, error, output) {
return {
code,
output: output || code,
options: name ? [{name}] : [],
errors: error ? [{ruleId: 'catch-error-name'}] : []
};
Expand Down Expand Up @@ -130,18 +131,19 @@ ruleTester.run('catch-error-name', rule, {
// }
// `),
],

invalid: [
testCase('try {} catch (err) {}', null, true),
testCase('try {} catch (error) {}', 'err', true),
testCase('try {} catch (err) {}', null, true, 'try {} catch (error) {}'),
testCase('try {} catch (error) {}', 'err', true, 'try {} catch (err) {}'),
testCase('try {} catch ({message}) {}', null, true),
testCase('try {} catch (outerError) {}', null, true),
testCase('try {} catch (innerError) {}', null, true),
testCase('obj.catch(err => {})', null, true),
testCase('obj.catch(error => {})', 'err', true),
testCase('try {} catch (outerError) {}', null, true, 'try {} catch (error) {}'),
testCase('try {} catch (innerError) {}', null, true, 'try {} catch (error) {}'),
testCase('obj.catch(err => {})', null, true, 'obj.catch(error => {})'),
testCase('obj.catch(error => {})', 'err', true, 'obj.catch(err => {})'),
testCase('obj.catch(({message}) => {})', null, true),
testCase('obj.catch(function (err) {})', null, true),
testCase('obj.catch(function (err) {})', null, true, 'obj.catch(function (error) {})'),
testCase('obj.catch(function ({message}) {})', null, true),
testCase('obj.catch(function (error) {})', 'err', true),
testCase('obj.catch(function (error) {})', 'err', true, 'obj.catch(function (err) {})'),
// Failing tests for #107
// testCase(`
// foo.then(() => {
Expand All @@ -163,6 +165,15 @@ ruleTester.run('catch-error-name', rule, {
}
}
`,
output: `
const handleError = error => {
try {
doSomething();
} catch (error2) {
console.log(error2);
}
}
`,
errors: [
{
ruleId: 'catch-error-name',
Expand All @@ -182,6 +193,17 @@ ruleTester.run('catch-error-name', rule, {
}
}
`,
output: `
const handleError = error => {
const error9 = new Error('foo bar');
try {
doSomething();
} catch (error2) {
console.log(error2);
}
}
`,
errors: [
{
ruleId: 'catch-error-name',
Expand All @@ -197,6 +219,13 @@ ruleTester.run('catch-error-name', rule, {
obj.catch(foo => { });
}
`,
output: `
const handleError = error => {
const error2 = new Error('foo bar');
obj.catch(error3 => { });
}
`,
errors: [
{
ruleId: 'catch-error-name',
Expand All @@ -212,6 +241,13 @@ ruleTester.run('catch-error-name', rule, {
obj.catch(foo => { });
}
`,
output: `
const handleError = error => {
const error2 = new Error('foo bar');
obj.catch(error3 => { });
}
`,
errors: [
{
ruleId: 'catch-error-name',
Expand All @@ -229,13 +265,18 @@ ruleTester.run('catch-error-name', rule, {
obj.catch(err => {});
obj.catch(err => {});
`,
output: `
obj.catch(error => {});
obj.catch(error => {});
`,
errors: [
{ruleId: 'catch-error-name'},
{ruleId: 'catch-error-name'}
]
},
{
code: 'try {} catch (_error) {}',
output: 'try {} catch (error) {}',
errors: [
{
ruleId: 'catch-error-name',
Expand Down

0 comments on commit 3c66c0e

Please sign in to comment.