1
+ import * as ts from 'typescript' ;
1
2
import {
2
3
CallExpression ,
3
4
Decorator ,
5
+ getJSDocDeprecatedTag ,
6
+ getJSDocTags ,
7
+ getTextOfJSDocComment ,
4
8
Identifier ,
9
+ JSDoc ,
5
10
LeftHandSideExpression ,
11
+ ModifiersArray ,
6
12
Node ,
13
+ NodeArray ,
7
14
ObjectFlags ,
8
15
ObjectType ,
9
16
PropertyAccessExpression ,
@@ -12,17 +19,27 @@ import {
12
19
TypeChecker ,
13
20
TypeFlags ,
14
21
TypeFormatFlags ,
15
- UnionTypeNode ,
16
22
TypeNode ,
17
- JSDoc ,
18
- getTextOfJSDocComment ,
19
- getJSDocDeprecatedTag ,
20
- ModifiersArray ,
21
- NodeArray ,
22
- getJSDocTags ,
23
+ UnionTypeNode ,
23
24
} from 'typescript' ;
24
25
import { isDynamicallyAdded } from './plugin-utils' ;
25
- import * as ts from 'typescript' ;
26
+
27
+ const [ tsVersionMajor , tsVersionMinor ] = ts . versionMajorMinor
28
+ ?. split ( '.' )
29
+ . map ( ( x ) => + x ) ;
30
+ export const isInUpdatedAstContext = tsVersionMinor >= 8 || tsVersionMajor > 4 ;
31
+
32
+ export function getDecorators ( node : ts . Node ) {
33
+ return isInUpdatedAstContext
34
+ ? ( ts . canHaveDecorators ( node ) && ts . getDecorators ( node ) ) ?? [ ]
35
+ : node . decorators ;
36
+ }
37
+
38
+ export function getModifiers ( node : ts . Node ) {
39
+ return isInUpdatedAstContext
40
+ ? ( ts . canHaveModifiers ( node ) && ts . getModifiers ( node ) ) ?? [ ]
41
+ : node . modifiers ;
42
+ }
26
43
27
44
export function isArray ( type : Type ) {
28
45
const symbol = type . getSymbol ( ) ;
@@ -194,7 +211,7 @@ export function findNullableTypeFromUnion(
194
211
}
195
212
196
213
export function hasModifiers (
197
- modifiers : ModifiersArray ,
214
+ modifiers : ModifiersArray | readonly ts . Modifier [ ] ,
198
215
toCheck : SyntaxKind [ ] ,
199
216
) : boolean {
200
217
if ( ! modifiers ) {
@@ -204,7 +221,7 @@ export function hasModifiers(
204
221
}
205
222
206
223
export function hasDecorators (
207
- decorators : NodeArray < Decorator > ,
224
+ decorators : NodeArray < Decorator > | readonly Decorator [ ] ,
208
225
toCheck : string [ ] ,
209
226
) : boolean {
210
227
if ( ! decorators ) {
@@ -241,15 +258,22 @@ export function createImportEquals(
241
258
) : ts . ImportEqualsDeclaration {
242
259
const [ major , minor ] = ts . versionMajorMinor ?. split ( '.' ) . map ( ( x ) => + x ) ;
243
260
244
- if ( major = = 4 && minor >= 2 ) {
261
+ if ( major > = 4 && minor >= 2 ) {
245
262
// support TS v4.2+
246
- return f . createImportEqualsDeclaration (
247
- undefined ,
248
- undefined ,
249
- false ,
250
- identifier ,
251
- f . createExternalModuleReference ( f . createStringLiteral ( from ) ) ,
252
- ) ;
263
+ return minor >= 8
264
+ ? f . createImportEqualsDeclaration (
265
+ undefined ,
266
+ false ,
267
+ identifier ,
268
+ f . createExternalModuleReference ( f . createStringLiteral ( from ) ) ,
269
+ )
270
+ : f . createImportEqualsDeclaration (
271
+ undefined ,
272
+ undefined ,
273
+ false ,
274
+ identifier ,
275
+ f . createExternalModuleReference ( f . createStringLiteral ( from ) ) ,
276
+ ) ;
253
277
}
254
278
return ( f . createImportEqualsDeclaration as any ) (
255
279
undefined ,
@@ -264,20 +288,27 @@ export function createNamedImport(
264
288
what : string [ ] ,
265
289
from : string ,
266
290
) {
267
- return f . createImportDeclaration (
268
- undefined ,
291
+ const importClause = f . createImportClause (
292
+ false ,
269
293
undefined ,
270
- f . createImportClause (
271
- false ,
272
- undefined ,
273
- f . createNamedImports (
274
- what . map ( ( name ) =>
275
- f . createImportSpecifier ( false , undefined , f . createIdentifier ( name ) ) ,
276
- ) ,
294
+ f . createNamedImports (
295
+ what . map ( ( name ) =>
296
+ f . createImportSpecifier ( false , undefined , f . createIdentifier ( name ) ) ,
277
297
) ,
278
298
) ,
279
- f . createStringLiteral ( from ) ,
280
299
) ;
300
+ return isInUpdatedAstContext
301
+ ? f . createImportDeclaration (
302
+ undefined ,
303
+ importClause ,
304
+ f . createStringLiteral ( from ) ,
305
+ )
306
+ : f . createImportDeclaration (
307
+ undefined ,
308
+ undefined ,
309
+ importClause ,
310
+ f . createStringLiteral ( from ) ,
311
+ ) ;
281
312
}
282
313
283
314
export function isCallExpressionOf ( name : string , node : ts . CallExpression ) {
@@ -348,15 +379,23 @@ export function safelyMergeObjects(
348
379
}
349
380
}
350
381
351
- export function updateDecoratorArguments < T extends ts . ClassDeclaration | ts . PropertyDeclaration | ts . GetAccessorDeclaration > (
382
+ export function updateDecoratorArguments <
383
+ T extends
384
+ | ts . ClassDeclaration
385
+ | ts . PropertyDeclaration
386
+ | ts . GetAccessorDeclaration ,
387
+ > (
352
388
f : ts . NodeFactory ,
353
389
node : T ,
354
390
decoratorName : string ,
355
- replaceFn : ( decoratorArguments : ts . NodeArray < ts . Expression > ) => ts . Expression [ ]
391
+ replaceFn : (
392
+ decoratorArguments : ts . NodeArray < ts . Expression > ,
393
+ ) => ts . Expression [ ] ,
356
394
) : T {
357
395
let updated = false ;
358
396
359
- const decorators = node . decorators . map ( ( decorator ) => {
397
+ const nodeOriginalDecorators = getDecorators ( node ) ;
398
+ const decorators = nodeOriginalDecorators . map ( ( decorator ) => {
360
399
if ( getDecoratorName ( decorator ) !== decoratorName ) {
361
400
return decorator ;
362
401
}
@@ -379,14 +418,71 @@ export function updateDecoratorArguments<T extends ts.ClassDeclaration | ts.Prop
379
418
}
380
419
381
420
if ( ts . isClassDeclaration ( node ) ) {
382
- return f . updateClassDeclaration ( node , decorators , node . modifiers , node . name , node . typeParameters , node . heritageClauses , node . members ) as T ;
421
+ return (
422
+ isInUpdatedAstContext
423
+ ? f . updateClassDeclaration (
424
+ node ,
425
+ [ ...decorators , ...getModifiers ( node ) ] ,
426
+ node . name ,
427
+ node . typeParameters ,
428
+ node . heritageClauses ,
429
+ node . members ,
430
+ )
431
+ : ( f . updateClassDeclaration as any ) (
432
+ node ,
433
+ decorators ,
434
+ node . modifiers ,
435
+ node . name ,
436
+ node . typeParameters ,
437
+ node . heritageClauses ,
438
+ node . members ,
439
+ )
440
+ ) as T ;
383
441
}
384
442
385
443
if ( ts . isPropertyDeclaration ( node ) ) {
386
- return f . updatePropertyDeclaration ( node , decorators , node . modifiers , node . name , node . questionToken , node . type , node . initializer ) as T ;
444
+ return (
445
+ isInUpdatedAstContext
446
+ ? f . updatePropertyDeclaration (
447
+ node ,
448
+ [ ...decorators , ...getModifiers ( node ) ] ,
449
+ node . name ,
450
+ node . questionToken ,
451
+ node . type ,
452
+ node . initializer ,
453
+ )
454
+ : ( f . updatePropertyDeclaration as any ) (
455
+ node ,
456
+ decorators ,
457
+ node . modifiers ,
458
+ node . name ,
459
+ node . questionToken ,
460
+ node . type ,
461
+ node . initializer ,
462
+ )
463
+ ) as T ;
387
464
}
388
465
389
466
if ( ts . isGetAccessorDeclaration ( node ) ) {
390
- return f . updateGetAccessorDeclaration ( node , decorators , node . modifiers , node . name , node . parameters , node . type , node . body ) as T ;
467
+ return (
468
+ isInUpdatedAstContext
469
+ ? f . updateGetAccessorDeclaration (
470
+ node ,
471
+ [ ...decorators , ...getModifiers ( node ) ] ,
472
+ node . name ,
473
+ node . parameters ,
474
+ node . type ,
475
+ node . body ,
476
+ )
477
+ : ( f . updateGetAccessorDeclaration as any ) (
478
+ node ,
479
+ decorators ,
480
+ node . modifiers ,
481
+ node . name ,
482
+ node . parameters ,
483
+ node . type ,
484
+ node . body ,
485
+ )
486
+ ) as T ;
391
487
}
392
488
}
0 commit comments