@@ -11,10 +11,6 @@ export function parseScriptSetupRanges(
11
11
ast : ts . SourceFile ,
12
12
vueCompilerOptions : VueCompilerOptions
13
13
) {
14
-
15
- let foundNonImportExportNode = false ;
16
- let importSectionEndOffset = 0 ;
17
-
18
14
const props : {
19
15
name ?: string ;
20
16
destructured ?: Set < string > ;
@@ -76,6 +72,8 @@ export function parseScriptSetupRanges(
76
72
) ?. end ?? 0 ;
77
73
78
74
let bindings = parseBindingRanges ( ts , ast ) ;
75
+ let foundNonImportExportNode = false ;
76
+ let importSectionEndOffset = 0 ;
79
77
80
78
ts . forEachChild ( ast , node => {
81
79
const isTypeExport = ( ts . isTypeAliasDeclaration ( node ) || ts . isInterfaceDeclaration ( node ) ) && node . modifiers ?. some ( mod => mod . kind === ts . SyntaxKind . ExportKeyword ) ;
@@ -103,9 +101,9 @@ export function parseScriptSetupRanges(
103
101
&& node . importClause ?. name
104
102
&& ! node . importClause . isTypeOnly
105
103
) {
106
- const moduleName = getNodeText ( ts , node . moduleSpecifier , ast ) . slice ( 1 , - 1 ) ;
104
+ const moduleName = _getNodeText ( node . moduleSpecifier ) . slice ( 1 , - 1 ) ;
107
105
if ( vueCompilerOptions . extensions . some ( ext => moduleName . endsWith ( ext ) ) ) {
108
- importComponentNames . add ( getNodeText ( ts , node . importClause . name , ast ) ) ;
106
+ importComponentNames . add ( _getNodeText ( node . importClause . name ) ) ;
109
107
}
110
108
}
111
109
} ) ;
@@ -132,26 +130,13 @@ export function parseScriptSetupRanges(
132
130
templateRefs,
133
131
} ;
134
132
135
- function _getStartEnd ( node : ts . Node ) {
136
- return getStartEnd ( ts , node , ast ) ;
137
- }
138
-
139
- function parseDefineFunction ( node : ts . CallExpression ) {
140
- return {
141
- ..._getStartEnd ( node ) ,
142
- exp : _getStartEnd ( node . expression ) ,
143
- arg : node . arguments . length ? _getStartEnd ( node . arguments [ 0 ] ) : undefined ,
144
- typeArg : node . typeArguments ?. length ? _getStartEnd ( node . typeArguments [ 0 ] ) : undefined ,
145
- } ;
146
- }
147
-
148
133
function visitNode ( node : ts . Node , parents : ts . Node [ ] ) {
149
134
const parent = parents [ parents . length - 1 ] ;
150
135
if (
151
136
ts . isCallExpression ( node )
152
137
&& ts . isIdentifier ( node . expression )
153
138
) {
154
- const callText = getNodeText ( ts , node . expression , ast ) ;
139
+ const callText = _getNodeText ( node . expression ) ;
155
140
if ( vueCompilerOptions . macros . defineModel . includes ( callText ) ) {
156
141
let localName : TextRange | undefined ;
157
142
let propName : TextRange | undefined ;
@@ -169,7 +154,7 @@ export function parseScriptSetupRanges(
169
154
options = node . arguments [ 1 ] ;
170
155
}
171
156
else if ( node . arguments . length >= 1 ) {
172
- if ( ts . isStringLiteral ( node . arguments [ 0 ] ) ) {
157
+ if ( ts . isStringLiteralLike ( node . arguments [ 0 ] ) ) {
173
158
propName = _getStartEnd ( node . arguments [ 0 ] ) ;
174
159
}
175
160
else {
@@ -185,7 +170,7 @@ export function parseScriptSetupRanges(
185
170
if ( ! ts . isPropertyAssignment ( property ) || ! ts . isIdentifier ( property . name ) ) {
186
171
continue ;
187
172
}
188
- const text = getNodeText ( ts , property . name , ast ) ;
173
+ const text = _getNodeText ( property . name ) ;
189
174
if ( text === 'type' ) {
190
175
runtimeType = _getStartEnd ( property . initializer ) ;
191
176
}
@@ -236,7 +221,7 @@ export function parseScriptSetupRanges(
236
221
if ( ! ts . isPropertyAssignment ( property ) || ! ts . isIdentifier ( property . name ) ) {
237
222
continue ;
238
223
}
239
- const text = getNodeText ( ts , property . name , ast ) ;
224
+ const text = _getNodeText ( property . name ) ;
240
225
if ( text === 'type' ) {
241
226
runtimeType = _getStartEnd ( property . initializer ) ;
242
227
}
@@ -267,7 +252,7 @@ export function parseScriptSetupRanges(
267
252
if ( ! ts . isPropertyAssignment ( property ) || ! ts . isIdentifier ( property . name ) ) {
268
253
continue ;
269
254
}
270
- const text = getNodeText ( ts , property . name , ast ) ;
255
+ const text = _getNodeText ( property . name ) ;
271
256
if ( text === 'type' ) {
272
257
runtimeType = _getStartEnd ( property . initializer ) ;
273
258
}
@@ -291,7 +276,7 @@ export function parseScriptSetupRanges(
291
276
} ;
292
277
if ( ts . isVariableDeclaration ( parent ) ) {
293
278
if ( ts . isIdentifier ( parent . name ) ) {
294
- slots . name = getNodeText ( ts , parent . name , ast ) ;
279
+ slots . name = _getNodeText ( parent . name ) ;
295
280
}
296
281
else {
297
282
slots . isObjectBindingPattern = ts . isObjectBindingPattern ( parent . name ) ;
@@ -304,7 +289,7 @@ export function parseScriptSetupRanges(
304
289
statement : getStatementRange ( ts , parents , node , ast )
305
290
} ;
306
291
if ( ts . isVariableDeclaration ( parent ) ) {
307
- emits . name = getNodeText ( ts , parent . name , ast ) ;
292
+ emits . name = _getNodeText ( parent . name ) ;
308
293
}
309
294
if ( node . typeArguments ?. length && ts . isTypeLiteralNode ( node . typeArguments [ 0 ] ) ) {
310
295
for ( const member of node . typeArguments [ 0 ] . members ) {
@@ -327,7 +312,7 @@ export function parseScriptSetupRanges(
327
312
props . destructured = new Set ( ) ;
328
313
const identifiers = collectIdentifiers ( ts , parent . name , [ ] ) ;
329
314
for ( const [ id , isRest ] of identifiers ) {
330
- const name = getNodeText ( ts , id , ast ) ;
315
+ const name = _getNodeText ( id ) ;
331
316
if ( isRest ) {
332
317
props . destructuredRest = name ;
333
318
}
@@ -337,7 +322,7 @@ export function parseScriptSetupRanges(
337
322
}
338
323
}
339
324
else {
340
- props . name = getNodeText ( ts , parent . name , ast ) ;
325
+ props . name = _getNodeText ( parent . name ) ;
341
326
}
342
327
}
343
328
@@ -360,33 +345,39 @@ export function parseScriptSetupRanges(
360
345
props . withDefaults . arg = _getStartEnd ( arg ) ;
361
346
}
362
347
if ( ts . isVariableDeclaration ( parent ) ) {
363
- props . name = getNodeText ( ts , parent . name , ast ) ;
348
+ props . name = _getNodeText ( parent . name ) ;
364
349
}
365
350
}
366
- else if ( vueCompilerOptions . macros . defineOptions . includes ( callText ) ) {
367
- if ( node . arguments . length && ts . isObjectLiteralExpression ( node . arguments [ 0 ] ) ) {
368
- const obj = node . arguments [ 0 ] ;
369
- ts . forEachChild ( obj , node => {
370
- if ( ts . isPropertyAssignment ( node ) && ts . isIdentifier ( node . name ) ) {
371
- const name = getNodeText ( ts , node . name , ast ) ;
372
- if ( name === 'inheritAttrs' ) {
373
- options . inheritAttrs = getNodeText ( ts , node . initializer , ast ) ;
374
- }
375
- }
376
- } ) ;
377
- for ( const prop of node . arguments [ 0 ] . properties ) {
378
- if ( ts . isPropertyAssignment ( prop ) && getNodeText ( ts , prop . name , ast ) === 'name' && ts . isStringLiteral ( prop . initializer ) ) {
379
- options . name = prop . initializer . text ;
351
+ else if (
352
+ vueCompilerOptions . macros . defineOptions . includes ( callText )
353
+ && node . arguments . length
354
+ && ts . isObjectLiteralExpression ( node . arguments [ 0 ] )
355
+ ) {
356
+ const obj = node . arguments [ 0 ] ;
357
+ ts . forEachChild ( obj , node => {
358
+ if ( ts . isPropertyAssignment ( node ) && ts . isIdentifier ( node . name ) ) {
359
+ const name = _getNodeText ( node . name ) ;
360
+ if ( name === 'inheritAttrs' ) {
361
+ options . inheritAttrs = _getNodeText ( node . initializer ) ;
380
362
}
381
363
}
364
+ } ) ;
365
+ for ( const prop of obj . properties ) {
366
+ if (
367
+ ts . isPropertyAssignment ( prop )
368
+ && _getNodeText ( prop . name ) === 'name' && ts . isStringLiteral ( prop . initializer )
369
+ ) {
370
+ options . name = prop . initializer . text ;
371
+ }
382
372
}
383
373
}
384
- else if ( vueCompilerOptions . composables . useTemplateRef . includes ( callText ) && node . arguments . length && ! node . typeArguments ?. length ) {
374
+ else if (
375
+ vueCompilerOptions . composables . useTemplateRef . includes ( callText )
376
+ && node . arguments . length
377
+ && ! node . typeArguments ?. length
378
+ ) {
385
379
const define = parseDefineFunction ( node ) ;
386
- let name ;
387
- if ( ts . isVariableDeclaration ( parent ) ) {
388
- name = getNodeText ( ts , parent . name , ast ) ;
389
- }
380
+ const name = ts . isVariableDeclaration ( parent ) ? _getNodeText ( parent . name ) : undefined ;
390
381
templateRefs . push ( {
391
382
name,
392
383
define
@@ -399,23 +390,39 @@ export function parseScriptSetupRanges(
399
390
} ) ;
400
391
}
401
392
}
393
+
402
394
ts . forEachChild ( node , child => {
403
395
parents . push ( node ) ;
404
396
visitNode ( child , parents ) ;
405
397
parents . pop ( ) ;
406
398
} ) ;
407
399
}
400
+
401
+ function parseDefineFunction ( node : ts . CallExpression ) {
402
+ return {
403
+ ..._getStartEnd ( node ) ,
404
+ exp : _getStartEnd ( node . expression ) ,
405
+ arg : node . arguments . length ? _getStartEnd ( node . arguments [ 0 ] ) : undefined ,
406
+ typeArg : node . typeArguments ?. length ? _getStartEnd ( node . typeArguments [ 0 ] ) : undefined ,
407
+ } ;
408
+ }
409
+
410
+ function _getStartEnd ( node : ts . Node ) {
411
+ return getStartEnd ( ts , node , ast ) ;
412
+ }
413
+
414
+ function _getNodeText ( node : ts . Node ) {
415
+ return getNodeText ( ts , node , ast ) ;
416
+ }
408
417
}
409
418
410
419
export function parseBindingRanges ( ts : typeof import ( 'typescript' ) , sourceFile : ts . SourceFile ) {
411
420
const bindings : TextRange [ ] = [ ] ;
412
421
ts . forEachChild ( sourceFile , node => {
413
422
if ( ts . isVariableStatement ( node ) ) {
414
- for ( const node_2 of node . declarationList . declarations ) {
415
- const vars = _findBindingVars ( node_2 . name ) ;
416
- for ( const _var of vars ) {
417
- bindings . push ( _var ) ;
418
- }
423
+ for ( const decl of node . declarationList . declarations ) {
424
+ const vars = _findBindingVars ( decl . name ) ;
425
+ bindings . push ( ...vars ) ;
419
426
}
420
427
}
421
428
else if ( ts . isFunctionDeclaration ( node ) ) {
@@ -454,43 +461,49 @@ export function parseBindingRanges(ts: typeof import('typescript'), sourceFile:
454
461
}
455
462
} ) ;
456
463
return bindings ;
464
+
457
465
function _getStartEnd ( node : ts . Node ) {
458
466
return getStartEnd ( ts , node , sourceFile ) ;
459
467
}
468
+
460
469
function _findBindingVars ( left : ts . BindingName ) {
461
470
return findBindingVars ( ts , left , sourceFile ) ;
462
471
}
463
472
}
464
473
465
- export function findBindingVars ( ts : typeof import ( 'typescript' ) , left : ts . BindingName , sourceFile : ts . SourceFile ) {
474
+ export function findBindingVars (
475
+ ts : typeof import ( 'typescript' ) ,
476
+ left : ts . BindingName ,
477
+ sourceFile : ts . SourceFile
478
+ ) {
466
479
const vars : TextRange [ ] = [ ] ;
467
480
worker ( left ) ;
468
481
return vars ;
469
- function worker ( _node : ts . Node ) {
470
- if ( ts . isIdentifier ( _node ) ) {
471
- vars . push ( getStartEnd ( ts , _node , sourceFile ) ) ;
482
+ function worker ( node : ts . Node ) {
483
+ if ( ts . isIdentifier ( node ) ) {
484
+ vars . push ( getStartEnd ( ts , node , sourceFile ) ) ;
472
485
}
473
486
// { ? } = ...
474
487
// [ ? ] = ...
475
- else if ( ts . isObjectBindingPattern ( _node ) || ts . isArrayBindingPattern ( _node ) ) {
476
- for ( const property of _node . elements ) {
488
+ else if ( ts . isObjectBindingPattern ( node ) || ts . isArrayBindingPattern ( node ) ) {
489
+ for ( const property of node . elements ) {
477
490
if ( ts . isBindingElement ( property ) ) {
478
491
worker ( property . name ) ;
479
492
}
480
493
}
481
494
}
482
495
// { foo: ? } = ...
483
- else if ( ts . isPropertyAssignment ( _node ) ) {
484
- worker ( _node . initializer ) ;
496
+ else if ( ts . isPropertyAssignment ( node ) ) {
497
+ worker ( node . initializer ) ;
485
498
}
486
499
// { foo } = ...
487
- else if ( ts . isShorthandPropertyAssignment ( _node ) ) {
488
- vars . push ( getStartEnd ( ts , _node . name , sourceFile ) ) ;
500
+ else if ( ts . isShorthandPropertyAssignment ( node ) ) {
501
+ vars . push ( getStartEnd ( ts , node . name , sourceFile ) ) ;
489
502
}
490
503
// { ...? } = ...
491
504
// [ ...? ] = ...
492
- else if ( ts . isSpreadAssignment ( _node ) || ts . isSpreadElement ( _node ) ) {
493
- worker ( _node . expression ) ;
505
+ else if ( ts . isSpreadAssignment ( node ) || ts . isSpreadElement ( node ) ) {
506
+ worker ( node . expression ) ;
494
507
}
495
508
}
496
509
}
@@ -537,4 +550,4 @@ function getStatementRange(
537
550
statementRange = getStartEnd ( ts , node , sourceFile ) ;
538
551
}
539
552
return statementRange ;
540
- }
553
+ }
0 commit comments