question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Mar 6, 2020

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.

1reaction
mdjermanoviccommented, Mar 10, 2020

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

I’m working on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

disallow the unary operators ++ and -- (no-plusplus) - ESLint
This rule has an object option. "allowForLoopAfterthoughts": true allows unary operators ++ and -- in the afterthought (final expression) of a for loop....
Read more >
no-plusplus - ESLint - Pluggable JavaScript Linter
This rule has an object option. "allowForLoopAfterthoughts": true allows unary operators ++ and -- in the afterthought (final expression) of a for loop....
Read more >
eslint error Unary operator '++' used no-plusplus
It is just that your default configuration and/or the coding style you are using does not allow prefix/postfix increment/decrement operators.
Read more >
No-plusplus - ESLint - W3cubDocs
This rule has an object option. "allowForLoopAfterthoughts": true allows unary operators ++ and -- in the afterthought (final expression) of a for ...
Read more >
Eslint Error Unary Operator '++' Used Noplusplus - ADocLib
This rule has an object option.allowForLoopAfterthoughts: true allows unary operators ++ and in the afterthought final expression of a for loop.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found