Skip to content

Commit 6b62278

Browse files
authored
Add vue/no-constant-condition rule (#1401)
* Add `vue/no-constant-condition` rule * Comment out failing test cases * Support `v-else-if`, `v-else` and `v-show` as well * Update plugin docs (remove version, add "unreleased" notice) * Remove `v-else` again 🤦
1 parent 42a543d commit 6b62278

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-constant-condition
5+
description: disallow constant expressions in conditions
6+
---
7+
# vue/no-constant-condition
8+
9+
> disallow constant expressions in conditions
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
13+
This rule is the same rule as core [no-constant-condition] rule but it applies to the expressions in `<template>`.
14+
15+
## :books: Further Reading
16+
17+
- [no-constant-condition]
18+
19+
[no-constant-condition]: https://eslint.org/docs/rules/no-constant-condition
20+
21+
## :mag: Implementation
22+
23+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-constant-condition.js)
24+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-constant-condition.js)
25+
26+
<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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author Flo Edelmann
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
const conditionalDirectiveNames = new Set(['v-show', 'v-if', 'v-else-if'])
9+
10+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
11+
module.exports = wrapCoreRule('no-constant-condition', {
12+
create(_context, { coreHandlers }) {
13+
return {
14+
VDirectiveKey(node) {
15+
if (
16+
conditionalDirectiveNames.has(`v-${node.name.name}`) &&
17+
node.parent.value &&
18+
node.parent.value.expression &&
19+
coreHandlers.IfStatement
20+
) {
21+
coreHandlers.IfStatement({
22+
// @ts-expect-error -- Process expression of VExpressionContainer as IfStatement.
23+
test: node.parent.value.expression
24+
})
25+
}
26+
}
27+
}
28+
}
29+
})
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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-else-if="true" /></template>',
35+
errors: [
36+
{
37+
messageId: 'unexpected',
38+
type: 'Literal',
39+
column: 36,
40+
endColumn: 40
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-show="{}" /></template>',
57+
errors: [
58+
{
59+
messageId: 'unexpected',
60+
type: 'ObjectExpression',
61+
column: 33,
62+
endColumn: 35
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+
// failing in Node.js v8, because template literals are not supported there:
90+
// {
91+
// code: '<template><CustomButton v-if="`foo`" /></template>',
92+
// errors: [
93+
// {
94+
// messageId: 'unexpected',
95+
// type: 'TemplateLiteral',
96+
// column: 31,
97+
// endColumn: 36
98+
// }
99+
// ]
100+
// },
101+
// {
102+
// code: '<template><CustomButton v-if="``" /></template>',
103+
// errors: [
104+
// {
105+
// messageId: 'unexpected',
106+
// type: 'TemplateLiteral',
107+
// column: 31,
108+
// endColumn: 33
109+
// }
110+
// ]
111+
// }
112+
]
113+
})

0 commit comments

Comments
 (0)