eslint-plugin-vue icon indicating copy to clipboard operation
eslint-plugin-vue copied to clipboard

Prevent calling composables directly in the event handler

Open Haberkamp opened this issue 8 months ago • 0 comments

Please describe what the rule should do:

This rule enforces that you can't directly call a composable inside an event handler.

Calling a composable inside an event handler directly can cause issues. For example when we call onMounted inside that composable, we'll get an error because onMounted is called outside the Vue context.

Here's an example

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:

<template>
  <button @click="useFoo">Click me!</button>
</template>

<script setup>
function useFoo() {
  onMounted() {}

  fetch('/api);
}
</script>

Additional context

To determine wether something is a composable, we check if a function starts with use. This is the standard convention for Vue.

Note: I would help to create a PR for that rule

Haberkamp avatar Mar 22 '25 18:03 Haberkamp