Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !640

Exempt variables prefixed with underscore from no-unused-vars rule

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/valscion/no-warn-underscore-prefix into master Sep 13, 2016
  • Overview 9
  • Commits 2
  • Pipelines 0
  • Changes 1

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);
screen shot 2016-09-13 at 22 45 01

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);
screen shot 2016-09-13 at 22 45 14 ## 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

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.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/valscion/no-warn-underscore-prefix