Skip to content

Add new vue/no-negated-condition rule #2795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/true-pumas-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-vue': minor
---

Added new [`vue/no-negated-condition`](https://eslint.vuejs.org/rules/no-negated-condition.html) rule
2 changes: 2 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/no-implicit-coercion] | Disallow shorthand type conversions in `<template>` | :wrench::bulb: | :hammer: |
| [vue/no-irregular-whitespace] | disallow irregular whitespace in `.vue` files | | :warning: |
| [vue/no-loss-of-precision] | Disallow literal numbers that lose precision in `<template>` | | :warning: |
| [vue/no-negated-condition] | Disallow negated conditions in `<template>` | | :hammer: |
| [vue/no-restricted-syntax] | Disallow specified syntax in `<template>` | | :hammer: |
| [vue/no-sparse-arrays] | Disallow sparse arrays in `<template>` | | :warning: |
| [vue/no-useless-concat] | Disallow unnecessary concatenation of literals or template literals in `<template>` | | :hammer: |
Expand Down Expand Up @@ -482,6 +483,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
[vue/no-multiple-slot-args]: ./no-multiple-slot-args.md
[vue/no-multiple-template-root]: ./no-multiple-template-root.md
[vue/no-mutating-props]: ./no-mutating-props.md
[vue/no-negated-condition]: ./no-negated-condition.md
[vue/no-parsing-error]: ./no-parsing-error.md
[vue/no-potential-component-option-typo]: ./no-potential-component-option-typo.md
[vue/no-ref-as-operand]: ./no-ref-as-operand.md
Expand Down
33 changes: 33 additions & 0 deletions docs/rules/no-negated-condition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-negated-condition
description: Disallow negated conditions in `<template>`
---

# vue/no-negated-condition

> Disallow negated conditions in `<template>`

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge>

## :book: Rule Details

This rule is the same rule as core [no-negated-condition] rule but it applies to the expressions in `<template>`.

## :couple: Related Rules

- [unicorn/no-negated-condition](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-negated-condition.md)

## :books: Further Reading

- [no-negated-condition]

[no-negated-condition]: https://eslint.org/docs/rules/no-negated-condition

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-negated-condition.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-negated-condition.js)

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/latest/rules/no-negated-condition)</sup>
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const plugin = {
'no-multiple-slot-args': require('./rules/no-multiple-slot-args'),
'no-multiple-template-root': require('./rules/no-multiple-template-root'),
'no-mutating-props': require('./rules/no-mutating-props'),
'no-negated-condition': require('./rules/no-negated-condition'),
'no-parsing-error': require('./rules/no-parsing-error'),
'no-potential-component-option-typo': require('./rules/no-potential-component-option-typo'),
'no-ref-as-operand': require('./rules/no-ref-as-operand'),
Expand Down
12 changes: 12 additions & 0 deletions lib/rules/no-negated-condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line internal/no-invalid-meta
module.exports = wrapCoreRule('no-negated-condition', {
applyDocument: true
})
166 changes: 166 additions & 0 deletions tests/lib/rules/no-negated-condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('../../eslint-compat').RuleTester
const rule = require('../../../lib/rules/no-negated-condition')

const tester = new RuleTester({
languageOptions: {
parser: require('vue-eslint-parser'),
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('no-negated-condition', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<div :class="foo ? 'baz' : 'bar'" />
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div :style="{ color: isActive ? 'red' : 'blue' }" />
</template>
`
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<div :class="!foo ? 'bar' : 'baz'" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 3,
column: 22,
endLine: 3,
endColumn: 42
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :style="{
'display': !isVisible ? 'none' : 'block',
'color': 'red'
}" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 4,
column: 22,
endLine: 4,
endColumn: 51
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'hidden': !foo ? true : false,
'visible': bar
}" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 4,
column: 21,
endLine: 4,
endColumn: 40
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :disabled="!enabled ? true : false" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 3,
column: 25,
endLine: 3,
endColumn: 48
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :class="a !== b ? 'different' : 'same'" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 3,
column: 22,
endLine: 3,
endColumn: 52
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :class="a != b ? 'not-equal' : 'equal'" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 3,
column: 22,
endLine: 3,
endColumn: 52
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :style="{
opacity: !(x && y) ? 0 : 1
}" />
</template>
`,
errors: [
{
message: 'Unexpected negated condition.',
line: 4,
column: 20,
endLine: 4,
endColumn: 37
}
]
}
]
})