Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-unused-vars]: reporter should report the last reference instead of declaration #13181

Closed
anikethsaha opened this issue Apr 15, 2020 · 6 comments · Fixed by #13354
Closed
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules

Comments

@anikethsaha
Copy link
Member

What rule do you want to change?
no-unused-vars

Does this change cause the rule to produce more or fewer warnings?
same

How will the change be implemented? (New option, new default behavior, etc.)?
default

Please provide some example code that this change will affect:
Currently, the reporter reports the location in a declaration of a variable which is fine but not for all cases.

case 1

const unusedVar = 1; // reporting here is ok

case2

let a = 1 // currently its reporting here,  reporting here is not ok as `a` has references though its being self referred 
a = a + 1;
a = a + 2;
a = a + 3; // it should report here

In this way, it makes easy to track the error if any.
Another advantage of reporting this way is many editors do provide shortcut keys to jump to the declaration of any variable/function but the opposite is not true for some.

ref : #12871, #13172

What does the rule currently do for this code?
reports in the variable declaration

What will the rule do after it's changed?
reports in the last reference of the variable if the variable is being self-referred

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

@anikethsaha anikethsaha added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Apr 15, 2020
@nzakas
Copy link
Member

nzakas commented Apr 16, 2020

Thanks for writing this up. Just to make sure I understand, you’re suggesting that the last reference of an unused variable be highlighted? And if there is only a declaration then the declaration is highlighted?

@anikethsaha
Copy link
Member Author

Yes

@nzakas
Copy link
Member

nzakas commented Apr 16, 2020

That sounds good to me. It would be nice if we could use a clearer message to, something like "The value of variable foo is never used after this assignment" or something similar.

@nzakas nzakas added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Apr 16, 2020
@anikethsaha
Copy link
Member Author

"The value of variable foo is never used after this assignment"

much better 👍

@Lonniebiz
Copy link

Lonniebiz commented Apr 25, 2020

This bug confused me (as shown in 13227).

// esLint version 6.7.2
async function fnTest()
{
	return "Promise will resolve to this string.";
}
async function main()
{
	let usedVariable = fnTest(); // esLint thinks usedVariable is unused
	// Omitted: Do other things before awaiting usedVariable
	usedVariable = await usedVariable; // this reassignment is what is unused (not the first line of this function as reported by eslint)
}

eslint reports line 1 of main() as unused, but it is actually line 3 of main() that is unused. eslint should report line 3 of that function-body as "unused reassignment". To me that seems like the most clear thing to call it.

@anikethsaha
Copy link
Member Author

I have submitted the PR #13354 to move this issue forward quickly.

nzakas pushed a commit that referenced this issue Jun 1, 2020
…#13354)

* Fix: changed reporter loc to last reference

* Chore: added some tests

* Chore: fixed no-unused loc for linter tests

* Update: refactored code using ternary exp
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 29, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants