@@ -69,67 +69,74 @@ export default plugin;
69
69
70
70
function createTsx (
71
71
fileName : string ,
72
- _sfc : Sfc ,
72
+ sfc : Sfc ,
73
73
ctx : Parameters < VueLanguagePlugin > [ 0 ] ,
74
74
appendGlobalTypes : boolean
75
75
) {
76
76
const ts = ctx . modules . typescript ;
77
+
77
78
const getLang = computed ( ( ) => {
78
- return ! _sfc . script && ! _sfc . scriptSetup ? 'ts'
79
- : _sfc . scriptSetup && _sfc . scriptSetup . lang !== 'js' ? _sfc . scriptSetup . lang
80
- : _sfc . script && _sfc . script . lang !== 'js' ? _sfc . script . lang
79
+ return ! sfc . script && ! sfc . scriptSetup ? 'ts'
80
+ : sfc . scriptSetup && sfc . scriptSetup . lang !== 'js' ? sfc . scriptSetup . lang
81
+ : sfc . script && sfc . script . lang !== 'js' ? sfc . script . lang
81
82
: 'js' ;
82
83
} ) ;
84
+
83
85
const getResolvedOptions = computed ( ( ) => {
84
- const options = parseVueCompilerOptions ( _sfc . comments ) ;
86
+ const options = parseVueCompilerOptions ( sfc . comments ) ;
85
87
if ( options ) {
86
88
const resolver = new CompilerOptionsResolver ( ) ;
87
89
resolver . addConfig ( options , path . dirname ( fileName ) ) ;
88
90
return resolver . build ( ctx . vueCompilerOptions ) ;
89
91
}
90
92
return ctx . vueCompilerOptions ;
91
93
} ) ;
94
+
92
95
const getScriptRanges = computed ( ( ) =>
93
- _sfc . script
94
- ? parseScriptRanges ( ts , _sfc . script . ast , ! ! _sfc . scriptSetup , false )
96
+ sfc . script
97
+ ? parseScriptRanges ( ts , sfc . script . ast , ! ! sfc . scriptSetup , false )
95
98
: undefined
96
99
) ;
100
+
97
101
const getScriptSetupRanges = computed ( ( ) =>
98
- _sfc . scriptSetup
99
- ? parseScriptSetupRanges ( ts , _sfc . scriptSetup . ast , getResolvedOptions ( ) )
102
+ sfc . scriptSetup
103
+ ? parseScriptSetupRanges ( ts , sfc . scriptSetup . ast , getResolvedOptions ( ) )
100
104
: undefined
101
105
) ;
106
+
102
107
const getSetupBindingNames = computedSet (
103
108
computed ( ( ) => {
104
109
const newNames = new Set < string > ( ) ;
105
110
const bindings = getScriptSetupRanges ( ) ?. bindings ;
106
- if ( _sfc . scriptSetup && bindings ) {
111
+ if ( sfc . scriptSetup && bindings ) {
107
112
for ( const { range } of bindings ) {
108
- newNames . add ( _sfc . scriptSetup . content . slice ( range . start , range . end ) ) ;
113
+ newNames . add ( sfc . scriptSetup . content . slice ( range . start , range . end ) ) ;
109
114
}
110
115
}
111
116
return newNames ;
112
117
} )
113
118
) ;
119
+
114
120
const getSetupImportComponentNames = computedSet (
115
121
computed ( ( ) => {
116
122
const newNames = new Set < string > ( ) ;
117
123
const bindings = getScriptSetupRanges ( ) ?. bindings ;
118
- if ( _sfc . scriptSetup && bindings ) {
124
+ if ( sfc . scriptSetup && bindings ) {
119
125
for ( const { range, moduleName, isDefaultImport, isNamespace } of bindings ) {
120
126
if (
121
127
moduleName
122
128
&& isDefaultImport
123
129
&& ! isNamespace
124
130
&& ctx . vueCompilerOptions . extensions . some ( ext => moduleName . endsWith ( ext ) )
125
131
) {
126
- newNames . add ( _sfc . scriptSetup . content . slice ( range . start , range . end ) ) ;
132
+ newNames . add ( sfc . scriptSetup . content . slice ( range . start , range . end ) ) ;
127
133
}
128
134
}
129
135
}
130
136
return newNames ;
131
137
} )
132
138
) ;
139
+
133
140
const getSetupDestructuredPropNames = computedSet (
134
141
computed ( ( ) => {
135
142
const newNames = new Set ( getScriptSetupRanges ( ) ?. defineProps ?. destructured ?. keys ( ) ) ;
@@ -140,6 +147,7 @@ function createTsx(
140
147
return newNames ;
141
148
} )
142
149
) ;
150
+
143
151
const getSetupTemplateRefNames = computedSet (
144
152
computed ( ( ) => {
145
153
const newNames = new Set (
@@ -150,29 +158,52 @@ function createTsx(
150
158
return newNames ;
151
159
} )
152
160
) ;
161
+
153
162
const setupHasDefineSlots = computed ( ( ) => ! ! getScriptSetupRanges ( ) ?. defineSlots ) ;
163
+
154
164
const getSetupSlotsAssignName = computed ( ( ) => getScriptSetupRanges ( ) ?. defineSlots ?. name ) ;
165
+
155
166
const getSetupPropsAssignName = computed ( ( ) => getScriptSetupRanges ( ) ?. defineProps ?. name ) ;
167
+
156
168
const getSetupInheritAttrs = computed ( ( ) => {
157
169
const value = getScriptSetupRanges ( ) ?. defineOptions ?. inheritAttrs ?? getScriptRanges ( ) ?. exportDefault ?. inheritAttrsOption ;
158
170
return value !== 'false' ;
159
171
} ) ;
172
+
173
+ const getSetupSlotsReferenceName = computedSet (
174
+ computed ( ( ) => {
175
+ const newNames = new Set (
176
+ getScriptSetupRanges ( ) ?. useSlots . map ( ( { name } ) => name ) . filter ( name => name !== undefined )
177
+ ) ;
178
+ return newNames ;
179
+ } )
180
+ ) ;
181
+
182
+ const getSetupAttrsReferenceName = computedSet (
183
+ computed ( ( ) => {
184
+ const newNames = new Set (
185
+ getScriptSetupRanges ( ) ?. useAttrs . map ( ( { name } ) => name ) . filter ( name => name !== undefined )
186
+ ) ;
187
+ return newNames ;
188
+ } )
189
+ ) ;
190
+
160
191
const getComponentSelfName = computed ( ( ) => {
161
192
const { exportDefault } = getScriptRanges ( ) ?? { } ;
162
- if ( _sfc . script && exportDefault ?. nameOption ) {
193
+ if ( sfc . script && exportDefault ?. nameOption ) {
163
194
const { nameOption } = exportDefault ;
164
- return _sfc . script . content . slice ( nameOption . start + 1 , nameOption . end - 1 ) ;
195
+ return sfc . script . content . slice ( nameOption . start + 1 , nameOption . end - 1 ) ;
165
196
}
166
197
const { defineOptions } = getScriptSetupRanges ( ) ?? { } ;
167
- if ( _sfc . scriptSetup && defineOptions ?. name ) {
198
+ if ( sfc . scriptSetup && defineOptions ?. name ) {
168
199
return defineOptions . name ;
169
200
}
170
201
const baseName = path . basename ( fileName ) ;
171
202
return capitalize ( camelize ( baseName . slice ( 0 , baseName . lastIndexOf ( '.' ) ) ) ) ;
172
203
} ) ;
173
- const getGeneratedTemplate = computed ( ( ) => {
174
204
175
- if ( getResolvedOptions ( ) . skipTemplateCodegen || ! _sfc . template ) {
205
+ const getGeneratedTemplate = computed ( ( ) => {
206
+ if ( getResolvedOptions ( ) . skipTemplateCodegen || ! sfc . template ) {
176
207
return ;
177
208
}
178
209
@@ -181,7 +212,7 @@ function createTsx(
181
212
ts,
182
213
compilerOptions : ctx . compilerOptions ,
183
214
vueCompilerOptions : getResolvedOptions ( ) ,
184
- template : _sfc . template ,
215
+ template : sfc . template ,
185
216
edited : getResolvedOptions ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
186
217
scriptSetupBindingNames : getSetupBindingNames ( ) ,
187
218
scriptSetupImportComponentNames : getSetupImportComponentNames ( ) ,
@@ -190,12 +221,13 @@ function createTsx(
190
221
hasDefineSlots : setupHasDefineSlots ( ) ,
191
222
slotsAssignName : getSetupSlotsAssignName ( ) ,
192
223
propsAssignName : getSetupPropsAssignName ( ) ,
224
+ slotsReferenceNames : getSetupSlotsReferenceName ( ) ,
225
+ attrsReferenceNames : getSetupAttrsReferenceName ( ) ,
193
226
inheritAttrs : getSetupInheritAttrs ( ) ,
194
227
selfComponentName : getComponentSelfName ( ) ,
195
228
} ) ;
196
229
197
230
let current = codegen . next ( ) ;
198
-
199
231
while ( ! current . done ) {
200
232
const code = current . value ;
201
233
codes . push ( code ) ;
@@ -207,15 +239,17 @@ function createTsx(
207
239
codes,
208
240
} ;
209
241
} ) ;
242
+
210
243
const getGeneratedScript = computed ( ( ) => {
211
- const codes : Code [ ] = [ ] ;
212
244
const linkedCodeMappings : Mapping [ ] = [ ] ;
213
245
let generatedLength = 0 ;
246
+
247
+ const codes : Code [ ] = [ ] ;
214
248
const codegen = generateScript ( {
215
249
ts,
216
250
compilerOptions : ctx . compilerOptions ,
217
251
vueCompilerOptions : getResolvedOptions ( ) ,
218
- sfc : _sfc ,
252
+ sfc : sfc ,
219
253
edited : getResolvedOptions ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
220
254
fileName,
221
255
lang : getLang ( ) ,
@@ -231,7 +265,6 @@ function createTsx(
231
265
fileEditTimes . set ( fileName , ( fileEditTimes . get ( fileName ) ?? 0 ) + 1 ) ;
232
266
233
267
let current = codegen . next ( ) ;
234
-
235
268
while ( ! current . done ) {
236
269
const code = current . value ;
237
270
codes . push ( code ) ;
0 commit comments