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-implicit-coercion: <string expression> + "" triggers false positive #14623
Comments
Hi @martyfmelb, thanks for the issue!
While it's true that there will be no string coercion, because all operands evaluate to string values, fixing
We could add this particular check, to mirror the behavior of this rule for numbers: /* eslint no-implicit-coercion: "error" */
var foo = { x: 2 }.x * 1; // error
var foo = [2][0] * 1; // error
var foo = Number(a) * 1; // no error! |
ESLint doesn't infer types, so the rule doesn't differentiate between string coercions of non-string values and redundant operations on values that are already strings. One could build two type-aware I'd be fine detecting the explicit |
Completely agree with @btmills, this is something that could be done in @typescript-eslint/eslint-plugin rather than in the core.
I marked this issue as accepted to fix this part only, and prepared PR #14641. |
Tell us about your environment
Node version: v10.15.0
npm version: v6.4.1
Local ESLint version: v7.25.0 (Currently used)
Global ESLint version: Not found
Operating System: win32 10.0.19042
What parser (default,
@babel/eslint-parser
,@typescript-eslint/parser
, etc.) are you using?@typescript-eslint/parser
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
Here are some example statements which trigger it with our particular
eslintrc
setup:Swapping the operands of
+
makes no difference.Changing the empty string to any other value in all of these cases removes this error. I've tried
"a"
, the number 0, false, true, null, empty array[]
, all cases for which I'd expect to find a message about implicit coercion — they all, counterintuitively, remove the error.I am running it from MINGW64 (i.e., bash on Windows):
./node_modules/.bin/eslint "/C/Projects/Client Website/client-site/Client.Site/Client/Site/Client.Squad.Web.UI/Assets/Client/Site/js/ng/directives/domain/domain-feature.js"
What did you expect to happen?
No error should result from the cases above.
For example, for the very first example above I would expect the output:
What actually happened? Please include the actual, raw output from ESLint.
See the ESLint messages above.
The text was updated successfully, but these errors were encountered: