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

Rule Change: Add option to no-unused-vars that will report a variable that has been marked as ignored but is being used. #17568

Closed
1 task done
Pearce-Ropion opened this issue Sep 15, 2023 · 9 comments · Fixed by #17662
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@Pearce-Ropion
Copy link
Contributor

Pearce-Ropion commented Sep 15, 2023

What rule do you want to change?

no-unused-vars

What change to do you want to make?

Generate more warnings

How do you think the change should be implemented?

A new option

Example code

/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }] */

const _myUnusedVariable = 10;

export const myUsedVariable = _myUnusedVariable + 10;

What does the rule currently do for this code?

Currently, the rule will not report this case because _myUnusedVariable is being used.

What will the rule do after it's changed?

The rule would report that a variable that has been marked as an "ignored unused variable" is actually being used.

I propose a new option, potentially called reportUsedIgnorePattern. Optionally, there could be a version of this option for each of the different ignore pattern options.

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

The main reason for this is that I've found that devs will name a variable with our ignore pattern since its a simple way to have another variable with a very similar name. I would like to instead encourage devs to rename their variables with a clear meaning by not allowing them to use the ignore pattern for a used variable.

I acknowledge that part of this is that we have a very simple ignore pattern, but I feel like this is still something that should be enforceable by the lint rule.

@Pearce-Ropion Pearce-Ropion added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Sep 15, 2023
@Rec0iL99
Copy link
Member

Hi @Pearce-Ropion, thanks for the issue. Can you please provide a few more usage examples to support this proposal?

Currently, the rule will not report this case because _myUnusedVariable is being used.

The rule will not check _myUnusedVariable for usage since it matches the ignore pattern (docs).

@Rec0iL99 Rec0iL99 added the needs info Not enough information has been provided to triage this issue label Sep 15, 2023
@Rec0iL99
Copy link
Member

Rec0iL99 commented Oct 4, 2023

No response so closing.

@Rec0iL99 Rec0iL99 closed this as completed Oct 4, 2023
@eslint-github-bot
Copy link

It looks like there wasn't enough information for us to know how to help you, so we're closing the issue.

Thanks for your understanding.

@Pearce-Ropion
Copy link
Contributor Author

Pearce-Ropion commented Oct 4, 2023

@Rec0iL99 Apologies, I had written out a response but I guess I never sent it. TIL GitHub caches draft comments a lot longer than expected.

The idea, is that if an ignore pattern is set, users should only be able to use that variable naming pattern for unused variables.

This increases code readability as a quick scan of the code would indicate to the user that a variable that is marked with an ignore pattern is unused.

In the current implementation, users are able to use variables that are marked with by the unused variable ignore pattern which decreases readability as you wonder why the variable was marked as "ignore-unused" given that it is actually being used.

I think my original example snippet makes the most sense but I can see how further explanation would be helpful.

Given

/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }] */

const _myUnusedVariable = 10;

export const myUsedVariable = _myUnusedVariable + 10;

My immediate thought when reading this code is: "why is _myUnusedVariable marked as an ignored unused variable (given the ignore pattern) when it is being used in the next line". Did the developer use the variable incorrectly? Should this variable actually be ignored?

Another example could be:

import { createElement } from './utils';

const _createElement = (tagName, id) => {
  return createElement(tagName, {
    id,
  });
};

const table = _createElement('table', 'goo');

Again, my immediate thought my here is why is _createTable marked as unused. I my mind, a marked unused variable should actually be unused so that I can just discount it as not important when reading over the code. But here, I have to go back and re-read the code to understand why the unused variable syntax was used and understand why the variable is important.

@Rec0iL99
Copy link
Member

Rec0iL99 commented Oct 8, 2023

Thanks for the extra info. I think I now understand your proposal. This looks quite similar to #13183. Can you please verify?

@Rec0iL99 Rec0iL99 added needs info Not enough information has been provided to triage this issue and removed needs info Not enough information has been provided to triage this issue labels Oct 8, 2023
@Pearce-Ropion
Copy link
Contributor Author

@Rec0iL99 Yes, this looks like the same idea. By marking a variable as ignored we are saying that it is now expected that it should never be read. By then using it, it violates the expected contract of what an unused variable should be.

My idea with making this rule suggesting is akin to what @mdjermanovic states here:

On the other hand, this looks useful, and duplicating the configured patterns and the overall logic for unused vars in another rule might not make sense.

It would be relatively easy to integrate this into the existing rule but duplicating the all of the logic that this rule employs into another rule just to provide this functionality might not make sense.

@Rec0iL99
Copy link
Member

Thanks. Pushing it forward for the core team to look into.

@Rec0iL99 Rec0iL99 reopened this Oct 10, 2023
@Rec0iL99 Rec0iL99 removed the needs info Not enough information has been provided to triage this issue label Oct 10, 2023
@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Oct 13, 2023
@nzakas
Copy link
Member

nzakas commented Oct 13, 2023

To summarize my understanding of the request:

The varsIgnorePattern option is meant to prevent the rule from flagging variables that match a certain naming pattern as unused. This allows developers to have unused variables that the rule won't complain about. However, if a variable matching varsIgnorePattern is actually used, then something isn't right: either the variable is named incorrectly or the rule is configured incorrectly. Right now, there is no way for developers to know that such a condition exists.

The proposal is to add an option (reportUsedIgnorePattern) that allows the rule to warn in this situation.

I wonder if this would actually make sense to enable this option by default?

I think this is a good option to add to the rule, and as it seemed that @mdjermanovic believed a similar proposal was useful in #13183, I'm marking as accepted.

@Pearce-Ropion if you'd like to open a PR, we can get the ball rolling.

@Pearce-Ropion
Copy link
Contributor Author

Great! I'll start looking into the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants