Bug: `no-unused-vars` thinks a lowercase name is used in JSX element tag
See original GitHub issueEnvironment
Node version: v14.17.5 npm version: v6.14.14 Local ESLint version: Not found Global ESLint version: Not found Operating System: darwin 20.6.0
What parser are you using?
What did you do?
const unused = () => {};
function Example() {
return <unused />;
}
What did you expect to happen?
According to the JSX spec, the output createElement
call disambiguates between component references (JS) and string tag names (DOM) depending on the first letter of a name being capitalized:
// input
<Capitalized />
<lowercase />
// output
createElement(Capitalized);
createElement("lowercase");
Thus, a lowercase identifier should not be considered used when it’s only part of a JSX tag.
The previous example (“what did you do?”) should report a no-unused-vars
error.
What actually happened?
The example does not report a no-unused-vars
error.
Participation
- I am willing to submit a pull request for this issue.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Lower case component names (use) are no longer supported ...
I am using a svg element in React JSX, thinking that <use> is valid in JSX. Is the following error correct ? Lower...
Read more >[no-unused-vars] does not count JSX as usage #111 - GitHub
I'll close this issue as the bug itself is working as intended. Feel free to open a PR to add to the docs...
Read more >JSX In Depth - React
Capitalized types indicate that the JSX tag is referring to a React component. These tags get compiled into a direct reference to the...
Read more >How to troubleshoot React errors - Blog - Pusher
React throws up an error with a message that says “Adjacent JSX elements must be wrapped in an enclosing tag“. This is because...
Read more >Assigned a Value but Never Used | React JS Debugging
In react component I want to display a name but I want to read it from the app state. This name will not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks!
As you’ve already noticed (and prepared a PR in eslint-plugin-react), this is an issue with the
react/jsx-uses-vars
rule.no-unused-vars
itself does not treat JSX identifiers as references because that’s React-specific JSX semantics, so this would be, by default, a reported error on thelowercase
variable, which is observable when you disablereact/jsx-uses-vars
rule.However,
no-unused-vars
has an additional feature - other rules can mark variables as used, andno-unused-vars
will not report those variables. In this case,react/jsx-uses-vars
marks thelowercase
variable as used.I’m closing this issue since this isn’t a problem in the core rule.
I encountered this on a larger repo and was initially unaware that this was triggered by the
react
plugin.Thank you for your help in finding the culprit!