vue/max-template-complexity
コンポーネント自身のテンプレートの複雑度を制限します。
既定の重大度: warning
プリセット: none
自動修正: なし。修正内容を確認してください
適用範囲: Vue SFC のテンプレート・ブロック。必要な script の文脈も例に含めています。
オプション: ルール固有のオプションはありません。重大度とプリセットは設定できます。
悪い例は cyclomatic complexity 13、cognitive complexity 25 で、閾値 11 と 16 を超えます。コンポーネント単位で計測し、対象はインライン HTML テンプレートです。
例の二つの値の計算内訳は複雑度の計算とコンポーネントの境界を参照してください。
設定(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"vue/max-template-complexity": "warn"
}
},
},
});
vp run lint
悪い
親が記述する分岐、ループ、slot の内容、式の条件で 13 と 25 になり、既定の閾値 11 と 16 を超えています。
<script setup lang="ts">
defineProps<{ rows: Row[] }>();
</script>
<template>
<section>
<h1>{{ user ? user.name : 'Guest' }}</h1>
<DataTable :rows="rows">
<template #cell="{ row, column }">
<span v-if="column.key === 'status'" :class="row.active ? 'on' : 'off'">{{ row.status ?? 'unknown' }}</span>
<a v-else-if="column.key === 'link' && row.url" :href="row.url">{{ row.label }}</a>
<template v-else>
<em v-for="tag in row.tags" :key="tag">
<b v-if="tag.pinned || tag.starred">{{ tag.hot ? '!' : '' }}</b>
</em>
</template>
</template>
</DataTable>
<p v-if="!rows.length && !loading">No data</p>
</section>
</template>
良い
描画を RowList に分け、親には v-if を一つ残します。親自身の値は 2 と 1 になります。
<template>
<RowList v-if="ready" :rows="rows" />
</template>
良い例は上記の設定でこのルールの検出を避ける例です。他のルールでは検出される場合があります。