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

Rule Proposal: vue/no-ref-object-destructure rule #1949

Closed
ota-meshi opened this issue Aug 17, 2022 · 1 comment · Fixed by #1965
Closed

Rule Proposal: vue/no-ref-object-destructure rule #1949

ota-meshi opened this issue Aug 17, 2022 · 1 comment · Fixed by #1965

Comments

@ota-meshi
Copy link
Member

Please describe what the rule should do:

A new rule reports usage of ref objects that can lead to loss of reactivity.

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:)

Provide 2-3 code examples that this rule should warn about:

// Ref Object
const count = ref(0)
const cnt1 = count.value /* ✗ BAD */
const { value: cnt2 } = count /* ✗ BAD */
const cnt3 = computed(() => count.value /* ✓ GOOD */)
const value1 = fn(count.value) /* ✗ BAD */
const value2 = fn(count) /* ✓ GOOD */
const value3 = computed(() => fn(count.value) /* ✓ GOOD */)
// Reactivity Transform
const count = $ref(0)
const cnt1 = count /* ✗ BAD */
const cnt2 = computed(() => count /* ✓ GOOD */)
const value1 = fn(count) /* ✗ BAD */
const value2 = fn($$(count)) /* ✓ GOOD */
const value3 = computed(() => fn(count) /* ✓ GOOD */)

Additional context

I think it's useful for users who use Reactivity Transform because it prevents them from forgetting to add $$().
So it is related to Reactivity Transform #1948.

Regarding Reactive Objects, the following code loses its reactivity:

const state = reactive({ count: 0 })
const cnt1 = state.count
const { count: cnt2 } = state
const value1 = fn(state.count)

But when count is an object, it may not lose reactivity, so it's hard to report:

const state = reactive({ count: { data: 0 } })
const cnt1 = state.count
const { count: cnt2 } = state
const value1 = fn(state.count)
@ota-meshi ota-meshi changed the title Add vue/no-ref-object-destructure rule Rule Proposal: vue/no-ref-object-destructure rule Aug 17, 2022
@FloEdelmann
Copy link
Member

Sort of related to #1933.

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.

2 participants