question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: `no-unused-vars` thinks a lowercase name is used in JSX element tag

See original GitHub issue

Environment

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?

@babel/eslint-parser

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Sep 8, 2021

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 the lowercase variable, which is observable when you disable react/jsx-uses-vars rule.

However, no-unused-vars has an additional feature - other rules can mark variables as used, and no-unused-vars will not report those variables. In this case, react/jsx-uses-vars marks the lowercase variable as used.

I’m closing this issue since this isn’t a problem in the core rule.

1reaction
alanorozcocommented, Sep 8, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found