Uh oh!
There was an error while loading. Please reload this page.
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
29a6eb8Comparevalscion
commented
Sep 13, 2016
Let me know if you'd like this change to also be documented somewhere :) |
gaearon
commented
Sep 13, 2016
Let's mention this somewhere in the usage guide. |
valscion
commented
Sep 14, 2016
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? |
vjeux
commented
Sep 15, 2016
(FWIW, we have the same lint rule on our PHP codebase at FB) |
valscion
commented
Sep 15, 2016
I'm quite stuck here, I don't know how to document this. Any help would be greatly appreciated 💕 |
gaearon
commented
Sep 16, 2016
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. |
gaearon
commented
Sep 16, 2016
Thank you! |
valscion
commented
Sep 18, 2016
That sounds cool! 👍 |
…eact#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 elsewhereThis 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