Skip to content

Commit 86d22b6

Browse files
committed
Add vue/no-constant-condition rule
1 parent 1b75e28 commit 86d22b6

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
347347
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
348348
| [vue/keyword-spacing](./keyword-spacing.md) | enforce consistent spacing before and after keywords | :wrench: |
349349
| [vue/max-len](./max-len.md) | enforce a maximum line length | |
350+
| [vue/no-constant-condition](./no-constant-condition.md) | disallow constant expressions in conditions | |
350351
| [vue/no-empty-pattern](./no-empty-pattern.md) | disallow empty destructuring patterns | |
351352
| [vue/no-extra-parens](./no-extra-parens.md) | disallow unnecessary parentheses | :wrench: |
352353
| [vue/no-irregular-whitespace](./no-irregular-whitespace.md) | disallow irregular whitespace | |

docs/rules/no-constant-condition.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-constant-condition
5+
description: disallow constant expressions in conditions
6+
since: v7.5.0
7+
---
8+
# vue/no-constant-condition
9+
10+
> disallow constant expressions in conditions
11+
12+
This rule is the same rule as core [no-constant-condition] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further Reading
15+
16+
- [no-constant-condition]
17+
18+
[no-constant-condition]: https://eslint.org/docs/rules/no-constant-condition
19+
20+
## :rocket: Version
21+
22+
This rule was introduced in eslint-plugin-vue v7.5.0
23+
24+
## :mag: Implementation
25+
26+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-constant-condition.js)
27+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-constant-condition.js)
28+
29+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-constant-condition)</sup>

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module.exports = {
5353
'no-bare-strings-in-template': require('./rules/no-bare-strings-in-template'),
5454
'no-boolean-default': require('./rules/no-boolean-default'),
5555
'no-confusing-v-for-v-if': require('./rules/no-confusing-v-for-v-if'),
56+
'no-constant-condition': require('./rules/no-constant-condition'),
5657
'no-custom-modifiers-on-v-model': require('./rules/no-custom-modifiers-on-v-model'),
5758
'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),
5859
'no-deprecated-destroyed-lifecycle': require('./rules/no-deprecated-destroyed-lifecycle'),

lib/rules/no-constant-condition.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Flo Edelmann
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule('no-constant-condition', {
10+
create(_context, { coreHandlers }) {
11+
return {
12+
VDirectiveKey(node) {
13+
if (
14+
node.name.name === 'if' &&
15+
node.parent.value &&
16+
node.parent.value.expression &&
17+
coreHandlers.IfStatement
18+
) {
19+
coreHandlers.IfStatement({
20+
// @ts-expect-error -- Process expression of VExpressionContainer as IfStatement.
21+
test: node.parent.value.expression
22+
})
23+
}
24+
}
25+
}
26+
}
27+
})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @author Flo Edelmann
3+
*/
4+
'use strict'
5+
6+
const { RuleTester } = require('eslint')
7+
const rule = require('../../../lib/rules/no-constant-condition.js')
8+
9+
const tester = new RuleTester({
10+
parser: require.resolve('vue-eslint-parser'),
11+
parserOptions: { ecmaVersion: 6 }
12+
})
13+
14+
tester.run('no-constant-condition', rule, {
15+
valid: [
16+
'<template><CustomButton v-if="a" /></template>',
17+
'<template><CustomButton v-if="a == 0" /></template>',
18+
'<template><CustomButton v-if="a == f()" /></template>',
19+
'<template><CustomButton v-other-directive="true" /></template>'
20+
],
21+
invalid: [
22+
{
23+
code: '<template><CustomButton v-if="-2" /></template>',
24+
errors: [
25+
{
26+
messageId: 'unexpected',
27+
type: 'UnaryExpression',
28+
column: 31,
29+
endColumn: 33
30+
}
31+
]
32+
},
33+
{
34+
code: '<template><CustomButton v-if="true" /></template>',
35+
errors: [
36+
{
37+
messageId: 'unexpected',
38+
type: 'Literal',
39+
column: 31,
40+
endColumn: 35
41+
}
42+
]
43+
},
44+
{
45+
code: '<template><CustomButton v-if="1" /></template>',
46+
errors: [
47+
{
48+
messageId: 'unexpected',
49+
type: 'Literal',
50+
column: 31,
51+
endColumn: 32
52+
}
53+
]
54+
},
55+
{
56+
code: '<template><CustomButton v-if="{}" /></template>',
57+
errors: [
58+
{
59+
messageId: 'unexpected',
60+
type: 'ObjectExpression',
61+
column: 31,
62+
endColumn: 33
63+
}
64+
]
65+
},
66+
{
67+
code: '<template><CustomButton v-if="0 < 1" /></template>',
68+
errors: [
69+
{
70+
messageId: 'unexpected',
71+
type: 'BinaryExpression',
72+
column: 31,
73+
endColumn: 36
74+
}
75+
]
76+
},
77+
{
78+
code: '<template><CustomButton v-if="0 || 1" /></template>',
79+
errors: [
80+
{
81+
messageId: 'unexpected',
82+
type: 'LogicalExpression',
83+
column: 31,
84+
endColumn: 37
85+
}
86+
]
87+
},
88+
{
89+
code: '<template><CustomButton v-if="`foo`" /></template>',
90+
errors: [
91+
{
92+
messageId: 'unexpected',
93+
type: 'TemplateLiteral',
94+
column: 31,
95+
endColumn: 36
96+
}
97+
]
98+
},
99+
{
100+
code: '<template><CustomButton v-if="``" /></template>',
101+
errors: [
102+
{
103+
messageId: 'unexpected',
104+
type: 'TemplateLiteral',
105+
column: 31,
106+
endColumn: 33
107+
}
108+
]
109+
}
110+
]
111+
})

0 commit comments

Comments
 (0)