44 NodeTypes ,
55 type SimpleExpressionNode ,
66 type TemplateChildNode ,
7+ isSimpleIdentifier ,
78 parserOptions ,
89 walkIdentifiers ,
910} from '@vue/compiler-dom'
@@ -19,16 +20,35 @@ export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
1920 return resolveTemplateUsedIdentifiers ( sfc ) . has ( local )
2021}
2122
22- const templateUsageCheckCache = createCache < Set < string > > ( )
23+ export function resolveTemplateVModelIdentifiers (
24+ sfc : SFCDescriptor ,
25+ ) : Set < string > {
26+ const template = sfc . template
27+ if ( ! template ?. ast ) return new Set < string > ( )
28+ return resolveTemplateAnalysisResult ( sfc ) . vModelIds
29+ }
30+
31+ const templateAnalysisCache = createCache < {
32+ usedIds : Set < string >
33+ vModelIds : Set < string >
34+ } > ( )
2335
2436function resolveTemplateUsedIdentifiers ( sfc : SFCDescriptor ) : Set < string > {
37+ return resolveTemplateAnalysisResult ( sfc ) . usedIds
38+ }
39+
40+ function resolveTemplateAnalysisResult ( sfc : SFCDescriptor ) : {
41+ usedIds : Set < string >
42+ vModelIds : Set < string >
43+ } {
2544 const { content, ast } = sfc . template !
26- const cached = templateUsageCheckCache . get ( content )
45+ const cached = templateAnalysisCache . get ( content )
2746 if ( cached ) {
2847 return cached
2948 }
3049
3150 const ids = new Set < string > ( )
51+ const vModelIds = new Set < string > ( )
3252
3353 ast ! . children . forEach ( walk )
3454
@@ -51,6 +71,20 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
5171 ids . add ( `v${ capitalize ( camelize ( prop . name ) ) } ` )
5272 }
5373
74+ // collect v-model target identifiers (simple identifiers only)
75+ if ( prop . name === 'model' ) {
76+ const exp = prop . exp
77+ if ( exp && exp . type === NodeTypes . SIMPLE_EXPRESSION ) {
78+ const expString = exp . content . trim ( )
79+ if (
80+ isSimpleIdentifier ( expString ) &&
81+ expString !== 'undefined'
82+ ) {
83+ vModelIds . add ( expString )
84+ }
85+ }
86+ }
87+
5488 // process dynamic directive arguments
5589 if ( prop . arg && ! ( prop . arg as SimpleExpressionNode ) . isStatic ) {
5690 extractIdentifiers ( ids , prop . arg )
@@ -81,8 +115,9 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
81115 }
82116 }
83117
84- templateUsageCheckCache . set ( content , ids )
85- return ids
118+ const result = { usedIds : ids , vModelIds }
119+ templateAnalysisCache . set ( content , result )
120+ return result
86121}
87122
88123function extractIdentifiers ( ids : Set < string > , node : ExpressionNode ) {
0 commit comments