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] false-positive for class expressions #2831

Closed
3 tasks done
ocavue opened this issue Dec 1, 2020 · 13 comments · Fixed by #2833
Closed
3 tasks done

[no-unused-vars] false-positive for class expressions #2831

ocavue opened this issue Dec 1, 2020 · 13 comments · Fixed by #2833
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ocavue
Copy link

ocavue commented Dec 1, 2020

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

$ git clone https://github.com/ocavue/typescript-eslint-issue-2831 /tmp/eslint-issue
$ cd /tmp/eslint-issue
$ yarn install 
$ yarn run lint 

Thanks for @derekparsons718 and @lukeapage for more examples.

export const arrayWithClasses = [class C1 { }]

export const objectWithClasses = {
    controller: class MyClass { }
}

export const functionReturnsClass = (): unknown => {
    return class A {}
}

Expected Result

No warning.

Actual Result

  1:40  warning  'C1' is defined but never used       @typescript-eslint/no-unused-vars
  4:23  warning  'MyClass' is defined but never used  @typescript-eslint/no-unused-vars
  8:18  warning  'A' is defined but never used        @typescript-eslint/no-unused-vars

Additional Info

If I downgrade @typescript-eslint/eslint-plugin and @typescript-eslint/parser to version 4.8.2, no warning occurs.
Versions

package version
@typescript-eslint/eslint-plugin 4.9.0
@typescript-eslint/parser 4.9.0
TypeScript 4.1.2
ESLint 7.14.0
node 15.2.1
@ocavue ocavue added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Dec 1, 2020
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels Dec 1, 2020
@bradzacher
Copy link
Member

that's an interesting pattern I've never seen before.

@scott-lc

This comment has been minimized.

@bradzacher

This comment has been minimized.

@bradzacher bradzacher changed the title [no-unused-vars] incorrect warning for v4.9.0 [no-unused-vars] false-positive for class expressions Dec 1, 2020
@derekparsons718
Copy link

I have a very similar case. Only difference is that the class is declared as an object property instead of an array element, and the object is exported for use in other files.

export const objectWithClasses = {
    controller: class MyClass { //warning: 'MyClass' is defined but never used
        //...
    }
}

I assume this is the same problem, but I can open a new issue if desired.

@bradzacher
Copy link
Member

@derekparsons718 - yup that's the same issue with class expressions.

@lukeapage
Copy link

Assume this is the same too:

const a = () => {
    return class A {};
};

@traverse
Copy link

traverse commented Dec 3, 2020

Assume this is the same too:

const a = () => {
    return class A {};
};

Yep that one as well, just ran into that exact issue.

@ocavue
Copy link
Author

ocavue commented Dec 3, 2020

Just added the examples above into the origin issue description. In case someone else ran into the same cases.

@ocavue
Copy link
Author

ocavue commented Dec 8, 2020

@bradzacher I can confirm that version 4.9.1 fixes this issue. Thanks for your help.

@lukeapage
Copy link

@bradzacher
The example I posted is still broken in 4.9.1. New issue or will you re-open?
#2831 (comment)

@ocavue
Copy link
Author

ocavue commented Dec 8, 2020

@bradzacher

The example I posted is still broken in 4.9.1. New issue or will you re-open?

#2831 (comment)

That is strange because I run through all the examples (including yours) and it seems to be okay. Can you check your lock files (e.g. yarn.lock) and see if there are two versions of this package?

image

@bradzacher
Copy link
Member

Yup - working fine against master.
You likely have an old version installed.

@lukeapage
Copy link

Sorry! I made a mistake and had accidentally reverted the dependabot branch to 2.9.0

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
6 participants