@@ -384,4 +384,140 @@ namespace ts {
384
384
export function isNonStaticMethodOrAccessorWithPrivateName ( member : ClassElement ) : member is PrivateIdentifierMethodDeclaration | PrivateIdentifierAccessorDeclaration {
385
385
return ! isStatic ( member ) && isMethodOrAccessor ( member ) && isPrivateIdentifier ( member . name ) ;
386
386
}
387
+
388
+ /**
389
+ * Gets an array of arrays of decorators for the parameters of a function-like node.
390
+ * The offset into the result array should correspond to the offset of the parameter.
391
+ *
392
+ * @param node The function-like node.
393
+ */
394
+ function getDecoratorsOfParameters ( node : FunctionLikeDeclaration | undefined ) {
395
+ let decorators : ( readonly Decorator [ ] | undefined ) [ ] | undefined ;
396
+ if ( node ) {
397
+ const parameters = node . parameters ;
398
+ const firstParameterIsThis = parameters . length > 0 && parameterIsThisKeyword ( parameters [ 0 ] ) ;
399
+ const firstParameterOffset = firstParameterIsThis ? 1 : 0 ;
400
+ const numParameters = firstParameterIsThis ? parameters . length - 1 : parameters . length ;
401
+ for ( let i = 0 ; i < numParameters ; i ++ ) {
402
+ const parameter = parameters [ i + firstParameterOffset ] ;
403
+ if ( decorators || parameter . decorators ) {
404
+ if ( ! decorators ) {
405
+ decorators = new Array ( numParameters ) ;
406
+ }
407
+
408
+ decorators [ i ] = parameter . decorators ;
409
+ }
410
+ }
411
+ }
412
+
413
+ return decorators ;
414
+ }
415
+
416
+ /**
417
+ * Gets an AllDecorators object containing the decorators for the class and the decorators for the
418
+ * parameters of the constructor of the class.
419
+ *
420
+ * @param node The class node.
421
+ */
422
+ export function getAllDecoratorsOfClass ( node : ClassLikeDeclaration ) : AllDecorators | undefined {
423
+ const decorators = node . decorators ;
424
+ const parameters = getDecoratorsOfParameters ( getFirstConstructorWithBody ( node ) ) ;
425
+ if ( ! decorators && ! parameters ) {
426
+ return undefined ;
427
+ }
428
+
429
+ return {
430
+ decorators,
431
+ parameters
432
+ } ;
433
+ }
434
+
435
+ /**
436
+ * Gets an AllDecorators object containing the decorators for the member and its parameters.
437
+ *
438
+ * @param parent The class node that contains the member.
439
+ * @param member The class member.
440
+ */
441
+ export function getAllDecoratorsOfClassElement ( member : ClassElement , parent : ClassLikeDeclaration ) : AllDecorators | undefined {
442
+ switch ( member . kind ) {
443
+ case SyntaxKind . GetAccessor :
444
+ case SyntaxKind . SetAccessor :
445
+ return getAllDecoratorsOfAccessors ( member as AccessorDeclaration , parent ) ;
446
+
447
+ case SyntaxKind . MethodDeclaration :
448
+ return getAllDecoratorsOfMethod ( member as MethodDeclaration ) ;
449
+
450
+ case SyntaxKind . PropertyDeclaration :
451
+ return getAllDecoratorsOfProperty ( member as PropertyDeclaration ) ;
452
+
453
+ default :
454
+ return undefined ;
455
+ }
456
+ }
457
+
458
+ /**
459
+ * Gets an AllDecorators object containing the decorators for the accessor and its parameters.
460
+ *
461
+ * @param parent The class node that contains the accessor.
462
+ * @param accessor The class accessor member.
463
+ */
464
+ function getAllDecoratorsOfAccessors ( accessor : AccessorDeclaration , parent : ClassExpression | ClassDeclaration ) : AllDecorators | undefined {
465
+ if ( ! accessor . body ) {
466
+ return undefined ;
467
+ }
468
+
469
+ const { firstAccessor, secondAccessor, getAccessor, setAccessor } = getAllAccessorDeclarations ( parent . members , accessor ) ;
470
+ const firstAccessorWithDecorators = firstAccessor . decorators ? firstAccessor : secondAccessor && secondAccessor . decorators ? secondAccessor : undefined ;
471
+ if ( ! firstAccessorWithDecorators || accessor !== firstAccessorWithDecorators ) {
472
+ return undefined ;
473
+ }
474
+
475
+ const decorators = firstAccessorWithDecorators . decorators ;
476
+ const parameters = getDecoratorsOfParameters ( setAccessor ) ;
477
+ if ( ! decorators && ! parameters ) {
478
+ return undefined ;
479
+ }
480
+
481
+ return {
482
+ decorators,
483
+ parameters,
484
+ getDecorators : getAccessor ?. decorators ,
485
+ setDecorators : setAccessor ?. decorators
486
+ } ;
487
+ }
488
+
489
+ /**
490
+ * Gets an AllDecorators object containing the decorators for the method and its parameters.
491
+ *
492
+ * @param method The class method member.
493
+ */
494
+ function getAllDecoratorsOfMethod ( method : MethodDeclaration ) : AllDecorators | undefined {
495
+ if ( ! method . body ) {
496
+ return undefined ;
497
+ }
498
+
499
+ const decorators = method . decorators ;
500
+ const parameters = getDecoratorsOfParameters ( method ) ;
501
+ if ( ! decorators && ! parameters ) {
502
+ return undefined ;
503
+ }
504
+
505
+ return { decorators, parameters } ;
506
+ }
507
+
508
+ /**
509
+ * Gets an AllDecorators object containing the decorators for the property.
510
+ *
511
+ * @param property The class property member.
512
+ */
513
+ function getAllDecoratorsOfProperty ( property : PropertyDeclaration ) : AllDecorators | undefined {
514
+ const decorators = property . decorators ;
515
+ if ( ! decorators ) {
516
+ return undefined ;
517
+
518
+ }
519
+
520
+ return { decorators } ;
521
+ }
522
+
387
523
}
0 commit comments