Vize

script/no-async-in-computed

computed の getter で非同期関数を使う箇所を検出します。

既定の重大度 プリセット 自動修正 オプション
error essential, happy-path, ecosystem, nuxt, opinionated なし なし

適用範囲:Vue SFC の JavaScript / TypeScript の script ブロック。対象となる Options API または script setup の形式は例を参照してください。

悪い例

computed の getter が async であり、fetch の結果を同期的な計算値ではなく Promise として返します。

<script setup lang="ts">
import { computed } from "vue";
const data = computed(async () => {
  const response = await fetch("/api/data");
  return response.json();
});
</script>

良い例

非同期の fetch を watch に移し、結果を data.value に保存します。クリーンアップで古い要求を中止し、無効になったコールバックが古い結果を書き込むのを防ぎます。async な computed getter は残りません。

<script setup lang="ts">
import { ref, watch } from "vue";
const query = ref("");
const data = ref<unknown>(null);
watch(query, async (value, _oldValue, onCleanup) => {
  const controller = new AbortController();
  let active = true;
  onCleanup(() => { active = false; controller.abort(); });
  const response = await fetch(`/api/data?q=${encodeURIComponent(value)}`, { signal: controller.signal });
  const next: unknown = await response.json();
  if (active) data.value = next;
});
</script>

設定(Vite+)

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

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

設定したら vp run lint を実行します。

関連ページ