Skip to content

Add "require-default-prop" rule (fixes #122) #135

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

Merged
merged 2 commits into from
Aug 21, 2017

Conversation

michalsnik
Copy link
Member

This PR implements require-default-prop rule, that was proposed in #122.

cc @armano2

Copy link
Contributor

@armano2 armano2 left a comment

Choose a reason for hiding this comment

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

I was thinking how we should treat type: null, it means any type, than undefined is valid for this case.

function findPropsWithoutDefaultValue (propsNode) {
return propsNode.value.properties
.filter(prop => {
if (prop.value.type !== 'ObjectExpression') return true
Copy link
Contributor

Choose a reason for hiding this comment

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

In case of spread operator value is going to be undefined:
TypeError: Cannot read property 'type' of undefined

export default {
  props: {
    a: {
      ...test
    }
  }
}

*/
function propHasDefault (prop) {
const propDefaultNode = prop.value.properties
.find(p => p.key.name === 'default')
Copy link
Contributor

Choose a reason for hiding this comment

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

In case of spread operator key is going to be undefined:
TypeError: Cannot read property 'name' of undefined

export default {
  props: {
    ...test
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I totally forgot about this case.. Thanks for the heads up @armano2 :)

p.key.name === 'props'
)

if (!propsNode || propsNode.value.type !== 'ObjectExpression') return
Copy link
Contributor

Choose a reason for hiding this comment

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

you can move propsNode.value.type !== 'ObjectExpression' into find

@armano2
Copy link
Contributor

armano2 commented Aug 6, 2017

@michalsnik there is few issues when someone is going to use spread operator in props

@michalsnik michalsnik force-pushed the add-require-default-prop-rule branch from 453490c to 624e8cd Compare August 15, 2017 09:58
@michalsnik
Copy link
Member Author

@armano2 thanks for the review! Can you please check once again?

Btw. regarding:

I was thinking how we should treat type: null, it means any type, than undefined is valid for this case.

I don't check type of any property in this rule. Only whether it is required and if has the default value set.

@michalsnik michalsnik merged commit 71929fa into master Aug 21, 2017
@michalsnik michalsnik deleted the add-require-default-prop-rule branch August 21, 2017 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants