Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to require-default-props to forbid default props for required props #1524

Closed
sharmilajesupaul opened this issue Nov 9, 2017 · 3 comments

Comments

@sharmilajesupaul
Copy link
Contributor

sharmilajesupaul commented Nov 9, 2017

The require-default-props rule allows required props to also have a default prop. It would be nice to add an option to warn against this behavior.

Option:
forbidDefaultForRequired (boolean) - Setting this to true will forbid required props from also having a default prop declaration. Default false.

schema: [{
 type: 'object',
      properties: {
        forbidDefaultForRequired: {
          type: 'boolean'
        }
      },
      additionalProperties: false,
}]

The following patterns are considered warnings:

function MyStatelessComponent({ foo, bar }) {
  return <div>{foo}{bar}</div>;
}

MyStatelessComponent.propTypes = {
  foo: PropTypes.string.isRequired,
};

MyStatelessComponent.defaultProps = {
  foo: 'something',
};
class Foo extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.foo}</h1>
    );
  }

  static propTypes = {
    foo: PropTypes.string,
  };

  static defaultProps = {
    foo: "foo",
  };
}

The following patterns are not considered warnings:

function MyStatelessComponent({ foo, bar }) {
  return <div>{foo}{bar}</div>;
}

MyStatelessComponent.propTypes = {
  foo: PropTypes.string,
};

MyStatelessComponent.defaultProps = {
  foo: 'something',
};
class Foo extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.foo}</h1>
    );
  }

  static propTypes = {
    foo: PropTypes.string,
  };

  static defaultProps = {
    foo: "foo",
  };
}
@sharmilajesupaul sharmilajesupaul changed the title Add option to require-default-props forbid default props for required props Add option to require-default-props to forbid default props for required props Nov 9, 2017
@ljharb
Copy link
Member

ljharb commented Nov 9, 2017

Sound great to me - as long as it does a "safe" thing when it encounters things that aren't statically analyzeable, like imported shapes, spread props, etc. It'd also be super fancy if spread props worked when the object literal it referenced was defined in the same file.

@sharmilajesupaul
Copy link
Contributor Author

+1 on warning on spread prop violations when the referenced object literal in the same file

Is the "safe" thing generally not warning on propTypes that aren't explicitly declared in the same file?

@ljharb
Copy link
Member

ljharb commented Nov 9, 2017

Yes, exactly - basically, what's safe is not creating false positives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants