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

False positive in prefer-rest-params when checking arguments.length #5990

Closed
mik01aj opened this issue Apr 28, 2016 · 14 comments
Closed

False positive in prefer-rest-params when checking arguments.length #5990

mik01aj opened this issue Apr 28, 2016 · 14 comments
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@mik01aj
Copy link

mik01aj commented Apr 28, 2016

ESLint version: 2.8.0

What parser are you using?

    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module",
        "ecmaFeatures": {"jsx": true},
    },

When linting this code:

function makeTree(rootPath, branches) {
    console.assert(arguments.length === 2);
    console.assert(_.isArray(rootPath));
    console.assert(_.isArray(branches) && _.isArray(branches[0]));
    return _.map(branches, branch => rootPath.concat(branch));
}

I get this warning:

Use the rest parameters instead of 'arguments'  prefer-rest-params

I believe this to be a false positive. Imho just checking arguments.length should be an exception to this rule (possibly configurable).

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Apr 28, 2016
@platinumazure
Copy link
Member

Hmm. This is a tough one. On the one hand, it would be easy to add a rest param and assert that its length is zero; on the other hand, introducing another variable that you won't even really use just to satisfy the rule would be pretty silly.

@eslint/eslint-team Do we consider this a bug, or an enhancement request?

@nzakas
Copy link
Member

nzakas commented Apr 28, 2016

It's an enhancement request, but one I don't think we should do. The purpose of the rule is to flag uses of arguments, which it is doing. You can replace the arguments length check by checking if branches is undefined.

@michaelficarra
Copy link
Member

@nzakas No, that is not how you would replace the arguments.length check. Still, it can be removed using rest parameters, so this rule is working as intended. But I'd support an option to allow arguments.length since I'd prefer to read a function with two parameters in the parameter list than see a rest parameter that is later destructured.

@nzakas nzakas added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Apr 28, 2016
@nzakas
Copy link
Member

nzakas commented Apr 28, 2016

@michaelficarra fair enough, are you willing to champion this option?

@michaelficarra
Copy link
Member

No. By support, I meant endorse. I do not plan on participating.

@mikesherov
Copy link
Contributor

@mik01aj, thanks for contributing. @nzakas, I'm torn on this one. While I do think the provided example doesn't show the full breadth of situations in which arguments.length is preferable, there are lots of good use cases, especially libraries with lots of variadic functions. Looking at the jQuery source, for example, yields 19 uses of arguments.length to power heavily overloaded functions.

Now, I'm not a fan of such parameter hockey in my own APIs, but arguments.length is nifty to have around even if you don't want arguments itself.

I'm not going to champion this one but I would give it a 👍 if someone else did.

@alberto
Copy link
Member

alberto commented Aug 1, 2016

@eslint/eslint-team is anyone willing to champion this?

@mysticatea
Copy link
Member

It seems reasonable to me.
Also, this is a relaxing enhancement, so this is a minor fix.

I will champion this.

@mysticatea mysticatea self-assigned this Aug 2, 2016
@alberto alberto added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Aug 2, 2016
@platinumazure
Copy link
Member

Just to make sure, we are expanding this to support any member expression checks where arguments is the object and the property is non-numeric per discussion in #6835?

@mikesherov
Copy link
Contributor

@platinumazure, seems correct.

@mik01aj
Copy link
Author

mik01aj commented Aug 4, 2016

@platinumazure According to docs on MDN, there are just these:

  • arguments.callee
  • arguments.caller
  • arguments.length
  • arguments[@@iterator]

I don't think that accessing the callee and caller should be allowed.

@platinumazure
Copy link
Member

@mik01aj Regarding callee/caller, we do have a separate rule for that (no-caller).

@mikesherov
Copy link
Contributor

Right, I agree @mik01aj but the rule is about prefering rest parameters over arguments, so erroring on using arguments other than as a parameter shouldn't cause an issue.

@mik01aj
Copy link
Author

mik01aj commented Aug 5, 2016

@platinumazure good point, thx. Agree.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

8 participants