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

feat: add suggestions to no-unused-vars #18352

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Tanujkanti4441
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

added suggestion to no-unused-vars rule.

Is there anything you'd like reviewers to focus on?

Fixes: #17545

@Tanujkanti4441 Tanujkanti4441 requested a review from a team as a code owner April 16, 2024 17:08
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Apr 16, 2024
@github-actions github-actions bot added the rule Relates to ESLint's core rules label Apr 16, 2024
Copy link

netlify bot commented Apr 16, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 947d0bd
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/662e84975310f70008c1987e

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the whole file has been reformatted, making it difficult to view the diff. Can you fix the formatting?

@@ -5,6 +5,8 @@

"use strict";

// const { parents } = require("cheerio/lib/api/traversing");
// const { remove } = require("../linter/rule-fixer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your editor might have added some unintentional imports?

@aladdin-add aladdin-add marked this pull request as draft April 17, 2024 03:40
@Tanujkanti4441
Copy link
Contributor Author

@eslint/eslint-team, code in this PR seems to work fine but having linting error that Method 'fix' expected no return value can i get some help in figuring out what is wrong or is there something i am not aware of?

@aladdin-add
Copy link
Member

aladdin-add commented Apr 22, 2024

It's required to return a value in fixers (to catch possible errors). if you don't want to fix in some cases, please use return null;

@Tanujkanti4441
Copy link
Contributor Author

Tanujkanti4441 commented Apr 23, 2024

It's required to return a value in fixers (to catch possible errors). if you don't want to fix in some cases, please use return null;

Thanks for reply!

but is this suggestion also true for the following code?

return fixer.removeRange(parent.parent.range);

because it actually returns a value and having error Method 'fix' expected no return value

lib/rules/no-unused-vars.js Outdated Show resolved Hide resolved
lib/rules/no-unused-vars.js Outdated Show resolved Hide resolved
@Tanujkanti4441 Tanujkanti4441 marked this pull request as ready for review April 26, 2024 15:16
@Tanujkanti4441
Copy link
Contributor Author

I think it's ready for review now.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments related to storing variables for later use.

Also, there are a lot of if statements that aren't obvious what they're doing. Can we add some comments describing what each is accomplishing?

* @returns {Object} fixer object
*/
function fixVariables(node) {
if (node.parent.type === "VariableDeclarator") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're using node.parent a lot, which requires looking up that property each time. I'd suggest saving a reference to both the parent and parent type so you don't have to keep evaluating it each time:

const parent  = node.parent;
const parentType = parent.type;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here node.parent is just used in fixVariable function and parent is already declared as

const parent = id.parent

is it ok if i don't replace node.parent for now because it will help me if i use the function later?

return fixer.removeRange(node.range);
}

if (sourceCode.getTokenBefore(node).value === "(" && sourceCode.getTokenAfter(node).value === ",") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're also doing getTokenBefore(node) and getTokenAfter(node) a few times here. It's best to store those values in a variable to save the function call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

return node.elements.filter(e => e !== null).length === 1;
}

if (parent.type === "VariableDeclarator") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about references throughout. We want to avoid a.b.c.d as much as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Implementing
Development

Successfully merging this pull request may close these issues.

Rule Change: Add support for suggestions for unused var
4 participants