Skip to content

Add vue/no-constant-condition rule #1401

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 5 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
| [vue/keyword-spacing](./keyword-spacing.md) | enforce consistent spacing before and after keywords | :wrench: |
| [vue/max-len](./max-len.md) | enforce a maximum line length | |
| [vue/no-constant-condition](./no-constant-condition.md) | disallow constant expressions in conditions | |
| [vue/no-empty-pattern](./no-empty-pattern.md) | disallow empty destructuring patterns | |
| [vue/no-extra-parens](./no-extra-parens.md) | disallow unnecessary parentheses | :wrench: |
| [vue/no-irregular-whitespace](./no-irregular-whitespace.md) | disallow irregular whitespace | |
Expand Down
29 changes: 29 additions & 0 deletions docs/rules/no-constant-condition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-constant-condition
description: disallow constant expressions in conditions
since: v7.5.0
---
# vue/no-constant-condition

> disallow constant expressions in conditions

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

## :books: Further Reading

- [no-constant-condition]

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

## :rocket: Version

This rule was introduced in eslint-plugin-vue v7.5.0

## :mag: Implementation

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

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-constant-condition)</sup>
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = {
'no-bare-strings-in-template': require('./rules/no-bare-strings-in-template'),
'no-boolean-default': require('./rules/no-boolean-default'),
'no-confusing-v-for-v-if': require('./rules/no-confusing-v-for-v-if'),
'no-constant-condition': require('./rules/no-constant-condition'),
'no-custom-modifiers-on-v-model': require('./rules/no-custom-modifiers-on-v-model'),
'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),
'no-deprecated-destroyed-lifecycle': require('./rules/no-deprecated-destroyed-lifecycle'),
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/no-constant-condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @author Flo Edelmann
*/
'use strict'

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

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule('no-constant-condition', {
create(_context, { coreHandlers }) {
return {
VDirectiveKey(node) {
if (
node.name.name === 'if' &&
node.parent.value &&
node.parent.value.expression &&
coreHandlers.IfStatement
) {
coreHandlers.IfStatement({
// @ts-expect-error -- Process expression of VExpressionContainer as IfStatement.
test: node.parent.value.expression
})
}
}
}
}
})
111 changes: 111 additions & 0 deletions tests/lib/rules/no-constant-condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @author Flo Edelmann
*/
'use strict'

const { RuleTester } = require('eslint')
const rule = require('../../../lib/rules/no-constant-condition.js')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: { ecmaVersion: 6 }
})

tester.run('no-constant-condition', rule, {
valid: [
'<template><CustomButton v-if="a" /></template>',
'<template><CustomButton v-if="a == 0" /></template>',
'<template><CustomButton v-if="a == f()" /></template>',
'<template><CustomButton v-other-directive="true" /></template>'
],
invalid: [
{
code: '<template><CustomButton v-if="-2" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'UnaryExpression',
column: 31,
endColumn: 33
}
]
},
{
code: '<template><CustomButton v-if="true" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'Literal',
column: 31,
endColumn: 35
}
]
},
{
code: '<template><CustomButton v-if="1" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'Literal',
column: 31,
endColumn: 32
}
]
},
{
code: '<template><CustomButton v-if="{}" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'ObjectExpression',
column: 31,
endColumn: 33
}
]
},
{
code: '<template><CustomButton v-if="0 < 1" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'BinaryExpression',
column: 31,
endColumn: 36
}
]
},
{
code: '<template><CustomButton v-if="0 || 1" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'LogicalExpression',
column: 31,
endColumn: 37
}
]
},
{
code: '<template><CustomButton v-if="`foo`" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'TemplateLiteral',
column: 31,
endColumn: 36
}
]
},
{
code: '<template><CustomButton v-if="``" /></template>',
errors: [
{
messageId: 'unexpected',
type: 'TemplateLiteral',
column: 31,
endColumn: 33
}
]
}
]
})