@@ -16,68 +16,88 @@ import {
16
16
17
17
export const transformModel : DirectiveTransform = ( dir , node , context ) => {
18
18
const baseResult = baseTransform ( dir , node , context )
19
- // base transform has errors
20
- if ( ! baseResult . props . length ) {
19
+ // base transform has errors OR component v-model (only need props)
20
+ if ( ! baseResult . props . length || node . tagType === ElementTypes . COMPONENT ) {
21
21
return baseResult
22
22
}
23
23
24
- const { tag, tagType } = node
25
- if ( tagType === ElementTypes . ELEMENT ) {
26
- if ( dir . arg ) {
24
+ if ( dir . arg ) {
25
+ context . onError (
26
+ createDOMCompilerError (
27
+ DOMErrorCodes . X_V_MODEL_ARG_ON_ELEMENT ,
28
+ dir . arg . loc
29
+ )
30
+ )
31
+ }
32
+
33
+ function checkDuplicatedValue ( ) {
34
+ const value = findProp ( node , 'value' )
35
+ if ( value ) {
27
36
context . onError (
28
37
createDOMCompilerError (
29
- DOMErrorCodes . X_V_MODEL_ARG_ON_ELEMENT ,
30
- dir . arg . loc
38
+ DOMErrorCodes . X_V_MODEL_UNNECESSARY_VALUE ,
39
+ value . loc
31
40
)
32
41
)
33
42
}
43
+ }
34
44
35
- if ( tag === 'input' || tag === 'textarea' || tag === 'select' ) {
36
- let directiveToUse = V_MODEL_TEXT
37
- let isInvalidType = false
38
- if ( tag === 'input' ) {
39
- const type = findProp ( node , `type` )
40
- if ( type ) {
41
- if ( type . type === NodeTypes . DIRECTIVE ) {
42
- // :type="foo"
43
- directiveToUse = V_MODEL_DYNAMIC
44
- } else if ( type . value ) {
45
- switch ( type . value . content ) {
46
- case 'radio' :
47
- directiveToUse = V_MODEL_RADIO
48
- break
49
- case 'checkbox' :
50
- directiveToUse = V_MODEL_CHECKBOX
51
- break
52
- case 'file' :
53
- isInvalidType = true
54
- context . onError (
55
- createDOMCompilerError (
56
- DOMErrorCodes . X_V_MODEL_ON_FILE_INPUT_ELEMENT ,
57
- dir . loc
58
- )
45
+ const { tag } = node
46
+ if ( tag === 'input' || tag === 'textarea' || tag === 'select' ) {
47
+ let directiveToUse = V_MODEL_TEXT
48
+ let isInvalidType = false
49
+ if ( tag === 'input' ) {
50
+ const type = findProp ( node , ` type` )
51
+ if ( type ) {
52
+ if ( type . type === NodeTypes . DIRECTIVE ) {
53
+ // :type="foo"
54
+ directiveToUse = V_MODEL_DYNAMIC
55
+ } else if ( type . value ) {
56
+ switch ( type . value . content ) {
57
+ case 'radio' :
58
+ directiveToUse = V_MODEL_RADIO
59
+ break
60
+ case 'checkbox' :
61
+ directiveToUse = V_MODEL_CHECKBOX
62
+ break
63
+ case 'file' :
64
+ isInvalidType = true
65
+ context . onError (
66
+ createDOMCompilerError (
67
+ DOMErrorCodes . X_V_MODEL_ON_FILE_INPUT_ELEMENT ,
68
+ dir . loc
59
69
)
60
- break
61
- }
70
+ )
71
+ break
72
+ default :
73
+ // text type
74
+ __DEV__ && checkDuplicatedValue ( )
75
+ break
62
76
}
63
77
}
64
- } else if ( tag === 'select' ) {
65
- directiveToUse = V_MODEL_SELECT
66
- }
67
- // inject runtime directive
68
- // by returning the helper symbol via needRuntime
69
- // the import will replaced a resolveDirective call.
70
- if ( ! isInvalidType ) {
71
- baseResult . needRuntime = context . helper ( directiveToUse )
78
+ } else {
79
+ // text type
80
+ __DEV__ && checkDuplicatedValue ( )
72
81
}
73
- } else {
74
- context . onError (
75
- createDOMCompilerError (
76
- DOMErrorCodes . X_V_MODEL_ON_INVALID_ELEMENT ,
77
- dir . loc
78
- )
79
- )
82
+ } else if ( tag === 'select' ) {
83
+ directiveToUse = V_MODEL_SELECT
84
+ } else if ( tag === 'textarea' ) {
85
+ __DEV__ && checkDuplicatedValue ( )
80
86
}
87
+ // inject runtime directive
88
+ // by returning the helper symbol via needRuntime
89
+ // the import will replaced a resolveDirective call.
90
+ if ( ! isInvalidType ) {
91
+ baseResult . needRuntime = context . helper ( directiveToUse )
92
+ }
93
+ } else {
94
+ context . onError (
95
+ createDOMCompilerError (
96
+ DOMErrorCodes . X_V_MODEL_ON_INVALID_ELEMENT ,
97
+ dir . loc
98
+ )
99
+ )
81
100
}
101
+
82
102
return baseResult
83
103
}
0 commit comments