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 forbidDefaultForRequired option #1532

Merged
merged 1 commit into from
Nov 15, 2017
Merged

Conversation

jomasti
Copy link
Contributor

@jomasti jomasti commented Nov 11, 2017

New option for require-default-props rule that forbids defining default
props for props that are required.

This doesn't handle spread properties since the work needed should also work for the default state of the rule. If that were to happen, it could cause reports for previously valid code, and that seems more suitable for a major version bump. Therefore, I think that should be in a separate PR.

Closes #1524

New option for require-default-props rule that forbids defining default
props for props that are required.
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall

...
```

* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basic eslint documentation; individual rule docs don't tend to explain this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just modified this block from another rule. Should I remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker then, altho i don’t think any rules should have this.

@ljharb
Copy link
Member

ljharb commented Nov 13, 2017

cc @sharmilajesupaul; a review would be great :-)

Copy link
Contributor

@sharmilajesupaul sharmilajesupaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just have a question about how this will behave with object spreads, otherwise lgtm pending @ljharb's comments

].join('\n'),
options: [{forbidDefaultForRequired: true}],
errors: [{
message: 'propType "foo" is required and should not have a defaultProp declaration.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this also warn on the case where a spread property has an object literal referenced in the same file?
eg:

const defaults = {
  foo: ''
}

class Hello extends React.Component {
  static propTypes = {
    foo: PropTypes.string.isRequired
  }

  static defaultProps = { ...defaults } // <---

  render () {
    return <div>Hello {this.props.foo}</div>;
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm read your pr discription

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jomasti, could you file an issue for the followup work, of supporting object literals in the same file spread into defaultProps/propTypes?

@jomasti
Copy link
Contributor Author

jomasti commented Nov 14, 2017

I created #1535 for tracking the spread property enhancement.

@ljharb
Copy link
Member

ljharb commented Nov 14, 2017

Will merge this tomorrow if no objections.

@ljharb ljharb merged commit 27b8279 into jsx-eslint:master Nov 15, 2017
@jomasti jomasti deleted the issue-1524 branch November 15, 2017 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants