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-implicit-coercion autofixer can change logic

See original GitHub issue

Tell us about your environment

  • ESLint Version: 3.6.1
  • Node Version: 6.7.0
  • npm Version: 3.10.3

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

default

Please show your full configuration:

rules:
  no-implicit-coercion: error

What did you do? Please include the actual source code causing the issue.

Ran eslint --fix on:

const catNoises = ["meow", "hiss", "purr"];

if (!~catNoises.indexOf("purr")) {
  console.log("no purr");
} else {
  console.log("yes purr");
}

What did you expect to happen?

Expected ESLint to autofix the code to:

const catNoises = ["meow", "hiss", "purr"];

if (catNoises.indexOf("purr") === -1) {
  console.log("no purr");
} else {
  console.log("yes purr");
}

Or just report the problem without trying to autofix it.

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

ESLint autofixed the code to:

const catNoises = ["meow", "hiss", "purr"];

if (!catNoises.indexOf("purr") !== -1) {
  console.log("no purr");
} else {
  console.log("yes purr");
}

This autofix changes the logic of the code significantly.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
meebercommented, Sep 28, 2016

@not-an-aardvark makes a good point, although his example contains a concerning lack of animal noises.

On a similar note:

if (~catNoises.indexOf("purr") === 0) { ... }

// gets fixed to:

if (catNoises.indexOf("purr") !== -1 === 0) { ... }
0reactions
nzakascommented, Oct 1, 2016

Lets just not auto fix ~. Our policy is not to auto fix unless it can be done safely, and in this case we obviously can’t. (Trying to detect methods that return numbers is a step beyond what we should be doing.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-implicit-coercion - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
demo-outil-edition - node_modules - eslint - CHANGELOG.md
dd1e9f4 Fix: revert changes to key-spacing due to regression (#12598) (Kai ... 0541eaf Fix: no-implicit-coercion invalid autofix with ...
Read more >
eslint: no-implicit-coercion for if conditionals - Stack Overflow
1 Answer 1 · Can I use this for boolean expected context like [0, 1, undefined, null, 2].filter((elem: number | null | undefined)...
Read more >
Blog | HHVM
All built-in attributes have IDE hover documentation. Breaking Changes. lambdas contained within an anonymous function will no longer implicitly ...
Read more >
Node.js API - ESLint - Breword 文档集合
lintFiles() method will throw an error when no target files are found. ... it also runs autofixing logic, similar to the --fix flag...
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