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

Bug: use-isnan doesn't report NaN in indexOf or lastIndexOf when fromIndex is given #18161

Closed
1 task done
Tanujkanti4441 opened this issue Mar 2, 2024 · 7 comments · Fixed by #18225
Closed
1 task done
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules

Comments

@Tanujkanti4441
Copy link
Contributor

Tanujkanti4441 commented Mar 2, 2024

Environment

Node version: latest
npm version: latest
Local ESLint version: v8.57.0
Global ESLint version:
Operating System: windows

What parser are you using?

Default (Espree)

What did you do?

i tried it in playground

/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/

var foo = myArray.indexOf((doStuff(), NaN)); // output will be -1
var foo = myArray.indexOf(NaN, 2) // output will be -1
var foo = myArray.lastIndexOf((doStuff(), NaN)); // output will be -1
var foo = myArray.lastIndexOf(NaN, 2) // output will be -1

What did you expect to happen?

I expected that use-isnan rule will report these cases as all have the output -1.

What actually happened?

the rule doesn't report these cases.

Link to Minimal Reproducible Example

https://eslint.org/play/#eyJ0ZXh0IjoiLyplc2xpbnQgdXNlLWlzbmFuOiBbXCJlcnJvclwiLCB7XCJlbmZvcmNlRm9ySW5kZXhPZlwiOiB0cnVlfV0qL1xuXG52YXIgZm9vID0gbXlBcnJheS5pbmRleE9mKChkb1N0dWZmKCksIE5hTikpOyAvLyBvdXRwdXQgd2lsbCBiZSAtMVxudmFyIGZvbyA9IG15QXJyYXkuaW5kZXhPZihOYU4sIDIpIC8vIG91dHB1dCB3aWxsIGJlIC0xXG52YXIgZm9vID0gbXlBcnJheS5sYXN0SW5kZXhPZigoZG9TdHVmZigpLCBOYU4pKTsgLy8gb3V0cHV0IHdpbGwgYmUgLTFcbnZhciBmb28gPSBteUFycmF5Lmxhc3RJbmRleE9mKE5hTiwgMikgLy8gb3V0cHV0IHdpbGwgYmUgLTEiLCJvcHRpb25zIjp7ImVudiI6e30sInJ1bGVzIjp7fSwicGFyc2VyT3B0aW9ucyI6eyJlY21hRmVhdHVyZXMiOnt9LCJlY21hVmVyc2lvbiI6ImxhdGVzdCIsInNvdXJjZVR5cGUiOiJtb2R1bGUifX19

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@Tanujkanti4441 Tanujkanti4441 added bug ESLint is working incorrectly repro:needed rule Relates to ESLint's core rules labels Mar 2, 2024
@Tanujkanti4441 Tanujkanti4441 changed the title Bug: use-insane doesn't report NaN in indexOf or lastIndexOf when fromIndex is given Bug: use-isnan doesn't report NaN in indexOf or lastIndexOf when fromIndex is given Mar 2, 2024
@mdjermanovic
Copy link
Member

mdjermanovic commented Mar 2, 2024

/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/

var foo = myArray.indexOf((doStuff(), NaN)); // output will be -1
var foo = myArray.indexOf(NaN, 2) // output will be -1
var foo = myArray.lastIndexOf((doStuff(), NaN)); // output will be -1
var foo = myArray.lastIndexOf(NaN, 2) // output will be -1

The rule does report myArray.indexOf((doStuff(), NaN)) and var foo = myArray.lastIndexOf((doStuff(), NaN)) in ESLint v9.0.0-beta.1. Handling of sequence expressions was added in #18059.

Note that the Playground is using ESLint v8.57.0.

@Tanujkanti4441
Copy link
Contributor Author

got it! but following tests are valid in ruleTester are they valid?

{
            code: "foo.lastIndexOf(NaN, 2)",
            options: [{ enforceForIndexOf: true }]
        },
        {
            code: "foo.indexOf(NaN, 4)",
            options: [{ enforceForIndexOf: true }]
        },

@mdjermanovic
Copy link
Member

As for not reporting cases with multiple arguments, it seems to be intentional:

node.arguments.length === 1 &&

{
code: "foo.lastIndexOf(NaN, b)",
options: [{ enforceForIndexOf: true }]
},

I believe this is because we don't know for sure if .indexOf()/.lastIndexOf() are really calls to Array.prototype methods, and an unexpected number of arguments is an indication that they're not. However, we may have missed the fact that these methods have two parameters.

I think it's fine to update this rule to report cases with two arguments, and treat this as a semver-minor bug fix. Curious what others think.

@mdjermanovic
Copy link
Member

So, the question is whether these cases should be reported:

/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/

var foo = myArray.indexOf(NaN, 2) // output will be -1
var foo = myArray.lastIndexOf(NaN, 2) // output will be -1

@fasttime
Copy link
Member

fasttime commented Mar 4, 2024

I think it's fine to update this rule to report cases with two arguments, and treat this as a semver-minor bug fix. Curious what others think.

Makes sense to me 👍🏻 The logic to check indexOf and lastIndexOf calls was added in #12379, but I cannot find a discussion about restricting the applicability of the check to calls with one argument. If the two arguments case was not properly considered, it seems fine to treat this change as a bugfix.

@Tanujkanti4441
Copy link
Contributor Author

Tanujkanti4441 commented Mar 22, 2024

@eslint/eslint-team are we up for implementing this?

@mdjermanovic
Copy link
Member

Yes, PR is welcome.

@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 22, 2024
@Tanujkanti4441 Tanujkanti4441 self-assigned this Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants