@@ -7,6 +7,8 @@ import { generateCamelized } from './camelized';
7
7
import type { TemplateCodegenContext } from './context' ;
8
8
import type { TemplateCodegenOptions } from './index' ;
9
9
import { generateInterpolation } from './interpolation' ;
10
+ import { generateObjectProperty } from './objectProperty' ;
11
+ import { generateStringLiteralKey } from './stringLiteralKey' ;
10
12
11
13
export function * generateElementDirectives (
12
14
options : TemplateCodegenOptions ,
@@ -15,85 +17,158 @@ export function* generateElementDirectives(
15
17
) : Generator < Code > {
16
18
for ( const prop of node . props ) {
17
19
if (
18
- prop . type = == CompilerDOM . NodeTypes . DIRECTIVE
19
- && prop . name ! == 'slot'
20
- && prop . name ! == 'on'
21
- && prop . name ! == 'model'
22
- && prop . name ! == 'bind'
23
- && prop . name ! == 'scope'
24
- && prop . name ! == 'data'
20
+ prop . type ! == CompilerDOM . NodeTypes . DIRECTIVE
21
+ || prop . name = == 'slot'
22
+ || prop . name = == 'on'
23
+ || prop . name = == 'model'
24
+ || prop . name = == 'bind'
25
+ || prop . name = == 'scope'
26
+ || prop . name = == 'data'
25
27
) {
26
- ctx . accessExternalVariable ( camelize ( 'v-' + prop . name ) , prop . loc . start . offset ) ;
28
+ continue ;
29
+ }
30
+ ctx . accessExternalVariable ( camelize ( 'v-' + prop . name ) , prop . loc . start . offset ) ;
31
+
32
+ yield * wrapWith (
33
+ prop . loc . start . offset ,
34
+ prop . loc . end . offset ,
35
+ ctx . codeFeatures . verification ,
36
+ `__VLS_asFunctionalDirective(` ,
37
+ ...generateIdentifier ( ctx , prop ) ,
38
+ `)(null!, { ...__VLS_directiveBindingRestFields, ` ,
39
+ ...generateArg ( options , ctx , prop ) ,
40
+ ...generateModifiers ( options , ctx , prop ) ,
41
+ ...generateValue ( options , ctx , prop ) ,
42
+ `}, null!, null!)`
43
+ ) ;
44
+ yield endOfLine ;
45
+ }
46
+ }
27
47
28
- if ( prop . arg ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION && ! prop . arg . isStatic ) {
29
- yield * generateInterpolation (
30
- options ,
31
- ctx ,
32
- prop . arg . content ,
33
- prop . arg . loc ,
34
- prop . arg . loc . start . offset + prop . arg . loc . source . indexOf ( prop . arg . content ) ,
35
- ctx . codeFeatures . all ,
36
- '(' ,
37
- ')'
38
- ) ;
39
- yield endOfLine ;
48
+ function * generateIdentifier (
49
+ ctx : TemplateCodegenContext ,
50
+ prop : CompilerDOM . DirectiveNode
51
+ ) : Generator < Code > {
52
+ const rawName = 'v-' + prop . name ;
53
+ yield * wrapWith (
54
+ prop . loc . start . offset ,
55
+ prop . loc . start . offset + rawName . length ,
56
+ ctx . codeFeatures . verification ,
57
+ `__VLS_directives.` ,
58
+ ...generateCamelized (
59
+ rawName ,
60
+ prop . loc . start . offset ,
61
+ {
62
+ ...ctx . codeFeatures . all ,
63
+ verification : false ,
64
+ completion : {
65
+ // fix https://github.com/vuejs/language-tools/issues/1905
66
+ isAdditional : true ,
67
+ } ,
68
+ navigation : {
69
+ resolveRenameNewName : camelize ,
70
+ resolveRenameEditText : getPropRenameApply ( prop . name ) ,
71
+ } ,
40
72
}
73
+ )
74
+ ) ;
75
+ }
41
76
42
- yield * wrapWith (
43
- prop . loc . start . offset ,
44
- prop . loc . end . offset ,
45
- ctx . codeFeatures . verification ,
46
- `__VLS_directiveAsFunction(__VLS_directives.` ,
47
- ...generateCamelized (
48
- 'v-' + prop . name ,
49
- prop . loc . start . offset ,
50
- {
51
- ...ctx . codeFeatures . all ,
52
- verification : false ,
53
- completion : {
54
- // fix https://github.com/vuejs/language-tools/issues/1905
55
- isAdditional : true ,
56
- } ,
57
- navigation : {
58
- resolveRenameNewName : camelize ,
59
- resolveRenameEditText : getPropRenameApply ( prop . name ) ,
60
- } ,
61
- }
62
- ) ,
63
- `)(null!, { ...__VLS_directiveBindingRestFields, ` ,
64
- ...(
65
- prop . exp ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION
66
- ? [
67
- ...wrapWith (
68
- prop . exp . loc . start . offset ,
69
- prop . exp . loc . end . offset ,
70
- ctx . codeFeatures . verification ,
71
- 'value'
72
- ) ,
73
- ': ' ,
74
- ...wrapWith (
75
- prop . exp . loc . start . offset ,
76
- prop . exp . loc . end . offset ,
77
- ctx . codeFeatures . verification ,
78
- ...generateInterpolation (
79
- options ,
80
- ctx ,
81
- prop . exp . content ,
82
- prop . exp . loc ,
83
- prop . exp . loc . start . offset ,
84
- ctx . codeFeatures . all ,
85
- '(' ,
86
- ')'
87
- )
88
- )
89
- ]
90
- : [ `undefined` ]
91
- ) ,
92
- `}, null!, null!)`
93
- ) ;
94
- yield endOfLine ;
95
- }
77
+ function * generateArg (
78
+ options : TemplateCodegenOptions ,
79
+ ctx : TemplateCodegenContext ,
80
+ prop : CompilerDOM . DirectiveNode
81
+ ) : Generator < Code > {
82
+ const { arg } = prop ;
83
+ if ( arg ?. type !== CompilerDOM . NodeTypes . SIMPLE_EXPRESSION ) {
84
+ return ;
85
+ }
86
+
87
+ const startOffset = arg . loc . start . offset + arg . loc . source . indexOf ( arg . content ) ;
88
+
89
+ yield * wrapWith (
90
+ startOffset ,
91
+ startOffset + arg . content . length ,
92
+ ctx . codeFeatures . verification ,
93
+ 'arg'
94
+ ) ;
95
+ yield ': ' ;
96
+ if ( arg . isStatic ) {
97
+ yield * generateStringLiteralKey (
98
+ arg . content ,
99
+ startOffset ,
100
+ ctx . codeFeatures . withoutHighlight
101
+ ) ;
102
+ }
103
+ else {
104
+ yield * generateInterpolation (
105
+ options ,
106
+ ctx ,
107
+ arg . content ,
108
+ arg . loc ,
109
+ startOffset ,
110
+ ctx . codeFeatures . all ,
111
+ '(' ,
112
+ ')'
113
+ ) ;
96
114
}
115
+ yield ', ' ;
116
+ }
117
+
118
+ function * generateModifiers (
119
+ options : TemplateCodegenOptions ,
120
+ ctx : TemplateCodegenContext ,
121
+ prop : CompilerDOM . DirectiveNode
122
+ ) : Generator < Code > {
123
+ if ( options . vueCompilerOptions . target < 3.5 ) {
124
+ return ;
125
+ }
126
+
127
+ yield 'modifiers: { ' ;
128
+ for ( const mod of prop . modifiers ) {
129
+ yield * generateObjectProperty (
130
+ options ,
131
+ ctx ,
132
+ mod . content ,
133
+ mod . loc . start . offset ,
134
+ ctx . codeFeatures . withoutHighlight
135
+ ) ;
136
+ yield ': true, ' ;
137
+ }
138
+ yield '}, ' ;
139
+ }
140
+
141
+ function * generateValue (
142
+ options : TemplateCodegenOptions ,
143
+ ctx : TemplateCodegenContext ,
144
+ prop : CompilerDOM . DirectiveNode
145
+ ) : Generator < Code > {
146
+ if ( prop . exp ?. type !== CompilerDOM . NodeTypes . SIMPLE_EXPRESSION ) {
147
+ return ;
148
+ }
149
+
150
+ yield * wrapWith (
151
+ prop . exp . loc . start . offset ,
152
+ prop . exp . loc . end . offset ,
153
+ ctx . codeFeatures . verification ,
154
+ 'value'
155
+ ) ;
156
+ yield ': ' ;
157
+ yield * wrapWith (
158
+ prop . exp . loc . start . offset ,
159
+ prop . exp . loc . end . offset ,
160
+ ctx . codeFeatures . verification ,
161
+ ...generateInterpolation (
162
+ options ,
163
+ ctx ,
164
+ prop . exp . content ,
165
+ prop . exp . loc ,
166
+ prop . exp . loc . start . offset ,
167
+ ctx . codeFeatures . all ,
168
+ '(' ,
169
+ ')'
170
+ )
171
+ ) ;
97
172
}
98
173
99
174
function getPropRenameApply ( oldName : string ) {
0 commit comments