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

prop-types improved handling for destructuring and member expressions

Open cbranch101 opened this issue 7 years ago • 10 comments

This adds handling for multiple previously unhandled situations related to destructuring and member expressions. It required that I pretty heavily rework the existing code related to marking prop types usages, but I think it actually simplifies it a bit from what was there before.

Multi-staged destructuring

        const Foo = ({ a: { b } }) => {
          const { c } = b
          return <div>{c}</div>
        }
// reports `'a.b.c' is missing in props validation`

Destructuring off of member expressions

        const Foo = props => {
          const { b } = props.a
          return <div>{b}</div>
        }
// reports `'a.b' is missing in props validation`

Renamed destructuring

        const Foo = ({ a: aRenamed }) => {
          const { b } = aRenamed
          return <div>{b}</div>
        }
// reports `'a.b' is missing in props validation`

Member expressions of off destructured variables

        const Foo = ({ a: { b } }) => {
          return <div>{c.d}</div>
        }
// reports `'a.b.c.d' is missing in props validation`

Everything should be working as it was before with one exception.

This test used to be reporting an error on names.map not being defined in prop type, but I'm now ignoring all array methods when checking prop types off a member expression.

{
      code: [
        'const Hello = (props) => {',
        '  let team = props.names.map((name) => {',
        '      return <li>{name}, {props.company}</li>;',
        '    });',
        '  return <ul>{team}</ul>;',
        '};'
      ].join('\n'),
      parser: 'babel-eslint',
      errors: [
        {message: '\'names\' is missing in props validation'},
        {message: '\'company\' is missing in props validation'}
      ]
    }

Fixes #1422 Fixes #1447

cbranch101 avatar Dec 13 '17 23:12 cbranch101

but I'm now ignoring all array methods when checking prop types off a member expression

@cbranch101 out of curiosity, why?

MatthewHerbst avatar Dec 20 '17 04:12 MatthewHerbst

@MatthewHerbst you can't know statically if .map is on an array or not.

ljharb avatar Dec 20 '17 05:12 ljharb

This rule would really help me out! Looks like the outstanding question is to @ljharb, and a conflict to be resolved.

brokentone avatar Jun 30 '18 14:06 brokentone

@cbranch101 this needs a rebase; I tried doing it but it wasn't a clean one.

ljharb avatar Jul 06 '18 04:07 ljharb

Any progress with this issue? Is there anything that I can do to further this along?

davidlewallen avatar Aug 15 '18 00:08 davidlewallen

@davidlewallen if you could make a branch that has these commits, rebased and passing tests, and you post a link to it here (without creating a duplicate PR) then I'd be happy to update this PR with your rebase/additions :-)

ljharb avatar Aug 15 '18 05:08 ljharb

I have a PR upcoming that deduplicates propType usage detection from prop-types and no-unused-prop-types https://github.com/alexzherdev/eslint-plugin-react/commit/348021f4fbfae9c39de6ed7f77ef6395d7916e6f (bringing them both down to < 200 lines compared to 1100+ lines originally 🤗). If that were landed first, then both rules would benefit from this improvement.

alexzherdev avatar Aug 15 '18 07:08 alexzherdev

Hey guys, sorry for the delay, got pulled off into some other things, I'll take a look at the rebase this week

cbranch101 avatar Aug 15 '18 16:08 cbranch101

ping @cbranch101, are you still interested in completing this PR?

ljharb avatar Dec 13 '19 00:12 ljharb

@cbranch101 unfortunately it seems like @alexzherdev's propType detection refactors makes this PR not trivially rebaseable. I was able to manually preserve the test cases, but I think you'll need to be the one to look at how to rebase the implementation changes.

I'll mark this as a draft in the meantime; please unmark it when it's rebased and ready to go.

ljharb avatar Oct 15 '20 18:10 ljharb