Skip to content
GitLab
    • Explore Projects Groups Snippets
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
An error occurred while fetching the assigned milestone of the selected merge_request.

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 8 years ago
  • 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.

Compare
  • master (base)

and
  • latest version
    29a6eb86
    2 commits, 2 years ago

1 file
+ 5
- 1

    Preferences

    File browser
    Compare changes
config/eslint.js
+ 5
- 1
  • View file @ 29a6eb86

  • Edit in single-file editor

  • Open in Web IDE


@@ -131,7 +131,11 @@ module.exports = {
'no-unreachable': 'warn',
'no-unused-expressions': 'warn',
'no-unused-labels': 'warn',
'no-unused-vars': ['warn', { vars: 'local', args: 'none' }],
'no-unused-vars': ['warn', {
vars: 'local',
varsIgnorePattern: '^_',
args: 'none'
}],
'no-use-before-define': ['warn', 'nofunc'],
'no-useless-computed-key': 'warn',
'no-useless-concat': 'warn',
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference:
Source branch: github/fork/valscion/no-warn-underscore-prefix

Menu

Explore Projects Groups Snippets