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 new rule 'default-props-match-prop-types' #1087

Merged
merged 8 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions docs/rules/default-props-match-prop-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Enforce all defaultProps have a corresponding non-required PropType (default-props-match-prop-types)

This rule aims to ensure that any `defaultProp` has a non-required `PropType` declaration.

Having `defaultProps` for non-existent `propTypes` is likely the result of errors in refactoring
or a sign of a missing `propType`. Having a `defaultProp` for a required property similarly
indicates a possible refactoring problem.

## Rule Details

The following patterns are considered warnings:

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

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

MyStatelessComponent.defaultProps = {
foo: "foo"
};
```

```jsx
var Greeting = React.createClass({
render: function() {
return <div>Hello {this.props.foo} {this.props.bar}</div>;
},

propTypes: {
foo: React.PropTypes.string,
bar: React.PropTypes.string
},

getDefaultProps: function() {
return {
baz: "baz"
};
}
});
```

```jsx
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.foo} {this.props.bar}</h1>
);
}
}

Greeting.propTypes = {
foo: React.PropTypes.string.isRequired,
bar: React.PropTypes.string
};

Greeting.defaultProps = {
foo: "foo"
};
```

```jsx
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.foo} {this.props.bar}</h1>
);
}

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

static defaultProps = {
baz: "baz"
};
}
```

```jsx
type Props = {
foo: string,
bar?: string
};

function MyStatelessComponent(props: Props) {
return <div>Hello {props.foo} {props.bar}</div>;
}

MyStatelessComponent.defaultProps = {
foo: "foo",
bar: "bar"
}
```

The following patterns are not considered warnings:

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

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

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

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

MyStatelessComponent.defaultProps = {
bar: 'some default'
};
```

```jsx
type Props = {
foo: string,
bar?: string
};

function MyStatelessComponent(props: Props) {
return <div>Hello {props.foo} {props.bar}</div>;
}

MyStatelessComponent.defaultProps = {
bar: 'some default'
};
```

```js
function NotAComponent({ foo, bar }) {}

NotAComponent.propTypes = {
foo: React.PropTypes.string,
bar: React.PropTypes.string.isRequired
};
```

## Rule Options

```js
...
"default-props-match-prop-types": [<enabled>, { "allowRequiredDefaults": <boolean> }]
...
```

### `allowRequiredDefaults`

When `true` the rule will ignore `defaultProps` for `isRequired` `propTypes`.

The following patterns are considered okay and do not cause warnings:

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

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

MyStatelessComponent.defaultProps = {
foo: "foo"
};
```

## When Not To Use It

If you don't care about stray `defaultsProps` in your components, you can disable this rule.

## Related rules

- [require-default-props](./require-default-props.md)

# Resources
- [Official React documentation on defaultProps](https://facebook.github.io/react/docs/typechecking-with-proptypes.html#default-prop-values)

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var allRules = {
'jsx-no-bind': require('./lib/rules/jsx-no-bind'),
'jsx-no-undef': require('./lib/rules/jsx-no-undef'),
'no-unknown-property': require('./lib/rules/no-unknown-property'),
'default-props-match-prop-types': require('./lib/rules/default-props-match-prop-types'),
'jsx-curly-spacing': require('./lib/rules/jsx-curly-spacing'),
'jsx-equals-spacing': require('./lib/rules/jsx-equals-spacing'),
'jsx-sort-props': require('./lib/rules/jsx-sort-props'),
Expand Down
Loading