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

no-unused-vars is incorrectly raised when function name matches parameter name #2363

Closed
sokra opened this issue Apr 23, 2015 · 6 comments
Closed
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 bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@sokra
Copy link

sokra commented Apr 23, 2015

The code snippet:

function a(a) { return a; }
a();

incorrectly complain about the parameter a being unused. It seem to prefer the reference to the funcion name instead of the parameter.

Propably caused by the order in block-scoped-var.js.

@nzakas nzakas added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Apr 23, 2015
@nzakas
Copy link
Member

nzakas commented Apr 23, 2015

Thanks, we'll take a look.

@jrvidal
Copy link
Contributor

jrvidal commented May 3, 2015

I think the a reference in the return a statement is being incorrectly classified as the function name by isSelfReference. It looks like it doesn't account for shadowing.

Would it make sense to extract the shadowing checks as a common tool?

@nzakas
Copy link
Member

nzakas commented May 4, 2015

Perhaps. We do have a lot of repeating logic across rules, not just related to scoping. I'm definitely open to suggestions to eliminate duplicate code.

@jrvidal
Copy link
Contributor

jrvidal commented May 6, 2015

I've been digging a little bit about this one. Some previous discussions can be found in #2095, df9b254.

Here are a few test cases related to no-unused-vars and recursive functions:

  1. The original report:

    "use strict";
    
    function a(a) {
    // Incorrectly reports the parameter 'a' as unused
        return a;
    }
    
    a(1); // 1
  2. An analogous case with an inner function:

    "use strict";
    
    function a() {
        // Incorrectly reports the 'a' inner function as unused
        function a() {
            return 1;
        }
    
        return a();
    }
    
    a(); // 1
  3. If a function expression is used, it only gets reported if it's named:

    "use strict";
    
    var a = function () {
        // OK, not reported
        var a = 1;
    
        return a;
    };
    
    var b = function b() {
        // Incorrectly reports 'b' as unused
        var b = 1;
    
        return b;
    };
    
    var c = function d() {
        // Incorrectly reports 'd' as unused
        var d = 1;
    
        return d;
    };
    
    a(); // 1
    b(); // 1
    c(); // 1
  4. On the other hand, function expressions are not checked for self-reference:

    "use strict";
    
    // Incorrectly? considers 'a' as used
    var a = function () {
        return a();
    };
  5. Recursive function declarations are not always detected either:

    "use strict";
    
    // Incorrectly? considers 'foo' as used
    function foo() {
        return function inner() {
                return foo();
        };
    }

For now, I'm trying to find a fix for the "false positives" in 1, 2 and 3. I am not sure whether detecting all "missed negatives" (undetected unused vars) statically is even feasible/desirable.

@jrvidal
Copy link
Contributor

jrvidal commented May 7, 2015

Actually there are tests that consider case 5 above as valid. Should we consider changing this behavior?

@nzakas
Copy link
Member

nzakas commented May 7, 2015

The recursive stuff is hard to track down, especially in the case of anonymous functions. I'd say start with the first three cases and focus on getting those fixed.

jrvidal added a commit to jrvidal/eslint that referenced this issue May 8, 2015
@nzakas nzakas closed this as completed in e80046c May 8, 2015
nzakas added a commit that referenced this issue May 8, 2015
Fix: Improves detection of self-referential functions (fixes #2363)
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 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 7, 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 bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

3 participants