Created by: valscion
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
:
class OpinionatedDebugger extends React.Component {
render() {
const { justIgnoreMe: _unused, ...rest } = this.props;
return <pre>{ JSON.stringify(rest) }</pre>;
}
}
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:
var obj = { a: 1, b: 2 };
var { a: _unused, ...rest } = obj;
console.log(rest);
Then try to change it to this and observe warnings are shown:
var obj = { a: 1, b: 2 };
var { a, ...rest } = obj;
console.log(rest);
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
Prefix with
_
unused block parameters and local variables. It's also acceptable to use just_
(although it's a bit less descriptive). This convention is recognized by the Ruby interpreter and tools like RuboCop and will suppress their unused variable warnings.