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: no-unused-vars
should consider this is used
#17299
Comments
Hi @fisker, can you please provide more context as to why this change is needed? The variable |
technically it's a bug to me. // same as
promise = promise ?? doSomeThing();
promise = promise ?? doSomeThing();
|
@Rec0iL99 Let me change the question. How should I fix my case? |
Ah, ok I get your point now. My apologies.
let promise;
function doSomeThingOnce() {
promise ??= doSomething();
}
function doSomeThing() {}
doSomeThingOnce();
doSomeThingOnce(); But not here where it is doing the same thing: let promise;
function doSomeThingOnce() {
promise = promise ?? doSomething();
}
function doSomeThing() {}
doSomeThingOnce();
doSomeThingOnce(); Looks like a bug. |
@Rec0iL99 It looks interesting. The So, could I consider this to be a valid expression? Now I intend to make these two codeblocks consistent, but I have encountered some difficulties. I have identified the problem in the /**
* Checks whether a given node is unused expression or not.
* @param {ASTNode} node The node itself
* @returns {boolean} The node is an unused expression.
* @private
*/
function isUnusedExpression(node) {
const parent = node.parent;
if (parent.type === "ExpressionStatement") {
// TODO we need to update this method or take some action here to resolve this question
return true;
}
if (parent.type === "SequenceExpression") {
const isLastExpression = parent.expressions[parent.expressions.length - 1] === node;
if (!isLastExpression) {
return true;
}
return isUnusedExpression(parent);
}
return false;
} |
😕 I apologize for my impatience. We should first discuss which segment's result should be considered normal before considering how to modify it. |
IMHO, it should be seen as used for |
I think var promise1, promise2;
promise1 ?? x; // this is considered using `promise1`
promise2 ??= x; // so this should be considered using `promise2` The fix would check the assignment operator here: eslint/lib/rules/no-unused-vars.js Lines 466 to 470 in 9718a97
@gweesin would you like to work on this? |
@mdjermanovic Yes, I'm glad to have the opportunity to try and resolve this issue. While attempting to fix this issue, I noticed a conflict with existing test cases. I am a bit confused and would appreciate some clarification on what behavior would be considered reasonable in this case. This is the brief example mentioned above. var promise1, promise2;
promise1 ?? x; // this is considered using `promise1`
promise2 ??= x; // so this should be considered using `promise2` if the cases are considered used, then why should the following scenario be considered as not being used? var promise1, promise2;
promise1 + 1; // this is considered using `promise1`
promise2 += 1; // however, the current test cases expects `promise2` is never used in my opinion, the If we expect I apologize for the lengthy description, but I believe it is necessary to ensure the specific expected behavior. Only then can I determine whether I need to accommodate just the current issue or make adjustments to the previous behavior and corresponding test cases as well. I am eagerly looking forward to your response. |
No. In // These are equivalent
promise2 ??= x;
if (promise2 == null) { // <-- it's used here
promise2 = x;
} Such an |
Thank you for the explanation. I think I understand why we need to do it this way. I will consider how to adjust the code accordingly. |
What rule do you want to change?
no-unused-vars
What change to do you want to make?
Generate fewer warnings
How do you think the change should be implemented?
Other
Example code
What does the rule currently do for this code?
playground
What will the rule do after it's changed?
Should not report this case.
Participation
Additional comments
No response
The text was updated successfully, but these errors were encountered: