Vize

script/return-in-computed-property

要求每个计算 getter 返回值。

默认严重程度 预设 自动修复 选项
error essential, happy-path, ecosystem, nuxt, opinionated 不支持 无

适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。

错误示例

块函数体计算 getter 求值 1 + 2 却从不返回,使计算值为 undefined。

<script setup lang="ts">
import { computed } from "vue";
const total = computed(() => { 1 + 2; });
</script>

正确示例

return 1 + 2 将表达式转为 getter 的返回值。规则查找 getter 自身返回值的 return,而非仅有表达式语句。

<script setup lang="ts">
import { computed } from "vue";
const total = computed(() => { return 1 + 2; });
</script>

配置(Vite+)

import { defineConfig } from "@vizejs/vite-plugin/vite-plus";

export default defineConfig({}).withVue({
  linter: {
    "preset": "incremental",
    "rules": {
      "script/return-in-computed-property": "error"
    }
  },
});

然后运行 vp run lint。

相关页面