Skip to content

useVueDefineMacrosOrder

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"useVueDefineMacrosOrder": "error"
}
}
}
}

¥Description

强制要求 Vue 编译器宏的特定顺序。

¥Enforce specific order of Vue compiler macros.

此规则确保 Vue 的组合 API 编译器宏在 <script setup> 代码块中的顺序一致。强制保持一致的顺序,通过建立可预测的结构,提高代码的可读性和可维护性。

¥This rule ensures consistent ordering of Vue’s Composition API compiler macros in <script setup> blocks. Enforcing a consistent order improves code readability and maintainability by establishing a predictable structure.

这些宏必须出现在所有其他语句之前(导入语句、类型声明和调试语句除外)。

¥These macros must also appear before any other statements (except imports, type declarations, and debugger statements).

¥Examples

¥Invalid

<script lang="ts" setup>
const emit = defineEmits(['update'])
const props = defineProps<{ name: string }>()
</script>
code-block.vue:2:7 lint/nursery/useVueDefineMacrosOrder  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

defineProps macro is out of order.

1 │ const emit = defineEmits([‘update’])
> 2 │ const props = defineProps<{ name: string }>()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

Macros defined in <script setup> should be ordered as follows: defineModel, defineProps, defineEmits

and be placed before any non-macro statements, except for type declarations, imports, exports or debugger statements.

Unsafe fix: Reorder macro defineProps.

1 - const·emit·=·defineEmits([update])
2 - const·props·=·defineProps<{·name:·string·}>()
1+ const·props·=·defineProps<{·name:·string·}>()
2+ const·emit·=·defineEmits([update])
3 3

<script lang="ts" setup>
import { ref } from 'vue'
const count = ref(0)
const props = defineProps<{ name: string }>()
</script>
code-block.vue:4:7 lint/nursery/useVueDefineMacrosOrder  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

defineProps macro is out of order.

3 │ const count = ref(0)
> 4 │ const props = defineProps<{ name: string }>()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │

Macros defined in <script setup> should be ordered as follows: defineModel, defineProps, defineEmits

and be placed before any non-macro statements, except for type declarations, imports, exports or debugger statements.

Unsafe fix: Reorder macro defineProps.

1 1 import { ref } from ‘vue’
2 -
3 - const·count·=·ref(0)
4 - const·props·=·defineProps<{·name:·string·}>()
2+ const·props·=·defineProps<{·name:·string·}>()
3+
4+
5+ const·count·=·ref(0)
5 6

¥Valid

<script lang="ts" setup>
const props = defineProps<{ name: string }>()
const emit = defineEmits(['update'])
</script>
<script lang="ts" setup>
import { ref } from 'vue'
const model = defineModel()
const props = defineProps<{ name: string }>()
const emit = defineEmits(['update'])
const count = ref(0)
</script>
<script lang="ts" setup>
import { ref } from 'vue'
interface Props {
value: string
}
const props = defineProps<Props>()
const emit = defineEmits(['update'])
</script>

¥Options

允许指定 Vue 编译器宏的出现顺序。

¥Allows specifying the order in which the Vue compiler macros should appear.

默认:["defineModel", "defineProps", "defineEmits"]

¥Default: ["defineModel", "defineProps", "defineEmits"]

这不仅限于内置宏,例如 unplug-vue-router definePage 也可以列在此处。

¥This is not limited to built-in macros, for example unplug-vue-router definePage can be listed here as well.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useVueDefineMacrosOrder": {
"options": {
"order": [
"definePage",
"defineProps",
"defineEmits",
"defineModel"
]
}
}
}
}
}
}

¥Related links