Skip to content

Commit 3c66c0e

Browse files
futpibsindresorhus
authored andcommitted
Make the catch-error-name rule fixable in most cases (#209)
Fixes #189
1 parent fcee1c6 commit 3c66c0e

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

docs/rules/catch-error-name.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Applies to both `try/catch` clauses and `promise.catch(...)` handlers.
44

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

7+
This rule is fixable unless the reported code was destructuring an error.
8+
79

810
## Fail
911

rules/catch-error-name.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,29 @@ const create = context => {
5858
const value = stack.pop();
5959

6060
if (value !== true && !caughtErrorsIgnorePattern.test(node.name)) {
61-
context.report({
61+
const expectedName = value || name;
62+
const problem = {
6263
node,
63-
message: `The catch parameter should be named \`${value || name}\`.`
64-
});
64+
message: `The catch parameter should be named \`${expectedName}\`.`
65+
};
66+
67+
if (node.type === 'Identifier') {
68+
problem.fix = fixer => {
69+
const fixings = [fixer.replaceText(node, expectedName)];
70+
71+
const scope = context.getScope();
72+
const variable = scope.set.get(node.name);
73+
if (variable) {
74+
for (const reference of variable.references) {
75+
fixings.push(fixer.replaceText(reference.identifier, expectedName));
76+
}
77+
}
78+
79+
return fixings;
80+
};
81+
}
82+
83+
context.report(problem);
6584
}
6685
}
6786

@@ -123,6 +142,7 @@ module.exports = {
123142
docs: {
124143
url: getDocsUrl(__filename)
125144
},
145+
fixable: 'code',
126146
schema
127147
}
128148
};

test/catch-error-name.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const ruleTester = avaRuleTester(test, {
88
}
99
});
1010

11-
function testCase(code, name, error) {
11+
function testCase(code, name, error, output) {
1212
return {
1313
code,
14+
output: output || code,
1415
options: name ? [{name}] : [],
1516
errors: error ? [{ruleId: 'catch-error-name'}] : []
1617
};
@@ -130,18 +131,19 @@ ruleTester.run('catch-error-name', rule, {
130131
// }
131132
// `),
132133
],
134+
133135
invalid: [
134-
testCase('try {} catch (err) {}', null, true),
135-
testCase('try {} catch (error) {}', 'err', true),
136+
testCase('try {} catch (err) {}', null, true, 'try {} catch (error) {}'),
137+
testCase('try {} catch (error) {}', 'err', true, 'try {} catch (err) {}'),
136138
testCase('try {} catch ({message}) {}', null, true),
137-
testCase('try {} catch (outerError) {}', null, true),
138-
testCase('try {} catch (innerError) {}', null, true),
139-
testCase('obj.catch(err => {})', null, true),
140-
testCase('obj.catch(error => {})', 'err', true),
139+
testCase('try {} catch (outerError) {}', null, true, 'try {} catch (error) {}'),
140+
testCase('try {} catch (innerError) {}', null, true, 'try {} catch (error) {}'),
141+
testCase('obj.catch(err => {})', null, true, 'obj.catch(error => {})'),
142+
testCase('obj.catch(error => {})', 'err', true, 'obj.catch(err => {})'),
141143
testCase('obj.catch(({message}) => {})', null, true),
142-
testCase('obj.catch(function (err) {})', null, true),
144+
testCase('obj.catch(function (err) {})', null, true, 'obj.catch(function (error) {})'),
143145
testCase('obj.catch(function ({message}) {})', null, true),
144-
testCase('obj.catch(function (error) {})', 'err', true),
146+
testCase('obj.catch(function (error) {})', 'err', true, 'obj.catch(function (err) {})'),
145147
// Failing tests for #107
146148
// testCase(`
147149
// foo.then(() => {
@@ -163,6 +165,15 @@ ruleTester.run('catch-error-name', rule, {
163165
}
164166
}
165167
`,
168+
output: `
169+
const handleError = error => {
170+
try {
171+
doSomething();
172+
} catch (error2) {
173+
console.log(error2);
174+
}
175+
}
176+
`,
166177
errors: [
167178
{
168179
ruleId: 'catch-error-name',
@@ -182,6 +193,17 @@ ruleTester.run('catch-error-name', rule, {
182193
}
183194
}
184195
`,
196+
output: `
197+
const handleError = error => {
198+
const error9 = new Error('foo bar');
199+
200+
try {
201+
doSomething();
202+
} catch (error2) {
203+
console.log(error2);
204+
}
205+
}
206+
`,
185207
errors: [
186208
{
187209
ruleId: 'catch-error-name',
@@ -197,6 +219,13 @@ ruleTester.run('catch-error-name', rule, {
197219
obj.catch(foo => { });
198220
}
199221
`,
222+
output: `
223+
const handleError = error => {
224+
const error2 = new Error('foo bar');
225+
226+
obj.catch(error3 => { });
227+
}
228+
`,
200229
errors: [
201230
{
202231
ruleId: 'catch-error-name',
@@ -212,6 +241,13 @@ ruleTester.run('catch-error-name', rule, {
212241
obj.catch(foo => { });
213242
}
214243
`,
244+
output: `
245+
const handleError = error => {
246+
const error2 = new Error('foo bar');
247+
248+
obj.catch(error3 => { });
249+
}
250+
`,
215251
errors: [
216252
{
217253
ruleId: 'catch-error-name',
@@ -229,13 +265,18 @@ ruleTester.run('catch-error-name', rule, {
229265
obj.catch(err => {});
230266
obj.catch(err => {});
231267
`,
268+
output: `
269+
obj.catch(error => {});
270+
obj.catch(error => {});
271+
`,
232272
errors: [
233273
{ruleId: 'catch-error-name'},
234274
{ruleId: 'catch-error-name'}
235275
]
236276
},
237277
{
238278
code: 'try {} catch (_error) {}',
279+
output: 'try {} catch (error) {}',
239280
errors: [
240281
{
241282
ruleId: 'catch-error-name',

0 commit comments

Comments
 (0)