-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Exempt variables prefixed with underscore from no-unused-vars rule #640
Conversation
This is useful when e.g. using object spread operator to remove only a certain field from the object. For example, this can be used to ignore a property from React component's `this.props`: render() { const { someAttribute: _unused, ...rest } = this.props; return <pre>{ JSON.stringify(rest) }</pre>; }
4fe8a30
to
29a6eb8
Compare
Let me know if you'd like this change to also be documented somewhere :) |
Let's mention this somewhere in the usage guide. |
I'm looking at the template/README.md file and I feel a bit unsure where this would fit in. This change is quite small so I had hoped there would've been an existing section for linting or object spreads but there isn't one. That file itself is already quite large so that makes me even more wary about tucking in this little detail as I fear I'd too much irrelevant details. What would a section covering this lint rule have? Should it contain more details about the linting in general, or just a one sentence note about this feature? |
(FWIW, we have the same lint rule on our PHP codebase at FB) |
I'm quite stuck here, I don't know how to document this. Any help would be greatly appreciated 💕 |
Ah, okay, let’s just sneak it in and maybe document it in some way when we release a package of our lint rules separately. |
Thank you! |
That sounds cool! 👍 |
…acebook#640) * Split no-unused-vars ESLint config to multiple lines * Exempt variables prefixed with underscore from no-unused-vars rule This is useful when e.g. using object spread operator to remove only a certain field from the object. For example, this can be used to ignore a property from React component's `this.props`: render() { const { someAttribute: _unused, ...rest } = this.props; return <pre>{ JSON.stringify(rest) }</pre>; }
This is useful when e.g. using object spread operator to remove only a
certain field from the object.
For example, this can be used to ignore a property from React
component's
this.props
:This PR was asked by Dan via Twitter when he replied to my tweet about ignoring unused variables with underscore prefix: https://twitter.com/dan_abramov/status/775764929894809602
Test plan:
Write this to any JS file in the project and observe no warnings are shown:
Then try to change it to this and observe warnings are shown:
## Same approach elsewhere
This approach is similar in nature to what
rubocop
, the de-facto linter in Ruby, specifies in its default config as well: https://github.com/bbatsov/ruby-style-guide#underscore-unused-vars