Repository: sindresorhus/eslint-plugin-unicorn (JavaScript)

Issue:
`catch-error-name` fixer bug
<!-- Issuehunt Badges -->

[<img alt="Issuehunt badges" src="https://issuehunt.io/r/sindresorhus/eslint-plugin-unicorn/issues/255/badge.svg" />](https://issuehunt.io/r/sindresorhus/eslint-plugin-unicorn/issues/255)

<!-- /Issuehunt Badges -->

`catch-error-name` fixer bug

error code like

```js
p.catch(err => doSomething(err))
```

will fix like this

```js
p.catch(error => doSomething(err))
```

the param `err` in catch function body is not replaced

 I noticed there is a bug #107 , but please make this rule not fixable for now please, this breaks code
<!-- Issuehunt content -->



> [futpib](https://issuehunt.io/u/futpib) earned $40.00 by resolving this issue!
> - Checkout the [Issuehunt explorer](https://issuehunt.io/r/sindresorhus/eslint-plugin-unicorn/) to discover more funded issues.
> - Need some help from other developers? [Add your repositories](https://issuehunt.io/r/new) on Issuehunt to raise funds.


<!-- /Issuehunt content-->


Discussion:
@issuehunt has funded $40.00 to this issue.

---
- Submit pull request via [IssueHunt](https://issuehunt.io/repos/55832243/issues/255) to receive this reward.
- Want to contribute? Chip in to this issue via [IssueHunt](https://issuehunt.io/repos/55832243/issues/255).
- Checkout the [IssueHunt Issue Explorer](https://issuehunt.io/issues) to see more funded issues.
- Need help from developers? [Add your repository](https://issuehunt.io/users/44827199/repositories) on IssueHunt to raise funds.
@sindresorhus has rewarded $36.00 to @futpib. [See it on IssueHunt](https://issuehunt.io/repos/55832243/issues/255)

- :moneybag: Total deposit: $40.00
- :tada: Repository reward(0%): $0.00
- :wrench: Service fee(10%): $4.00



Patch:
diff --git a/rules/catch-error-name.js b/rules/catch-error-name.js
index d6d9b577c5..54cfd8820c 100644
--- a/rules/catch-error-name.js
+++ b/rules/catch-error-name.js
@@ -43,6 +43,8 @@ const create = context => {
 		caughtErrorsIgnorePattern: '^_$'
 	}, context.options[0]);
 
+	const {scopeManager} = context.getSourceCode();
+
 	const {name} = options;
 	const caughtErrorsIgnorePattern = new RegExp(options.caughtErrorsIgnorePattern);
 	const stack = [];
@@ -55,7 +57,7 @@ const create = context => {
 		stack.push(stack.length > 0 || value);
 	}
 
-	function popAndReport(node) {
+	function popAndReport(node, scopeNode) {
 		const value = stack.pop();
 
 		if (value !== true && !caughtErrorsIgnorePattern.test(node.name)) {
@@ -69,12 +71,10 @@ const create = context => {
 				problem.fix = fixer => {
 					const fixings = [fixer.replaceText(node, expectedName)];
 
-					const scope = context.getScope();
+					const scope = scopeManager.acquire(scopeNode);
 					const variable = scope.set.get(node.name);
-					if (variable) {
-						for (const reference of variable.references) {
-							fixings.push(fixer.replaceText(reference.identifier, expectedName));
-						}
+					for (const reference of variable.references) {
+						fixings.push(fixer.replaceText(reference.identifier, expectedName));
 					}
 
 					return fixings;
@@ -101,7 +101,8 @@ const create = context => {
 		},
 		'CallExpression:exit': node => {
 			if (isLintablePromiseCatch(node)) {
-				popAndReport(node.arguments[0].params[0]);
+				const callbackNode = node.arguments[0];
+				popAndReport(callbackNode.params[0], callbackNode);
 			}
 		},
 		CatchClause: node => {
@@ -120,7 +121,7 @@ const create = context => {
 			push(node.param.name === errName || errName);
 		},
 		'CatchClause:exit': node => {
-			popAndReport(node.param);
+			popAndReport(node.param, node);
 		}
 	};
 };


Test patch:
diff --git a/test/catch-error-name.js b/test/catch-error-name.js
index 258df6037f..316442558f 100644
--- a/test/catch-error-name.js
+++ b/test/catch-error-name.js
@@ -133,17 +133,17 @@ ruleTester.run('catch-error-name', rule, {
 	],
 
 	invalid: [
-		testCase('try {} catch (err) {}', null, true, 'try {} catch (error) {}'),
-		testCase('try {} catch (error) {}', 'err', true, 'try {} catch (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) {}'),
-		testCase('obj.catch(err => {})', null, true, 'obj.catch(error => {})'),
-		testCase('obj.catch(error => {})', 'err', true, 'obj.catch(err => {})'),
+		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(({message}) => {})', null, true),
-		testCase('obj.catch(function (err) {})', null, true, 'obj.catch(function (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) {})', 'err', true, 'obj.catch(function (err) {})'),
+		testCase('obj.catch(function (error) { console.log(error) })', 'err', true, 'obj.catch(function (err) { console.log(err) })'),
 		// Failing tests for #107
 		// testCase(`
 		// 	foo.then(() => {