Eslint-plugin-react: Rule proposal: Disallow unused JSX expressions

Created on 21 Mar 2016  路  12Comments  路  Source: yannickcr/eslint-plugin-react

There's probably a better name for this, I just couldn't think of it :stuck_out_tongue:

These are valid:

function foo() {
  return (
    <div>Hello world!</div>
  );
}
var x = <div>Hello world!</div>;

These are invalid:

function foo() {
  <div>Hello world!</div>;
}
if (something) {
  <div>Hello world!</div>;
}

While doing a code review, I encountered some code that missed a return statement and just had the JSX sitting there. No lint error :(

help wanted new rule

All 12 comments

+1, this should parallel http://eslint.org/docs/rules/no-unused-expressions and probably be called no-unused-jsx-expressions

Yeah, that name is much nicer :+1:

Looks like this was implemented as require-render-return, at least for the simple cases.

I still think it would be useful on its own.

I hit something similar again today:

      {sections.map(section => {
        <AdsHelpTrayContentList
          contentList={section.items}
          key={section.title}
          title={section.title}
          onItemSelected={props.onItemSelected}
        />
      })}

Can you see the problem?

...

I used { } instead of ( ) in the arrow function for the map call, so the function wasn't actually returning anything. 馃槧

Since eslint core rule no-unused-expressions has covered jsx expressions now, is this proposal still relevant?

@golopot when was that added?

@ljharb demo .

I see that it works, but since https://github.com/eslint/eslint/pull/2030 eslint is explicitly supposed to not care about variable usage inside jsx, so I鈥檓 wondering when it started checking unused jsx expressions.

I think that the commit you referred to only affects no-unused. The implementation of no-unused-expressions does not rely on .eslintUsed property.

ok, so the implication is that this capability has always existed in eslint and this rule wasn鈥檛 needed.

Closing, but will reopen if @Daniel15 confirms there鈥檚 still a gap.

Thanks! I'll look into no-unused-expressions. Look like at Facebook we had originally disabled that rule because it showed a warning for foo && foo.bar(), but it looks like the allowShortCircuit config option can be set to allow that. I'll try it out.

Was this page helpful?
0 / 5 - 0 ratings