Skip to content

Commit

Permalink
catch-error-name: Use underscore naming pattern (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHen authored and sindresorhus committed May 31, 2019
1 parent 7fbadf1 commit 0f8d5b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
6 changes: 3 additions & 3 deletions docs/rules/catch-error-name.md
Expand Up @@ -59,10 +59,10 @@ try {

```js
const handleError = error => {
const error2 = new Error('🦄');
const error_ = new Error('🦄');

obj.catch(error3 => {
// `error3` is allowed because of shadowed variables
obj.catch(error__ => {
// `error__` is allowed because of shadowed variables
});
}
```
Expand Down
23 changes: 9 additions & 14 deletions rules/catch-error-name.js
@@ -1,5 +1,6 @@
'use strict';
const astUtils = require('eslint-ast-utils');
const avoidCapture = require('./utils/avoid-capture');
const getDocsUrl = require('./utils/get-docs-url');

// Matches `someObj.then([FunctionExpression | ArrowFunctionExpression])`
Expand All @@ -25,19 +26,11 @@ function isLintablePromiseCatch(node) {
return arg0.type === 'FunctionExpression' || arg0.type === 'ArrowFunctionExpression';
}

// TODO: Use `./utils/avoid-capture.js` instead
function indexifyName(name, scope) {
const variables = scope.variableScope.set;

let index = 1;
while (variables.has(index === 1 ? name : name + index)) {
index++;
}

return name + (index === 1 ? '' : index);
}

const create = context => {
const {
ecmaVersion
} = context.parserOptions;

const options = {
name: 'error',
caughtErrorsIgnorePattern: '^_$',
Expand Down Expand Up @@ -96,7 +89,8 @@ const create = context => {
return;
}

const errorName = indexifyName(name, context.getScope());
const scope = context.getScope();
const errorName = avoidCapture(name, [scope.variableScope], ecmaVersion);
push(params.length === 0 || params[0].name === errorName || errorName);
}
},
Expand All @@ -118,7 +112,8 @@ const create = context => {
return;
}

const errName = indexifyName(name, context.getScope());
const scope = context.getScope();
const errName = avoidCapture(name, [scope.variableScope], ecmaVersion);
push(node.param.name === errName || errName);
},
'CatchClause:exit': node => {
Expand Down
92 changes: 59 additions & 33 deletions test/catch-error-name.js
Expand Up @@ -28,62 +28,62 @@ ruleTester.run('catch-error-name', rule, {
const handleError = error => {
try {
doSomething();
} catch (error2) {
console.log(error2);
} catch (error_) {
console.log(error_);
}
}
`),
testCase(`
const handleError = err => {
try {
doSomething();
} catch (err2) {
console.log(err2);
} catch (err_) {
console.log(err_);
}
}
`, 'err'),
testCase(`
const handleError = error => {
const error2 = new Error('🦄');
const error_ = new Error('🦄');
try {
doSomething();
} catch (error3) {
console.log(error3);
} catch (error__) {
console.log(error__);
}
}
`),
testCase('obj.catch(error => {})'),
testCase(`
const handleError = error => {
obj.catch(error2 => { });
obj.catch(error_ => { });
}
`),
testCase(`
const handleError = err => {
obj.catch(err2 => { });
obj.catch(err_ => { });
}
`, 'err'),
testCase(`
const handleError = error => {
const error2 = new Error('foo bar');
const error_ = new Error('foo bar');
obj.catch(error3 => { });
obj.catch(error__ => { });
}
`),
testCase(`
const handleError = error => {
const error2 = new Error('foo bar');
const error3 = new Error('foo bar');
const error4 = new Error('foo bar');
const error5 = new Error('foo bar');
const error6 = new Error('foo bar');
const error7 = new Error('foo bar');
const error8 = new Error('foo bar');
const error9 = new Error('foo bar');
const error10 = new Error('foo bar');
const error_ = new Error('foo bar');
const error__ = new Error('foo bar');
const error___ = new Error('foo bar');
const error____ = new Error('foo bar');
const error_____ = new Error('foo bar');
const error______ = new Error('foo bar');
const error_______ = new Error('foo bar');
const error________ = new Error('foo bar');
const error_________ = new Error('foo bar');
obj.catch(error11 => { });
obj.catch(error__________ => { });
}
`),
testCase('obj.catch(() => {})'),
Expand Down Expand Up @@ -166,6 +166,23 @@ ruleTester.run('catch-error-name', rule, {
}
`,
output: `
const handleError = error => {
try {
doSomething();
} catch (error_) {
console.log(error_);
}
}
`,
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error_`.'
}
]
},
{
code: `
const handleError = error => {
try {
doSomething();
Expand All @@ -174,10 +191,19 @@ ruleTester.run('catch-error-name', rule, {
}
}
`,
output: `
const handleError = error => {
try {
doSomething();
} catch (error_) {
console.log(error_);
}
}
`,
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error2`.'
message: 'The catch parameter should be named `error_`.'
}
]
},
Expand All @@ -199,59 +225,59 @@ ruleTester.run('catch-error-name', rule, {
try {
doSomething();
} catch (error2) {
console.log(error2);
} catch (error_) {
console.log(error_);
}
}
`,
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error2`.'
message: 'The catch parameter should be named `error_`.'
}
]
},
{
code: `
const handleError = error => {
const error2 = new Error('foo bar');
const error_ = new Error('foo bar');
obj.catch(foo => { });
}
`,
output: `
const handleError = error => {
const error2 = new Error('foo bar');
const error_ = new Error('foo bar');
obj.catch(error3 => { });
obj.catch(error__ => { });
}
`,
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error3`.'
message: 'The catch parameter should be named `error__`.'
}
]
},
{
code: `
const handleError = error => {
const error2 = new Error('foo bar');
const error_ = new Error('foo bar');
obj.catch(foo => { });
}
`,
output: `
const handleError = error => {
const error2 = new Error('foo bar');
const error_ = new Error('foo bar');
obj.catch(error3 => { });
obj.catch(error__ => { });
}
`,
errors: [
{
ruleId: 'catch-error-name',
message: 'The catch parameter should be named `error3`.'
message: 'The catch parameter should be named `error__`.'
}
],
options: [
Expand Down

0 comments on commit 0f8d5b4

Please sign in to comment.