Skip to content

Rule Proposal: vue/require-valid-default-prop #117

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

Closed
armano2 opened this issue Jul 30, 2017 · 5 comments · Fixed by #119
Closed

Rule Proposal: vue/require-valid-default-prop #117

armano2 opened this issue Jul 30, 2017 · 5 comments · Fixed by #119

Comments

@armano2
Copy link
Contributor

armano2 commented Jul 30, 2017

Please describe what the rule should do:

Enforces prop default values to be valid. In vue we have 2 ways to define default prop value:

  • by function
    {
      foo: {
        default () {}
      }
    }
  • by literal value
    {
      foo: {
        default: ''
      }
    }

Literal value is not is not valid when type is specified and set to Array or Object

This rule should also check type of literal values and determine if types match.
When variable is passed as default we should omit checking

What category of rule is this? (place an "X" next to just one item)

[ ] Enforces code style
[x] Warns about a potential error
[ ] Suggests an alternate way of doing something
[ ] Other (please specify:)

Valid:

Vue.component('example', {
  props: {
    // basic type check (`null` means accept any type)
    propA: Number,
    // multiple possible types
    propB: [String, Number],
    // a number with default value
    propD: {
      type: Number,
      default: 100
    },
    // object/array defaults should be returned from a factory function
    propE: {
      type: Object,
      default: function () {
        return { message: 'hello' }
      }
    }
  }
})

Invalid:

Vue.component('example', {
  props: {
    propA: {
      type: String,
      default: {}
    },
    propB: {
      type: String,
      default: []
    },
    propC: {
      type: Object,
      default: []
    },
    propD: {
      type: Array,
      default: []
    },
    propE: {
      type: Object,
      default: { message: 'hello' }
    }
  }
})

see more at: https://vuejs.org/v2/guide/components.html#Prop-Validation

@armano2
Copy link
Contributor Author

armano2 commented Jul 30, 2017

I don't think that name for this rule is ok, we should find out something more accurate.

@armano2
Copy link
Contributor Author

armano2 commented Jul 31, 2017

@michalsnik what do you think about this? can i start working on this?

@michalsnik
Copy link
Member

I'm thinking maybe we should add require-default-prop rule instead, like here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-default-props.md and additionally warn about wrong type of default prop in it? But that might be too much as for one rule. So I'm ok with the current rule too.

@armano2
Copy link
Contributor Author

armano2 commented Aug 1, 2017

@michalsnik we should distinct both of rules, require-default-prop should be optional, and its should require to specify default value if "required" is not set or set to false.

require-valid-default-prop should be enabled by default (in next release), its catching errors / issues.

armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Aug 2, 2017
@michalsnik michalsnik added this to the Official release milestone Aug 3, 2017
michalsnik pushed a commit that referenced this issue Aug 4, 2017
* Add rule `vue/require-valid-default-prop`.
fixes #117

* Update doc & error message & add more tests
@lcspring
Copy link

good

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

Successfully merging a pull request may close this issue.

3 participants