`no-plusplus` with `'allowForLoopAfterthoughts': true` fails if the afterthought contains multiple `++` or `--` statements
See original GitHub issueTell 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)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Hi @erhsparks, thanks for the issue!
In my opinion, this should be treated as a bug.
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.Marking as accepted because 3 team members agree this is a bug.
I’m working on this.