11import * as t from '@babel/types' ;
22import { NodePath } from '@babel/traverse' ;
33import { createIdentifier } from './utils' ;
4- import { State , ExcludesBoolean } from '.' ;
4+ import { State } from '.' ;
55
66export type Tag = t . Identifier | t . MemberExpression | t . StringLiteral | t . CallExpression ;
77
@@ -24,7 +24,7 @@ const getType = (path: NodePath<t.JSXOpeningElement>) => {
2424 return typePath ? typePath . get ( 'value' ) . node : null ;
2525} ;
2626
27- const parseModifiers = ( value : t . ArrayExpression ) : string [ ] => (
27+ const parseModifiers = ( value : any ) : string [ ] => (
2828 t . isArrayExpression ( value )
2929 ? value . elements
3030 . map ( ( el ) => ( t . isStringLiteral ( el ) ? el . value : '' ) )
@@ -64,37 +64,38 @@ const parseDirectives = (params: {
6464 const shouldResolve = ! [ 'html' , 'text' , 'model' , 'models' ] . includes ( directiveName )
6565 || ( isVModel && ! isComponent ) ;
6666
67- if ( [ 'models' , 'model' ] . includes ( directiveName ) ) {
68- if ( t . isArrayExpression ( value ) ) {
69- const elementsList = isVModels ? value . elements ! : [ value ] ;
67+ let modifiers = underscoreModifiers ;
7068
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 ] ;
7571
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+ }
7976
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 ) {
8885 args . push ( t . nullLiteral ( ) ) ;
8986 }
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 ) ) ;
9899 } else {
99100 modifiersSet . push ( new Set ( underscoreModifiers ) ) ;
100101 }
@@ -107,7 +108,9 @@ const parseDirectives = (params: {
107108 directive : shouldResolve ? [
108109 resolveDirective ( path , state , tag , directiveName ) ,
109110 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 ] ,
111114 ! ! modifiersSet [ 0 ] ?. size && t . objectExpression (
112115 [ ...modifiersSet [ 0 ] ] . map (
113116 ( modifier ) => t . objectProperty (
@@ -116,7 +119,7 @@ const parseDirectives = (params: {
116119 ) ,
117120 ) ,
118121 ) ,
119- ] . filter ( Boolean as any as ExcludesBoolean ) : undefined ,
122+ ] . filter ( Boolean ) as t . Expression [ ] : undefined ,
120123 } ;
121124} ;
122125
0 commit comments