question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule auto-fix variable name conflicts

See original GitHub issue

The version of ESLint you are using.

v7.7.0

The problem you want to solve.

I have this code

function foo() {
	try {
	} catch (err) {
		console.log(err);

		if (test) {
			throw a;
		} else {
			throw b;
		}
	}
}

We added a rule unicorn/catch-error-name with auto-fix, it will fix err to error.

function foo() {
	try {
	} catch (error) {
		console.log(error);

		if (test) {
			throw a;
		} else {
			throw b;
		}
	}
}

We are adding a new rule prefer-ternary, we plan to fix this code to

function foo() {
	try {
	} catch (err) {
		console.log(err);

		const error = test ? a : b;
		throw error;
	}
}

Here is the problem, when we check variable name error in prefer-ternary rule and catch-error-name, they will both show error is not used, but when apply fixes, there will be conflicts.

function foo() {
	try {
	} catch (error) {
		console.log(error);

		const error = test ? a : b; // <-- error is already used.
		throw error;
	}
}

Any solution/suggestion?

Your take on the correct solution to problem.

N/A

Are you willing to submit a pull request to implement this change?

N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Oct 6, 2020

I reopened this issue to discuss if it would be useful to add something to public API (since FixTracker isn’t public), but now I realized there’s a simple way to extend the fixed range using the already existing features.

If the rule wants to fix something inside a node and make sure there will be no other fixes inside the node in the same pass, the fix() can just add empty strings around the node:

*fix(fixer) {

    // yield some fixes inside `node` ...

    // extend the range to `node`:
    yield fixer.insertTextBefore(node, "");
    yield fixer.insertTextAfter(node, "");
}

Then, the report translator will merge fixes and the unchanged code into one big fix having the range of node.

0reactions
nzakascommented, Oct 13, 2020

All right, closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Rules - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Resharper function to fix naming convention issues
When an inconsistent naming is detected, ReSharper underlines the symbol in question. Click on it. · Click on the light bulb. The first...
Read more >
Using Prettier and ESLint to automate formatting and fixing ...
Remove conflicting rules and run serially; Run Prettier followed by ESLint programmatically; Run Prettier as an ESLint rule. First, let's get a ...
Read more >
JavaScript naming conflicts: How existing code can ... - 2ality
Sometimes the name of a proposed feature (a method, a global variable, etc.) clashes with existing code and has to be changed.
Read more >
Gentle Introduction To ESLint Rules - DEV Community ‍ ‍
ESLint can determine the conflicts by inspecting the node/token range ... replaceTextRange([4, 7], "bar") // changes the var name to bar ] }....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found