eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

`jsx-no-leaked-render` performs redundant props check and invalid fix in coerce mode

Open wialy opened this issue 3 years ago • 2 comments

Context

There is a sample Checkbox component:

const Checkbox = ({
  isChecked = false,
  isIndeterminate = false,
}: {
  isChecked?: boolean;
  isIndeterminate?: boolean;
}) => (
  <input 
    checked={isIndeterminate ? false : isChecked} 
    type="checkbox"
  />
);

The eslint config file contains a rule:

"react/jsx-no-leaked-render": ["error", { validStrategies: ["coerce"] }],

Expected behavior

Linting the code raises no issues

Actual behavior

  1. Eslint displays an error for the line:
checked={isIndeterminate ? false : isChecked}
Potential leaked value that might cause unintentionally rendered values or rendering crasheseslint[react/jsx-no-leaked-render](https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules/jsx-no-leaked-render.md)
  1. When auto-fix is performed, the code above is replaced with
checked={!!isIndeterminate && false} 

which causes the logical issues (the isChecked is removed in that case).

wialy avatar Sep 11 '22 11:09 wialy

isChecked could be undefined, according to the types, and default arguments aren’t something that can really be used reliably with static analysis - so i think the warning may be correct.

I agree, though, that the autofix is incorrect - it should be fixing it to !isIndeterminate && isChecked

ljharb avatar Sep 11 '22 15:09 ljharb

cc @Belco90

ljharb avatar Sep 11 '22 15:09 ljharb

hey, would like to work on this issue

himanshu007-creator avatar Nov 01 '22 14:11 himanshu007-creator

Go for it!

ljharb avatar Nov 01 '22 15:11 ljharb

i was not able to reproduce the issue

himanshu007-creator avatar Nov 01 '22 15:11 himanshu007-creator

In that case, can you make a PR with passing test cases, and it can close this issue?

ljharb avatar Nov 01 '22 15:11 ljharb

I had an identical situation to the OP, and this error still occurred. @himanshu007-creator, did you remember to change the allowed strategies to only coerce?

tcl333 avatar Nov 29 '22 00:11 tcl333