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

Exemption for for...of unused variable. #12287

Closed
dimaqq opened this issue Sep 19, 2019 · 14 comments
Closed

Exemption for for...of unused variable. #12287

dimaqq opened this issue Sep 19, 2019 · 14 comments

Comments

@dimaqq
Copy link

@dimaqq dimaqq commented Sep 19, 2019

In similar vein to for...in #2342
Should there be similar exemption for [async] for...of?

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

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

How will the change be implemented? (New option, new default behavior, etc.)?
I don't know

Please provide some example code that this change will affect:

async function forever(x) {
  for await (const unused of x) {
    void unused;
  }
}

forever(monitor_network());

What does the rule currently do for this code?
Line 42: 'unused' is defined but never used no-unused-vars

What will the rule do after it's changed?
Nothing

Are you willing to submit a pull request to implement this change?
This seems tricky...

@mqklin

This comment has been minimized.

Copy link

@mqklin mqklin commented Sep 26, 2019

This is broken for me after eslint update to v6 as well:

for (const x of y) { // x is defined but never used (no-unused-vars)
  console.log(x);
}
@kaicataldo

This comment has been minimized.

Copy link
Member

@kaicataldo kaicataldo commented Sep 26, 2019

This looks like a duplicate of #12117. Can you see if the solutions there fix your issue?

@kaicataldo

This comment has been minimized.

Copy link
Member

@kaicataldo kaicataldo commented Sep 26, 2019

Note that this issue doesn't show in the ESLint Demo .

Are you using babel-eslint?

@mqklin

This comment has been minimized.

Copy link

@mqklin mqklin commented Sep 27, 2019

Yes, I use babel-eslint. Yes, updating babel-eslint fixed the issue, thanks a lot!

@g-plane g-plane added question and removed bug evaluating rule labels Sep 27, 2019
@g-plane

This comment has been minimized.

Copy link
Member

@g-plane g-plane commented Sep 27, 2019

Closing this issue as it looks like the question has been answered. Please feel free to visit us in the ESLint Gitter if you have any other issues!

@g-plane g-plane closed this Sep 27, 2019
@platinumazure

This comment has been minimized.

Copy link
Member

@platinumazure platinumazure commented Sep 28, 2019

@g-plane I could be missing something, but... I'm not sure this should be closed. The original post was a rule enhancement request for "for await of", but then someone else came and asked about "for of" and we noted the babel-eslint/eslint-scope issue.

Should we still evaluate the original rule request? If not, why not?

@g-plane

This comment has been minimized.

Copy link
Member

@g-plane g-plane commented Sep 28, 2019

Oh, it seems reasonable. Sorry.

@g-plane g-plane reopened this Sep 28, 2019
@g-plane g-plane added bug evaluating rule and removed question labels Sep 28, 2019
@kaicataldo

This comment has been minimized.

Copy link
Member

@kaicataldo kaicataldo commented Sep 28, 2019

I'm not able to reproduce with our demo.

@dimaqq I see you left a reaction on my comment above - did this solve your problem (hoping it did!)?

@dimaqq

This comment has been minimized.

Copy link
Author

@dimaqq dimaqq commented Sep 28, 2019

I can only test it on Monday, sorry.
I think that with void trick it ought to work; and without it I’m not sure...

@mysticatea

This comment has been minimized.

Copy link
Member

@mysticatea mysticatea commented Sep 28, 2019

I guess you can use varsIgnorePattern option to ignore unused variables by naming convention.

For example, Online Demo.

@dimaqq

This comment has been minimized.

Copy link
Author

@dimaqq dimaqq commented Sep 29, 2019

@kaicataldo

This comment has been minimized.

Copy link
Member

@kaicataldo kaicataldo commented Sep 29, 2019

None of the rules are on in the link you provided. When I turn on no-unused-vars, it’s displaying an error as expected.

@dimaqq

This comment has been minimized.

Copy link
Author

@dimaqq dimaqq commented Sep 30, 2019

I'm so sorry for the confusion.

When I read #2342 I was left with the impression that the following is allowed:

for (unused in something) {}

But of course that's only because unused could be anything here, e.g. a global, and other rules kick in now: no-undef, prefer-const.

It didn't think that the following is semantically different wrt. the unused variable rule and treated both cases the same:

for (const unused in something) {}

Weirdly there's test case for the above, as if that's valid code:

{ code: "(function(obj) { for ( const name in obj ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },

Yet, if I "enable all rules" in the eslint demo, that very example triggers no-unused-vars 😕

In any case void unused; works and keeps eslint silent.

Also, as far I can tell for await (const x of y) is treated exactly same as for (const x of y).

I guess I'm OK then :)

@kaicataldo

This comment has been minimized.

Copy link
Member

@kaicataldo kaicataldo commented Sep 30, 2019

Thanks for following up! I'm going to go ahead and close this issue then.

@kaicataldo kaicataldo closed this Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.