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: [no-unused-vars] false positive if name is type/value merged and exported #6188

Closed
4 tasks done
bradzacher opened this issue Dec 9, 2022 · 4 comments · Fixed by #6873
Closed
4 tasks done

Bug: [no-unused-vars] false positive if name is type/value merged and exported #6188

bradzacher opened this issue Dec 9, 2022 · 4 comments · Fixed by #6873
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@bradzacher
Copy link
Member

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.9.3&jsx=true&sourceType=module&showAST=es&code=C4TwDgpgBAZg9nKBeKBvAvgbgFAQB5hwBOwUAxnAHYDOp8iKAFAIZEDmAXLAgJTIB8abFChEIwAK5FKUVmxzpsQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tidmUQAmtAG4BDaKgyRE0aB2iRwYAL4hlQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

type foo = {};
export const foo = (arg: foo) => {
  return arg;
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-unused-vars": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

no error because the type foo is used and the value foo is exported

Actual Result

'foo' is assigned a value but never used. 2:14 - 2:17

Additional Info

No response

@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Dec 9, 2022
@mithodin
Copy link

Also affected by this, we use the following quite often for React components:

interface Test {
    test: string;
}

export const Test = ({ test }: Test) => <>{test}</>;

@Maxim-Mazurok
Copy link
Contributor

Having the same issue with ToStringAble even though it's definitely used:

interface ToStringAble {
  toString(): string;
}
export interface EqualAble {
  equals(other: EqualAble): boolean;
}
export const ToStringAble = (object: ToStringAble): EqualAble => ({
  equals: (other: ToStringAble) => object.toString() === other.toString(),
});

@bradzacher
Copy link
Member Author

bradzacher commented Mar 30, 2023

The bug is in this function here:

function isExported(variable: TSESLint.Scope.Variable): boolean {
const definition = variable.defs[0];
if (definition) {
let node = definition.node;
if (node.type === AST_NODE_TYPES.VariableDeclarator) {
node = node.parent!;
} else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) {
return false;
}
return node.parent!.type.indexOf('Export') === 0;
}
return false;
}

Specifically that it only considers if the FIRST declaration is exported.
So if the second declaration is exported, the logic doesn't pick that up.
Ideally it should check all declarations to see if any are exported

@bradzacher bradzacher added the good first issue Good for newcomers label Mar 30, 2023
Maxim-Mazurok added a commit to Maxim-Mazurok/typescript-eslint that referenced this issue Apr 9, 2023
@Maxim-Mazurok
Copy link
Contributor

Thanks for suggestion @bradzacher , opened PR: #6873

JoshuaKGoldberg added a commit that referenced this issue Jul 17, 2023
… is exported (#6873)

* fix #6188

* optimize

* add tests

* nit: simplify to .some

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants