Skip to content

Commit

Permalink
Don't report dynamic types on inputs with v-model directive (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsnik committed Nov 24, 2017
1 parent 6b13226 commit 2793ca2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
1 change: 0 additions & 1 deletion docs/rules/valid-v-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This rule reports `v-model` directives in the following cases:
- The directive does not have that attribute value. E.g. `<input v-model>`
- The directive does not have the attribute value which is valid as LHS. E.g. `<input v-model="foo() + bar()">`
- The directive is on unsupported elements. E.g. `<div v-model="foo"></div>`
- The directive is on `<input>` elements which their types are dynamic. E.g. `<input :type="type" v-model="foo">`
- The directive is on `<input>` elements which their types are `file`. E.g. `<input type="file" v-model="foo">`
- The directive's reference is iteration variables. E.g. `<div v-for="x in list"><input type="file" v-model="x"></div>`

Expand Down
25 changes: 8 additions & 17 deletions lib/rules/valid-v-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,14 @@ function create (context) {
data: { name }
})
}
if (name === 'input') {
if (utils.hasDirective(element, 'bind', 'type')) {
context.report({
node,
loc: node.loc,
message: "'v-model' directives don't support dynamic input types.",
data: { name }
})
}
if (utils.hasAttribute(element, 'type', 'file')) {
context.report({
node,
loc: node.loc,
message: "'v-model' directives don't support 'file' input type.",
data: { name }
})
}

if (name === 'input' && utils.hasAttribute(element, 'type', 'file')) {
context.report({
node,
loc: node.loc,
message: "'v-model' directives don't support 'file' input type.",
data: { name }
})
}

if (node.key.argument) {
Expand Down
18 changes: 8 additions & 10 deletions tests/lib/rules/valid-v-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ tester.run('valid-v-model', rule, {
{
filename: 'test.vue',
code: '<template><div><div v-for="x in list"><input v-model="x.foo"></div></div></template>'
},
{
filename: 'test.vue',
code: '<template><input :type="a" v-model="b"></template>'
},
{
filename: 'test.vue',
code: '<template><input v-bind:type="a" v-model="b"></template>'
}
],
invalid: [
Expand Down Expand Up @@ -94,16 +102,6 @@ tester.run('valid-v-model', rule, {
code: '<template><input v-model="a + b"></template>',
errors: ["'v-model' directives require the attribute value which is valid as LHS."]
},
{
filename: 'test.vue',
code: '<template><input :type="a" v-model="b"></template>',
errors: ["'v-model' directives don't support dynamic input types."]
},
{
filename: 'test.vue',
code: '<template><input v-bind:type="a" v-model="b"></template>',
errors: ["'v-model' directives don't support dynamic input types."]
},
{
filename: 'test.vue',
code: '<template><input type="file" v-model="b"></template>',
Expand Down

0 comments on commit 2793ca2

Please sign in to comment.