-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RFC0011): add vue/no-custom-modifiers-on-v-model rule which chec…
…ks if v-model has not allowed modifiers (Vue 2 backward compatibility)
- Loading branch information
Showing
6 changed files
with
185 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,53 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-custom-modifiers-on-v-model | ||
description: disallow custom modifiers on v-model used on the component | ||
--- | ||
# vue/no-custom-modifiers-on-v-model | ||
> disallow custom modifiers on v-model used on the component | ||
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
|
||
This rule checks whether `v-model `used on the component do not have custom modifiers. | ||
|
||
## Rule Details | ||
|
||
This rule reports `v-model` directives in the following cases: | ||
|
||
- The directive used on the component has custom modifiers. E.g. `<MyComponent v-model.aaa="foo" />` | ||
|
||
<eslint-code-block :rules="{'vue/no-custom-modifiers-on-v-model': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<MyComponent v-model="foo" /> | ||
<MyComponent v-model.trim="foo" /> | ||
<MyComponent v-model.lazy="foo" /> | ||
<MyComponent v-model.number="foo" /> | ||
<!-- ✗ BAD --> | ||
<MyComponent v-model.aaa="foo" /> | ||
<MyComponent v-model.aaa.bbb="foo" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related rules | ||
|
||
- [valid-v-model] | ||
|
||
[valid-v-model]: valid-v-model.md | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-custom-modifiers-on-v-model.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-custom-modifiers-on-v-model.js) |
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
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,57 @@ | ||
/** | ||
* @author Przemyslaw Falowski (@przemkow) | ||
* @fileoverview This rule checks whether v-model used on the component do not have custom modifiers | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
const VALID_MODIFIERS = new Set(['lazy', 'number', 'trim']) | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow custom modifiers on v-model used on the component', | ||
category: 'essential', | ||
url: 'https://eslint.vuejs.org/rules/no-custom-modifiers-on-v-model.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
notSupportedModifier: "'v-model' directives don't support the modifier '{{name}}'." | ||
} | ||
}, | ||
create (context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=true][key.name.name='model']" (node) { | ||
const element = node.parent.parent | ||
|
||
if (utils.isCustomComponent(element)) { | ||
for (const modifier of node.key.modifiers) { | ||
if (!VALID_MODIFIERS.has(modifier.name)) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'notSupportedModifier', | ||
data: { name: modifier.name } | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,72 @@ | ||
/** | ||
* @author Przemyslaw Falowski (@przemkow) | ||
* @fileoverview This rule checks whether v-model used on the component do not have custom modifiers | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-custom-modifiers-on-v-model') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
ruleTester.run('no-custom-modifiers-on-v-model', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model:propName="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model:propName.trim="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model.trim="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model:propName.lazy="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model.lazy="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model:propName.number="foo"></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model.number="foo"></template>' | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model:propName.aaa="foo"></template>', | ||
errors: ["'v-model' directives don't support the modifier 'aaa'."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><MyComponent v-model.aaa="foo"></template>', | ||
errors: ["'v-model' directives don't support the modifier 'aaa'."] | ||
} | ||
] | ||
}) |