Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 🤦
- Loading branch information
1 parent
42a543d
commit 6b62278
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-constant-condition | ||
description: disallow constant expressions in conditions | ||
--- | ||
# vue/no-constant-condition | ||
|
||
> disallow constant expressions in conditions | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
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 | ||
|
||
## :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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @author Flo Edelmann | ||
*/ | ||
'use strict' | ||
|
||
const { wrapCoreRule } = require('../utils') | ||
|
||
const conditionalDirectiveNames = new Set(['v-show', 'v-if', 'v-else-if']) | ||
|
||
// 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 ( | ||
conditionalDirectiveNames.has(`v-${node.name.name}`) && | ||
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 | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @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-else-if="true" /></template>', | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
type: 'Literal', | ||
column: 36, | ||
endColumn: 40 | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><CustomButton v-if="1" /></template>', | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
type: 'Literal', | ||
column: 31, | ||
endColumn: 32 | ||
} | ||
] | ||
}, | ||
{ | ||
code: '<template><CustomButton v-show="{}" /></template>', | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
type: 'ObjectExpression', | ||
column: 33, | ||
endColumn: 35 | ||
} | ||
] | ||
}, | ||
{ | ||
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 | ||
} | ||
] | ||
} | ||
|
||
// failing in Node.js v8, because template literals are not supported there: | ||
// { | ||
// 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 | ||
// } | ||
// ] | ||
// } | ||
] | ||
}) |