Skip to content

bug: false negatives/positives (rare) - analyzes derived function's args, not the eventual setter's #38

@NickvanDyke

Description

@NickvanDyke

Did you check docs and existing issues?

  • I have read the plugin docs
  • I have searched the existing issues
  • I have read You Might Not Need An Effect and am reasonably confident that I do need an effect

Plugin version

0.5.3

ESLint version

9.0.0

Describe the bug

When linting code with a derived setter (a function that internally calls setState), the plugin analyzes the args passed to the derived function, not the eventual setter call. This causes false negatives when the derived setter internally passes internal args to the setter, and false positives when the derived setter receives internal args but does not pass them to the setter.

Steps To Reproduce

Lint the following code for a false negative:

        function Form() {
          const [firstName, setFirstName] = useState('Dwayne');
          const [lastName, setLastName] = useState('The Rock');
          const [fullName, setFullName] = useState('');

          useEffect(() => {
            const doSet = () => {
              setFullName(firstName + ' ' + lastName);
            }

            doSet();
          }, [firstName, lastName]);
        }

Lint the following code for a false positive (contrived):

        function Form() {
          const [firstName, setFirstName] = useState('Dwayne');
          const [lastName, setLastName] = useState('The Rock');
          const [fullName, setFullName] = useState('');

          useEffect(() => {
            const doSet = (arg1, arg2) => {
              console.log(arg1, arg2);
            }

            doSet(firstName, lastName);
          }, [firstName, lastName]);
        }

Expected Behavior

The first example should flag no-derived-state.

The second example should not flag no-derived-state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions