Skip to content

Commit efe4a58

Browse files
committed
feat(RFC0005): add no-v-model-argument rule which disallows v-model arguments.
Vue 2 compatibility rule
1 parent b9def18 commit efe4a58

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
5050
| [vue/no-unused-components](./no-unused-components.md) | disallow registering components that are not used inside templates | |
5151
| [vue/no-unused-vars](./no-unused-vars.md) | disallow unused variable definitions of v-for directives or scope attributes | |
5252
| [vue/no-use-v-if-with-v-for](./no-use-v-if-with-v-for.md) | disallow use v-if on the same element as v-for | |
53+
| [vue/no-v-model-argument](./no-v-model-argument.md) | disallow adding an argument to `v-model` used in custom component | |
5354
| [vue/require-component-is](./require-component-is.md) | require `v-bind:is` of `<component>` elements | |
5455
| [vue/require-prop-type-constructor](./require-prop-type-constructor.md) | require prop type to be a constructor | :wrench: |
5556
| [vue/require-render-return](./require-render-return.md) | enforce render function to always return value | |

docs/rules/no-v-model-argument.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-v-model-argument
5+
description: disallow adding an argument to `v-model` used in custom component
6+
---
7+
# vue/no-v-model-argument
8+
> disallow adding an argument to `v-model` used in custom component
9+
10+
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`.
11+
12+
This rule checks whether `v-model` used on custom component do not have an argument.
13+
14+
## :book: Rule Details
15+
16+
This rule reports `v-model` directives in the following cases:
17+
18+
- The directive used on component has an argument. E.g. `<MyComponent v-model:aaa="foo" />`
19+
20+
<eslint-code-block :rules="{'vue/no-v-model-argument': ['error']}">
21+
22+
```vue
23+
<template>
24+
<!-- ✓ GOOD -->
25+
<MyComponent v-model="foo" />
26+
27+
28+
<!-- ✗ BAD -->
29+
<MyComponent v-model:aaa="foo" />
30+
</template>
31+
```
32+
33+
</eslint-code-block>
34+
35+
36+
## :wrench: Options
37+
38+
Nothing.
39+
40+
## :couple: Related rules
41+
42+
- [valid-v-model]
43+
44+
[valid-v-model]: valid-v-model.md
45+
46+
## :mag: Implementation
47+
48+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-v-model-argument.js)
49+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-v-model-argument.js)

lib/configs/essential.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
'vue/no-unused-components': 'error',
1919
'vue/no-unused-vars': 'error',
2020
'vue/no-use-v-if-with-v-for': 'error',
21+
'vue/no-v-model-argument': 'error',
2122
'vue/require-component-is': 'error',
2223
'vue/require-prop-type-constructor': 'error',
2324
'vue/require-render-return': 'error',

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module.exports = {
6363
'no-unused-vars': require('./rules/no-unused-vars'),
6464
'no-use-v-if-with-v-for': require('./rules/no-use-v-if-with-v-for'),
6565
'no-v-html': require('./rules/no-v-html'),
66+
'no-v-model-argument': require('./rules/no-v-model-argument'),
6667
'object-curly-spacing': require('./rules/object-curly-spacing'),
6768
'order-in-components': require('./rules/order-in-components'),
6869
'prop-name-casing': require('./rules/prop-name-casing'),

lib/rules/no-v-model-argument.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @author Przemyslaw Falowski (@przemkow)
3+
* @fileoverview This rule checks whether v-model used on custom component do not have an argument
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const utils = require('../utils')
12+
13+
// ------------------------------------------------------------------------------
14+
// Rule Definition
15+
// ------------------------------------------------------------------------------
16+
17+
module.exports = {
18+
meta: {
19+
type: 'problem',
20+
docs: {
21+
description: 'disallow adding an argument to `v-model` used in custom component',
22+
category: 'essential',
23+
url: 'https://eslint.vuejs.org/rules/no-v-model-argument.html'
24+
},
25+
fixable: null,
26+
schema: [],
27+
messages: {
28+
vModelRequireNoArgument: "'v-model' directives require no argument."
29+
}
30+
},
31+
32+
create (context) {
33+
return utils.defineTemplateBodyVisitor(context, {
34+
"VAttribute[directive=true][key.name.name='model']" (node) {
35+
const element = node.parent.parent
36+
37+
if (node.key.argument && utils.isCustomComponent(element)) {
38+
context.report({
39+
node,
40+
loc: node.loc,
41+
messageId: 'vModelRequireNoArgument'
42+
})
43+
}
44+
}
45+
})
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @author Przemyslaw Falowski (@przemkow)
3+
* @fileoverview This rule checks whether v-model used on custom component do not have an argument
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const rule = require('../../../lib/rules/no-v-model-argument')
12+
const RuleTester = require('eslint').RuleTester
13+
14+
// ------------------------------------------------------------------------------
15+
// Tests
16+
// ------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester({
19+
parser: require.resolve('vue-eslint-parser'),
20+
parserOptions: { ecmaVersion: 2015 }
21+
})
22+
23+
ruleTester.run('no-v-model-argument', rule, {
24+
25+
valid: [
26+
{
27+
filename: 'test.vue',
28+
code: '<template><MyComponent v-model="bar"></MyComponent></template>'
29+
}
30+
],
31+
32+
invalid: [
33+
{
34+
filename: 'test.vue',
35+
code: '<template><MyComponent v-model:foo="bar"></MyComponent></template>',
36+
errors: ["'v-model' directives require no argument."]
37+
},
38+
{
39+
filename: 'test.vue',
40+
code: '<template><MyComponent v-model:foo.trim="bar"></MyComponent></template>',
41+
errors: ["'v-model' directives require no argument."]
42+
}
43+
]
44+
})

0 commit comments

Comments
 (0)