1
1
import * as t from '@babel/types' ;
2
2
import { NodePath } from '@babel/traverse' ;
3
3
import { createIdentifier } from './utils' ;
4
- import { State , ExcludesBoolean } from '.' ;
4
+ import { State } from '.' ;
5
5
6
6
export type Tag = t . Identifier | t . MemberExpression | t . StringLiteral | t . CallExpression ;
7
7
@@ -24,7 +24,7 @@ const getType = (path: NodePath<t.JSXOpeningElement>) => {
24
24
return typePath ? typePath . get ( 'value' ) . node : null ;
25
25
} ;
26
26
27
- const parseModifiers = ( value : t . ArrayExpression ) : string [ ] => (
27
+ const parseModifiers = ( value : any ) : string [ ] => (
28
28
t . isArrayExpression ( value )
29
29
? value . elements
30
30
. map ( ( el ) => ( t . isStringLiteral ( el ) ? el . value : '' ) )
@@ -64,37 +64,38 @@ const parseDirectives = (params: {
64
64
const shouldResolve = ! [ 'html' , 'text' , 'model' , 'models' ] . includes ( directiveName )
65
65
|| ( isVModel && ! isComponent ) ;
66
66
67
- if ( [ 'models' , 'model' ] . includes ( directiveName ) ) {
68
- if ( t . isArrayExpression ( value ) ) {
69
- const elementsList = isVModels ? value . elements ! : [ value ] ;
67
+ let modifiers = underscoreModifiers ;
70
68
71
- elementsList . forEach ( ( element ) => {
72
- if ( isVModels && ! t . isArrayExpression ( element ) ) {
73
- throw new Error ( 'You should pass a Two-dimensional Arrays to v-models' ) ;
74
- }
69
+ if ( t . isArrayExpression ( value ) ) {
70
+ const elementsList = isVModels ? value . elements ! : [ value ] ;
75
71
76
- const { elements } = element as t . ArrayExpression ;
77
- const [ first , second , third ] = elements ;
78
- let modifiers = underscoreModifiers ;
72
+ elementsList . forEach ( ( element ) => {
73
+ if ( isVModels && ! t . isArrayExpression ( element ) ) {
74
+ throw new Error ( 'You should pass a Two-dimensional Arrays to v-models' ) ;
75
+ }
79
76
80
- if ( second && ! t . isArrayExpression ( second ) && ! t . isSpreadElement ( second ) ) {
81
- args . push ( second ) ;
82
- modifiers = parseModifiers ( third as t . ArrayExpression ) ;
83
- } else if ( t . isArrayExpression ( second ) ) {
84
- args . push ( t . nullLiteral ( ) ) ;
85
- modifiers = parseModifiers ( second ) ;
86
- } else {
87
- // work as v-model={[value]} or v-models={[[value]]}
77
+ const { elements } = element as t . ArrayExpression ;
78
+ const [ first , second , third ] = elements ;
79
+
80
+ if ( second && ! t . isArrayExpression ( second ) && ! t . isSpreadElement ( second ) ) {
81
+ args . push ( second ) ;
82
+ modifiers = parseModifiers ( third as t . ArrayExpression ) ;
83
+ } else if ( t . isArrayExpression ( second ) ) {
84
+ if ( ! shouldResolve ) {
88
85
args . push ( t . nullLiteral ( ) ) ;
89
86
}
90
- modifiersSet . push ( new Set ( modifiers ) ) ;
91
- vals . push ( first as t . Expression ) ;
92
- } ) ;
93
- } else if ( isVModel ) {
94
- // work as v-model={value}
95
- args . push ( t . nullLiteral ( ) ) ;
96
- modifiersSet . push ( new Set ( underscoreModifiers ) ) ;
97
- }
87
+ modifiers = parseModifiers ( second ) ;
88
+ } else if ( ! shouldResolve ) {
89
+ // work as v-model={[value]} or v-models={[[value]]}
90
+ args . push ( t . nullLiteral ( ) ) ;
91
+ }
92
+ modifiersSet . push ( new Set ( modifiers ) ) ;
93
+ vals . push ( first as t . Expression ) ;
94
+ } ) ;
95
+ } else if ( isVModel && ! shouldResolve ) {
96
+ // work as v-model={value}
97
+ args . push ( t . nullLiteral ( ) ) ;
98
+ modifiersSet . push ( new Set ( underscoreModifiers ) ) ;
98
99
} else {
99
100
modifiersSet . push ( new Set ( underscoreModifiers ) ) ;
100
101
}
@@ -107,7 +108,9 @@ const parseDirectives = (params: {
107
108
directive : shouldResolve ? [
108
109
resolveDirective ( path , state , tag , directiveName ) ,
109
110
vals [ 0 ] || value ,
110
- ! ! modifiersSet [ 0 ] ?. size && t . unaryExpression ( 'void' , t . numericLiteral ( 0 ) , true ) ,
111
+ modifiersSet [ 0 ] ?. size
112
+ ? args [ 0 ] || t . unaryExpression ( 'void' , t . numericLiteral ( 0 ) , true )
113
+ : args [ 0 ] ,
111
114
! ! modifiersSet [ 0 ] ?. size && t . objectExpression (
112
115
[ ...modifiersSet [ 0 ] ] . map (
113
116
( modifier ) => t . objectProperty (
@@ -116,7 +119,7 @@ const parseDirectives = (params: {
116
119
) ,
117
120
) ,
118
121
) ,
119
- ] . filter ( Boolean as any as ExcludesBoolean ) : undefined ,
122
+ ] . filter ( Boolean ) as t . Expression [ ] : undefined ,
120
123
} ;
121
124
} ;
122
125
0 commit comments