vetur icon indicating copy to clipboard operation
vetur copied to clipboard

Prop validation treats optional props as required

Open last-partizan opened this issue 2 years ago • 9 comments

  • [x] I have searched through existing issues
  • [x] I have read through docs
  • [x] I have read FAQ
  • [x] I have tried restarting VS Code or running Vetur: Restart VLS

Info

  • Platform: Linux
  • Vetur version: Latest
  • VS Code version: N/A (i'm using neovim, coc and coc-vetur)

Problem

When using following prop definition propName: String, vetur considers it required. But for vue it's optional.

image

https://github.com/vuejs/vetur/blob/master/server/src/modes/script/componentInfo.ts#L371 I think problem is here. And i can try to fix it myself and make PR, if this is really a bug and not desired behaviour. For me it looks like a bug.

Reproducible Case

https://github.com/last-partizan/vetur-props-validation-bug

This is Test.vue

<template>
  <div>
    {{ foo }}
  </div>
</template>

<script>
export default {
  props: {
    foo: String,
    bar: {type: String, required: false},
  }
}
</script>

This is App.vue

<template>
  <Test />
</template>

<script>
import Test from './Test.vue'

export default {
  components: { Test },
}
</script>

This is vetur warning: <Test> misses props: foo

last-partizan avatar Aug 04 '21 07:08 last-partizan

Same here:

export default defineComponent({
 props: {
    noCloseOnBackdrop: {
      type: Boolean,
      required: false
    }
  }
})

image

It looks like this had to do with noCloseOnBackdrop is in camelCase and no-close-on-backdorp is used...

yooouuri avatar Aug 06 '21 08:08 yooouuri

@yooouuri that's strange.

my minimal example

export default {
  props: {
    foo: String,
    bar: {type: String, required: false},
    camelCaseBar: {type: String, required: false},
  }
}

triggers warning only for foo prop. Could you check my repo with Reproducible Case?

last-partizan avatar Aug 06 '21 09:08 last-partizan

https://vuejs.github.io/vetur/guide/interpolation.html#prop-validation

https://github.com/vuejs/vetur/issues/2141#issuecomment-674117099

It is by design.

yoyo930021 avatar Aug 06 '21 11:08 yoyo930021

Hmm, why such design decision?

For me it looks counter-intuitive. Props are optional, but vetur requires me to pass them.

I understand, it's better to explicitly set required: true or false, but that's job for eslint. It already has vue/require-prop-types for catching props: ["foo"]. And, if implemented this way - will show warnings only in relevant place (component itself, not everywhere where it's used).

last-partizan avatar Aug 06 '21 14:08 last-partizan

Please set "vetur.validation.templateProps": false when you don't need it.

Another information: https://github.com/vuejs/vetur/issues/2235#issuecomment-688013007

yoyo930021 avatar Aug 06 '21 14:08 yoyo930021

Oh, i see people having the same problem as me.

And i see

But I think this case might be taken as required: false.
I will talk to @octref.

But looks like nothing was changed?

I think this feature would be useful to more people, when it works as expected. And expected way - is to treat optional props as optional.

I would like to use it, but in current state it gives more trouble that benefits.

I know it's opensource project, and i don't want to bother you with my wishes. I'm offering help, i could implement fixes needed (maybe with some guiding).

But we need come to conclusion about how this should work, so everyone could benefit from it.

https://github.com/vuejs/vetur/issues/2235#issuecomment-818990826

Here is good point, i totally agree with it, but if we need to preserve current behaviuor, i could offer following chages:

Create new option: "vetur.validation.templatePropsValidationLevel": "standard" | "strict".

  • standard validation behaves as everyone expects - errors when required props are missing
  • strict - same as above, + warning about missing optional (but without explicit required field). Current behaviour.

@yoyo930021 what do you think?

Maybe @octref has something to say about this?

last-partizan avatar Aug 06 '21 15:08 last-partizan

Oh, i see people having the same problem as me.

And i see

But I think this case might be taken as required: false. I will talk to @octref.

But looks like nothing was changed?

I think this feature would be useful to more people, when it works as expected. And expected way - is to treat optional props as optional.

I would like to use it, but in current state it gives more trouble that benefits.

I know it's opensource project, and i don't want to bother you with my wishes. I'm offering help, i could implement fixes needed (maybe with some guiding).

But we need come to conclusion about how this should work, so everyone could benefit from it.

#2235 (comment)

Here is good point, i totally agree with it, but if we need to preserve current behaviuor, i could offer following chages:

Create new option: "vetur.validation.templatePropsValidationLevel": "standard" | "strict".

* `standard` validation behaves as everyone expects - errors when required props are missing

* `strict` - same as above, + warning about missing optional (but without explicit `required` field). Current behaviour.

@yoyo930021 what do you think?

Maybe @octref has something to say about this?

I respect @octref This feature is design by him.

yoyo930021 avatar Aug 12 '21 08:08 yoyo930021

Any update on this?

vis97c avatar Nov 25 '21 19:11 vis97c

I'm popping here to tell you that IMHO is a wrong design decision.

  • Vue consider prop optional by defult.
  • Vetur doesn't

Regards,

☮😀

marcomilone avatar Feb 23 '22 13:02 marcomilone