noVueNegationElse (HTML)
Language HTML
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noVueNegationElse - This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
vue/no-negated-v-if-condition
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueNegationElse": "error" } } }}Description
Section titled “Description”Disallow negated conditions in v-if and v-else-if when a v-else branch follows.
A condition that starts with !, or that compares with != or !==, makes the
reader reverse the logic. When a v-else branch follows, the reader has to reverse
it a second time to understand when the v-else content shows. Removing the
negation and swapping the two branches shows the same content in the same cases,
and is easier to read.
A negated condition without a v-else branch is allowed, because there is no
branch to swap with. A double negation such as !!value is also allowed, because it
converts the value to true or false instead of reversing it.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<template> <div v-if="!isLoading">Content</div> <div v-else>Loading...</div></template>code-block.vue:2:14 lint/nursery/noVueNegationElse FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition is negated, but a v-else branch follows it.
1 │ <template>
> 2 │ <div v-if="!isLoading">Content</div>
│ ^^^^^^^^^^
3 │ <div v-else>Loading...</div>
4 │ </template>
ℹ The reader has to reverse the condition twice to know when the v-else content shows.
ℹ Remove the negation and swap the content of the two branches.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the negation and swap the branches.
1 1 │ <template>
2 │ - ··<div·v-if="!isLoading">Content</div>
3 │ - ··<div·v-else>Loading...</div>
2 │ + ··<div·v-if="isLoading">Loading...</div>
3 │ + ··<div·v-else>Content</div>
4 4 │ </template>
5 5 │
<template> <div v-if="status !== 'done'">Working</div> <div v-else>Done</div></template>code-block.vue:2:14 lint/nursery/noVueNegationElse FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition is negated, but a v-else branch follows it.
1 │ <template>
> 2 │ <div v-if="status !== 'done'">Working</div>
│ ^^^^^^^^^^^^^^^^^
3 │ <div v-else>Done</div>
4 │ </template>
ℹ The reader has to reverse the condition twice to know when the v-else content shows.
ℹ Remove the negation and swap the content of the two branches.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the negation and swap the branches.
1 1 │ <template>
2 │ - ··<div·v-if="status·!==·'done'">Working</div>
3 │ - ··<div·v-else>Done</div>
2 │ + ··<div·v-if="status·===·'done'">Done</div>
3 │ + ··<div·v-else>Working</div>
4 4 │ </template>
5 5 │
<template> <div v-if="isAdmin">Admin</div> <div v-else-if="!isMember">Guest</div> <div v-else>Member</div></template>code-block.vue:3:19 lint/nursery/noVueNegationElse FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition is negated, but a v-else branch follows it.
1 │ <template>
2 │ <div v-if="isAdmin">Admin</div>
> 3 │ <div v-else-if="!isMember">Guest</div>
│ ^^^^^^^^^
4 │ <div v-else>Member</div>
5 │ </template>
ℹ The reader has to reverse the condition twice to know when the v-else content shows.
ℹ Remove the negation and swap the content of the two branches.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Remove the negation and swap the branches.
1 1 │ <template>
2 2 │ <div v-if="isAdmin">Admin</div>
3 │ - ··<div·v-else-if="!isMember">Guest</div>
4 │ - ··<div·v-else>Member</div>
3 │ + ··<div·v-else-if="isMember">Member</div>
4 │ + ··<div·v-else>Guest</div>
5 5 │ </template>
6 6 │
<template> <div v-if="isLoading">Loading...</div> <div v-else>Content</div>
<div v-if="!isLoading">Content</div>
<div v-if="!isAdmin">Guest</div> <div v-else-if="isMember">Member</div>
<div v-if="!!user">Signed in</div> <div v-else>Signed out</div></template>See Also
Section titled “See Also”- To disallow negated conditions in JavaScript
ifstatements and ternaries, seenoNegationElse.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.