vuejs / eslint-plugin-vue

Official ESLint plugin for Vue.js
https://eslint.vuejs.org/
MIT License
4.47k stars 667 forks source link

`MaybeRef`s should be used with `unref` #2619

Open jhoermann opened 23 hours ago

jhoermann commented 23 hours ago

Please describe what the rule should do: Currently using a MaybeRef without using unref wouldn't lead to an eslint error:

const maybeRef: MaybeRef<boolean> = ref(false)
if (maybeRef) { // would be always true in this case but not catched with @typescript-eslint/no-unnecessary-condition
    ...
}

It should be enforced using unref in this case to prevent false positives in conditions.
Name of rule tbd.

What category should the rule belong to? [ ] Enforces code style (layout) [x] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

FloEdelmann commented 22 hours ago

This would require type information, so it would only be possible when typescript-eslint's parser is used.

Other patterns that should be reported:

(maybeRef ? x : y)
!maybeRef

maybeRef || x
maybeRef && y
maybeRef ?? x

x || maybeRef
x && maybeRef
x ?? maybeRef

maybeRef == x
maybeRef != x
maybeRef === x
maybeRef !== x

x == maybeRef
x != maybeRef
x === maybeRef
x !== maybeRef

Boolean(maybeRef)
String(maybeRef)

As a name, I'd suggest vue/require-mayberef-unwrap

Similar to