Skip to content
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

no-plusplus with 'allowForLoopAfterthoughts': true fails if the afterthought contains multiple ++ or -- statements #13005

Closed
erhsparks opened this issue Mar 6, 2020 · 6 comments · Fixed by #13024
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@erhsparks
Copy link

erhsparks commented Mar 6, 2020

Tell us about your environment

  • ESLint Version: 6.8.0
  • Node Version: 10.15.3
  • npm Version: 6.4.1

What parser (default, Babel-ESLint, etc.) are you using?
babel-eslint

Please show your full configuration:

Configuration
module.exports = {
  env: {
    browser: true,
    es6: true,
    jest: true,
    node: true,
    mocha: true,
  },
  extends: [
    'airbnb',
    'eslint:recommended',
    'google',
    'plugin:react/recommended',
    'prettier',
    'prettier/react',
    'plugin:prettier/recommended',
    'plugin:flowtype/recommended',
  ],
  parser: 'babel-eslint',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
      modules: true,
    },
  },
  plugins: ['babel', 'react', 'prettier', 'sort-imports-es6-autofix', 'flowtype'],
  rules: {
    // specifically allowing no-plusplus for for loop afterthoughts
    'no-plusplus': ['error', { 'allowForLoopAfterthoughts': true }],

    // ...all the other rules that aren't relevant to this example
  },
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
// (actually specified in the project's .eslintrc.js, but here for visibility)

const example = [1, 2, 3, 4, 5];

// eslint errors at `i++` and `j++` here
for (let i = 0, j = i + 1; j < example.length; i++, j++) {
  // ...do stuff with example[i] and example[j]
}
$ eslint --ext .js src

What did you expect to happen?
Expected eslint not to error on i++ and j++ because { 'allowForLoopAfterthoughts': true } is specified

What actually happened? Please include the actual, raw output from ESLint.

/path/to/example
  7:48  error  Unary operator '++' used  no-plusplus
  7:53  error  Unary operator '++' used  no-plusplus

I was able to fix the warning by changing i++, j++ -> i += 1, j += 1, but this is something of an antipattern.

Yes, there are other ways to write this, i.e.

const example = [1, 2, 3, 4, 5];

// eslint does not fail at `i++` here
for (let i = 0; i < example.length - 1; i++) {
  // ...do stuff with example[i] and example[i + 1]
}

// or

// eslint does not fail at `i++` here
for (let i = 1; i < example.length; i++) {
  // ...do stuff with example[i - 1] and example[i]
}

work just fine, but:
a) I'm updating eslint on a large codebase, so I'd prefer to make as few changes as possible.
b) refactors for this simple example aside, this feels like a bug to me.

If this is the intended behaviour because as touched on in the no-plusplus docs there are unintended consequences, then I'd suggest updating that doc to specifically call out that you can only override this rule if the for loop afterthought consists only of one single ++ or --. (for example, changing the for loop to the hideous ...; i += 1, j++) as an experiment still errors on j++ because the afterthought is a larger expression)

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Mar 6, 2020
@mdjermanovic mdjermanovic added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Mar 6, 2020
@mdjermanovic
Copy link
Member

Hi @erhsparks, thanks for the issue!

In my opinion, this should be treated as a bug.

"allowForLoopAfterthoughts": true allows unary operators ++ and -- in the afterthought (final expression) of a for loop.

It already sounds like ++ and -- should be allowed anywhere inside for-loop update expressions, but I'd suggest that we fix the option to additionally allow just comma operands in that context.

@mdjermanovic
Copy link
Member

We could also fix these false negatives:

/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */

for (i++; j++;);  

@nzakas
Copy link
Member

nzakas commented Mar 9, 2020

Agreed, seems like a bug.

@yeonjuan
Copy link
Member

I agree with treating it as a bug. 👍

@mdjermanovic
Copy link
Member

Marking as accepted because 3 team members agree this is a bug.

I'm working on this.

@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Mar 10, 2020
@erhsparks
Copy link
Author

Thanks, all!

kaicataldo pushed a commit that referenced this issue Mar 18, 2020
…) (#13024)

* Fix: no-plusplus allow comma operands in for afterthought (fixes #13005)

* Allow deep sequence operands
anikethsaha pushed a commit to anikethsaha/eslint that referenced this issue Mar 23, 2020
…nt#13005) (eslint#13024)

* Fix: no-plusplus allow comma operands in for afterthought (fixes eslint#13005)

* Allow deep sequence operands
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 16, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Sep 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants