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

`define-macros-order`: should allow referencing local variables defined before the macros statement

Open andreww2012 opened this issue 6 months ago • 3 comments

What did you do?

define-macros-order should probably allow referencing local variables defined before the macros:

<script setup lang="ts">
/* eslint vue/define-macros-order: [2, {order: ["defineOptions", "defineModel", "defineProps", "defineEmits", "defineSlots"]}] no-use-before-define: 2 */
import {ref} from 'vue';

const lastSetModelValue = ref<string>();
// "defineModel should be the first statement in `<script setup>` (after any potential import statements or type definitions)",
// but it's not safe to move `lastSetModelValue` after the macros
const [modelValue, modelModifiers] = defineModel<string>({
  set: (value: string) => {
    lastSetModelValue.value = value;
    return value;
  },
  get: (value: string) => {
    return lastSetModelValue.value ?? value;
  },
});
</script>

What did you expect to happen?

The rule should not report .

If lastSetModelValue definition is moved below defineModel, no-use-before-define will be triggered as well as define-macros-order on the following macros definitions.

Repository to reproduce this issue

Reproduction

andreww2012 avatar May 17 '25 08:05 andreww2012

but it's not safe to move lastSetModelValue after the macros

Why is it not safe? Could you provide a concrete example where we can see that it is not safe?

ota-meshi avatar May 17 '25 12:05 ota-meshi

I think my wording wasn't fully correct - it only might be unsafe because you're required to ensure any potential accesses to modelValue performed before the initialization of lastSetModelValue, which makes your code more fragile, and you also get reports from no-use-before-define as well as lose the ability to put the related code together.

Vue playground example

andreww2012 avatar May 18 '25 07:05 andreww2012

So it's technically safe, right? If so, I think it's a stylistic issue, and it would be good to add an option to handle it. Please open a PR if you're interested.

ota-meshi avatar May 18 '25 07:05 ota-meshi