8000 [no-unused-vars] Prevent false positives with classes · Issue #586 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

[no-unused-vars] Prevent false positives with classes #586

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

Closed
luxaritas opened this issue Jun 3, 2019 · 5 comments
Closed

[no-unused-vars] Prevent false positives with classes #586

luxaritas opened this issue Jun 3, 2019 · 5 comments
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@luxaritas
Copy link
Contributor
luxaritas commented Jun 3, 2019

Repro

{
  "rules": {
    "@typescript-eslint/no-unused-vars": ["error"]
  }
}
class Base {
    public f(value) {
        throw new Error("IllegalOperationError");
    }
}

class Impl extends Base {
    public f(value) {
        console.log(value);
    }
}

class SecondaryImpl extends Base {
    public f(value) {
        console.log("Irrelevant");
    }
}

Expected Result
No violation

Actual Result
Violation (twice)

Additional Info
For some reason when I filed #52, I apparently thought this rule already did this, not sure why, but it's the same idea.

Versions

package version
@typescript-eslint/eslint-plugin 1.9.0
@typescript-eslint/parser 1.9.0
TypeScript 3.3.4000
ESLint 5.15.3
node 8.11.3
npm 6.9.0
@luxaritas luxaritas added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jun 3, 2019
@bradzacher
Copy link
Member

you mention there's two violations.
What are they? What lines?

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Jun 3, 2019
@luxaritas
Copy link
Contributor Author

Whoops sorry.
'value' is defined but never used (@typescript-eslint/no-unused-vars), on both lines 2 and 14.

@bradzacher
Copy link
Member

to me those violations look correct.
I suspect you missed something in your example code, because currently you have 3 isolated classes, 2 of which have unused parameters.

@luxaritas
Copy link
Contributor Author

Yikes, the latter two classes were supposed to extend Base. Fixed in the example code.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed awaiting response Issues waiting for a reply from the OP or another party triage Waiting for team members to take a look labels Jun 3, 2019
@bradzacher
Copy link
Member

Revisiting this - this will never be a truly supported use case.
Detecting that a class has a subclass that defines a method which uses the argument is very complex and (unless I'm mistaken) it's not possible with typescript's current checker API.

You can get around this using the following rule config, and by deleting the unused argument in SecondaryImpl. This is both valid TypeScript code, and lints fine.

/* eslint no-unused-vars: ['error', { argsIgnorePattern: "^_" }] */
class Base {
	public f(_value: string) {
        throw new Error("IllegalOperationError");
    }
}

export class Impl extends Base {
    public f(value: string) {
        console.log(value);
    }
}

export class SecondaryImpl extends Base {
    public f() {
        console.log("Irrelevant");
    }
}

typescript repl
eslint repl

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants
0