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 negative with variables matching HTML element names #2985

Closed
3 tasks done
karlhorky opened this issue Jan 30, 2021 · 8 comments
Closed
3 tasks done
Labels
external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@karlhorky
Copy link

  • 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

{
  "rules": {
    "@typescript-eslint/no-unused-vars": [
      "warn",
      {
        "args": "after-used",
        "ignoreRestSiblings": true,
      },
    ],
  }
}
// False Negative:
// The `div` parameter below is not recognized as an unused variable
function App(div: any) {
  return <div />
}

This also happens with other variables (eg. variables declared separately):

const div = 123;

export function App() {
  return <div />;
}

However, the following correctly warns:

// Any parameter name not matching lowercase HTML tags will be
// correctly recognized as an unused variable
function App(somethingElse: any) {
  return <div />
}

Screenshots

False Negative

Screen Shot 2021-01-30 at 12 26 49

Correct Warning

Screen Shot 2021-01-30 at 12 24 21

tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "UpLeveled Node + React TSConfig",

  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "target": "es2015",

    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "isolatedModules": true,

    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "downlevelIteration": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react-jsx",
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "skipLibCheck": true,
    "strict": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
  "exclude": ["node_modules", "build"]
}

Expected Result

Any variables matching HTML element names should warn and error with @typescript-eslint/no-unused-vars

Actual Result

No errors or warnings for variable names matching HTML element names.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.14.1
@typescript-eslint/parser 4.14.1
TypeScript 4.1.2
ESLint 7.18.0
node 15.5.0
@karlhorky karlhorky added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jan 30, 2021
@bradzacher
Copy link
Member

Here is the related code that should prevent this from happening:

if (node.name.type === AST_NODE_TYPES.JSXIdentifier) {
if (node.name.name[0].toUpperCase() === node.name.name[0]) {
// lower cased component names are always treated as "intrinsic" names, and are converted to a string,
// not a variable by JSX transforms:
// <div /> => React.createElement("div", null)
this.visit(node.name);
}

We have a passing test for this:

function div() {} // should not be referenced
<div />;

and associated snapshot:
Variable$2 {
defs: Array [
FunctionNameDefinition$1 {
name: Identifier<"div">,
node: FunctionDeclaration$1,
},
],
name: "div",
references: Array [],
isValueVariable: true,
isTypeVariable: false,
},

(there's no test for the no-unused-vars rule specifically)

I don't have a checkout handy so I can't look into it right now, but this shouldn't be possible.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Jan 31, 2021
@bradzacher
Copy link
Member

I'm unable to repro this against master.

image

@karlhorky
Copy link
Author

Very interesting! I'll have to test again...

@karlhorky
Copy link
Author

karlhorky commented Feb 2, 2021

Ok, so I tested with the full config first (it's failing here):

For this file:

const a = 1;

export default function App(div: any) {
  return <div />;
}

It only reports the a warning:

$ yarn eslint .
yarn run v1.22.10
$ /Users/k/p/typescript-eslint-no-unused-vars-jsx-div-repro-full-config/node_modules/.bin/eslint .
Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. Assuming latest React version for linting.

/Users/k/p/typescript-eslint-no-unused-vars-jsx-div-repro-full-config/src/index.tsx
  1:7  warning  'a' is assigned a value but never used  @typescript-eslint/no-unused-vars

✖ 1 problem (0 errors, 1 warning)

✨  Done in 2.22s.

https://github.com/karlhorky/typescript-eslint-no-unused-vars-jsx-div-repro-full-config

Next I'll try to create a minimal reproduction...

@karlhorky
Copy link
Author

Ok, so it appears to be an issue with react/jsx-uses-vars specified in the parent config eslint-config-react-app - I guess it's best to also disable this one!

Sorry about taking your time here!

Closing...

@karlhorky
Copy link
Author

Reported in eslint-plugin-react here: jsx-eslint/eslint-plugin-react#2916

@bradzacher
Copy link
Member

Thanks for following up! Nice catch on the repro!

Worth noting that shouldn't actually need the react/jsx-uses-vars rule any more if you're using our parser, because it fully understands JSX semantics!!
(yes - it even references React appropriately)

@bradzacher bradzacher added external This issue is with another package, not typescript-eslint itself and removed awaiting response Issues waiting for a reply from the OP or another party labels Feb 2, 2021
karlhorky added a commit to upleveled/eslint-config-upleveled that referenced this issue Feb 2, 2021
@karlhorky
Copy link
Author

Great, glad to help! And thanks for the additional info - will disable this rule permanently in our config.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants