English
vue-perfectionist/no-complex-template-expression
Limit logical operators and conditional nesting in Vue templates.
At a glance
| Concern | Behavior |
|---|---|
| Scope | Template |
| Defaults | At most two logical operators and one conditional nesting level. |
| Required options | None; enabling the rule uses its defaults. |
| Automatic fixes | — |
| Type information | No TypeScript type services required. |
Usage
Configure vue-eslint-parser; see Getting Started.
js
import vuePerfectionist from 'eslint-plugin-vue-perfectionist'
export default [
{
files: ['**/*.vue'],
plugins: { 'vue-perfectionist': vuePerfectionist },
rules: { 'vue-perfectionist/no-complex-template-expression': 'error' },
},
]📖 Rule Details
Keep conditions and bindings readable by extracting branching into computed properties or named functions. Simple translation and formatting calls are fine.
vue
<template>
<button
:disabled="isDisabled"
type="button"
>
{{ t('submit') }}
</button>
<span>{{ ready ? label : fallback }}</span>
<div v-if="name && source && category" />
</template>correct
vue
<template>
<button
:disabled="!name.trim() || (!source && !category) || uncertain"
type="button"
/>
<span>{{ pending ? 'loading' : failed ? 'error' : 'done' }}</span>
</template>incorrect
🔧 Options
json
{
"maxLogicalExpressions": 2,
"maxConditionalDepth": 1,
"directives": ["if", "else-if", "show", "bind"],
"checkInterpolations": true,
"ignoreScopeReferences": false
}maxLogicalExpressions(nonnegative integer, default2): total number of&&,||, and??operators in each expression.a && b && chas two. Counts across call arguments, arrays, objects, computed keys, and inline callback bodies.0prohibits all logical operators.maxConditionalDepth(nonnegative integer, default1): deepest nesting of ternary expressions. A single ternary has depth one. Nested tests and branches count equally; sibling ternaries do not add depth.0prohibits ternaries.directives(array, default["if", "else-if", "show", "bind"]): replaces checked directive names, without thev-prefix or argument. Also acceptstext,html,model, andmemo.[]disables directive checks.checkInterpolations(boolean, defaulttrue): checks.ignoreScopeReferences(boolean, defaultfalse): skip an entire expression if it references a runtime-visiblev-foror slot variable. Type-only references do not qualify. Av-if/v-else-ifcannot use its own element'sv-foraliases, and slot variables are only visible to descendants. Outer loop or slot variables still qualify in those positions. Other expressions on the same element are still checked. By default, local expressions are checked: extract them into a function acceptingitem/row, or into a child component, rather than a computed property that cannot access template locals.
For example, permit three logical operators and skip expressions that depend on loop or slot variables:
json
{ "maxLogicalExpressions": 3, "ignoreScopeReferences": true }Scope
Only checks Vue template expressions with vue-eslint-parser. Plain JS/TS, script blocks, static attributes, dynamic directive arguments, and empty or unparseable expressions are skipped. TypeScript expression wrappers do not reset either metric; erased type annotations and type arguments do not count. Each binding/interpolation has independent counters.
Calls, arithmetic, member access, optional chaining, and negation have no cost by themselves. Their nested logical and conditional expressions still count. The rule measures these two specific metrics, not every kind of complexity. An expression exceeding both limits receives two diagnostics, both located on the complete expression.
v-on, v-for sources, and slot binding patterns are always outside this rule. Use prefer-simple-event-handler and no-complex-v-for-source for their separate checks.
No automatic fix or suggestion is provided. Extraction requires choosing a meaningful name and preserving reactivity and template-local scope.