@@ -393,7 +393,7 @@ namespace ts {
393
393
}
394
394
}
395
395
396
- function ensureParameter ( p : ParameterDeclaration , modifierMask ?: ModifierFlags ) : ParameterDeclaration {
396
+ function ensureParameter ( p : ParameterDeclaration , modifierMask ?: ModifierFlags , type ?: TypeNode ) : ParameterDeclaration {
397
397
let oldDiag : typeof getSymbolAccessibilityDiagnostic | undefined ;
398
398
if ( ! suppressNewDiagnosticContexts ) {
399
399
oldDiag = getSymbolAccessibilityDiagnostic ;
@@ -406,7 +406,7 @@ namespace ts {
406
406
p . dotDotDotToken ,
407
407
filterBindingPatternInitializers ( p . name ) ,
408
408
resolver . isOptionalParameter ( p ) ? ( p . questionToken || createToken ( SyntaxKind . QuestionToken ) ) : undefined ,
409
- ensureType ( p , p . type , /*ignorePrivate*/ true ) , // Ignore private param props, since this type is going straight back into a param
409
+ type || ensureType ( p , p . type , /*ignorePrivate*/ true ) , // Ignore private param props, since this type is going straight back into a param
410
410
ensureNoInitializer ( p )
411
411
) ;
412
412
if ( ! suppressNewDiagnosticContexts ) {
@@ -535,6 +535,36 @@ namespace ts {
535
535
return createNodeArray ( newParams , params . hasTrailingComma ) ;
536
536
}
537
537
538
+ function updateAccessorParamsList ( input : AccessorDeclaration , isPrivate : boolean ) {
539
+ let newParams : ParameterDeclaration [ ] | undefined ;
540
+ if ( ! isPrivate ) {
541
+ const thisParameter = getThisParameter ( input ) ;
542
+ if ( thisParameter ) {
543
+ newParams = [ ensureParameter ( thisParameter ) ] ;
544
+ }
545
+ }
546
+ if ( isSetAccessorDeclaration ( input ) ) {
547
+ let newValueParameter : ParameterDeclaration | undefined ;
548
+ if ( ! isPrivate ) {
549
+ const valueParameter = getSetAccessorValueParameter ( input ) ;
550
+ if ( valueParameter ) {
551
+ const accessorType = getTypeAnnotationFromAllAccessorDeclarations ( input , resolver . getAllAccessorDeclarations ( input ) ) ;
552
+ newValueParameter = ensureParameter ( valueParameter , /*modifierMask*/ undefined , accessorType ) ;
553
+ }
554
+ }
555
+ if ( ! newValueParameter ) {
556
+ newValueParameter = createParameter (
557
+ /*decorators*/ undefined ,
558
+ /*modifiers*/ undefined ,
559
+ /*dotDotDotToken*/ undefined ,
560
+ "value"
561
+ ) ;
562
+ }
563
+ newParams = append ( newParams , newValueParameter ) ;
564
+ }
565
+ return createNodeArray ( newParams || emptyArray ) as NodeArray < ParameterDeclaration > ;
566
+ }
567
+
538
568
function ensureTypeParams ( node : Node , params : NodeArray < TypeParameterDeclaration > | undefined ) {
539
569
return hasModifier ( node , ModifierFlags . Private ) ? undefined : visitNodes ( params , visitDeclarationSubtree ) ;
540
570
}
@@ -811,10 +841,33 @@ namespace ts {
811
841
return cleanup ( sig ) ;
812
842
}
813
843
case SyntaxKind . GetAccessor : {
844
+ // For now, only emit class accessors as accessors if they were already declared in an ambient context.
845
+ if ( input . flags & NodeFlags . Ambient ) {
846
+ const isPrivate = hasModifier ( input , ModifierFlags . Private ) ;
847
+ const accessorType = getTypeAnnotationFromAllAccessorDeclarations ( input , resolver . getAllAccessorDeclarations ( input ) ) ;
848
+ return cleanup ( updateGetAccessor (
849
+ input ,
850
+ /*decorators*/ undefined ,
851
+ ensureModifiers ( input ) ,
852
+ input . name ,
853
+ updateAccessorParamsList ( input , isPrivate ) ,
854
+ ! isPrivate ? ensureType ( input , accessorType ) : undefined ,
855
+ /*body*/ undefined ) ) ;
856
+ }
814
857
const newNode = ensureAccessor ( input ) ;
815
858
return cleanup ( newNode ) ;
816
859
}
817
860
case SyntaxKind . SetAccessor : {
861
+ // For now, only emit class accessors as accessors if they were already declared in an ambient context.
862
+ if ( input . flags & NodeFlags . Ambient ) {
863
+ return cleanup ( updateSetAccessor (
864
+ input ,
865
+ /*decorators*/ undefined ,
866
+ ensureModifiers ( input ) ,
867
+ input . name ,
868
+ updateAccessorParamsList ( input , hasModifier ( input , ModifierFlags . Private ) ) ,
869
+ /*body*/ undefined ) ) ;
870
+ }
818
871
const newNode = ensureAccessor ( input ) ;
819
872
return cleanup ( newNode ) ;
820
873
}
@@ -1374,17 +1427,27 @@ namespace ts {
1374
1427
return maskModifierFlags ( node , mask , additions ) ;
1375
1428
}
1376
1429
1377
- function ensureAccessor ( node : AccessorDeclaration ) : PropertyDeclaration | undefined {
1378
- const accessors = resolver . getAllAccessorDeclarations ( node ) ;
1379
- if ( node . kind !== accessors . firstAccessor . kind ) {
1380
- return ;
1381
- }
1430
+ function getTypeAnnotationFromAllAccessorDeclarations ( node : AccessorDeclaration , accessors : AllAccessorDeclarations ) {
1382
1431
let accessorType = getTypeAnnotationFromAccessor ( node ) ;
1383
- if ( ! accessorType && accessors . secondAccessor ) {
1432
+ if ( ! accessorType && node !== accessors . firstAccessor ) {
1433
+ accessorType = getTypeAnnotationFromAccessor ( accessors . firstAccessor ) ;
1434
+ // If we end up pulling the type from the second accessor, we also need to change the diagnostic context to get the expected error message
1435
+ getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode ( accessors . firstAccessor ) ;
1436
+ }
1437
+ if ( ! accessorType && accessors . secondAccessor && node !== accessors . secondAccessor ) {
1384
1438
accessorType = getTypeAnnotationFromAccessor ( accessors . secondAccessor ) ;
1385
1439
// If we end up pulling the type from the second accessor, we also need to change the diagnostic context to get the expected error message
1386
1440
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode ( accessors . secondAccessor ) ;
1387
1441
}
1442
+ return accessorType ;
1443
+ }
1444
+
1445
+ function ensureAccessor ( node : AccessorDeclaration ) : PropertyDeclaration | undefined {
1446
+ const accessors = resolver . getAllAccessorDeclarations ( node ) ;
1447
+ if ( node . kind !== accessors . firstAccessor . kind ) {
1448
+ return ;
1449
+ }
1450
+ const accessorType = getTypeAnnotationFromAllAccessorDeclarations ( node , accessors ) ;
1388
1451
const prop = createProperty ( /*decorators*/ undefined , maskModifiers ( node , /*mask*/ undefined , ( ! accessors . setAccessor ) ? ModifierFlags . Readonly : ModifierFlags . None ) , node . name , node . questionToken , ensureType ( node , accessorType ) , /*initializer*/ undefined ) ;
1389
1452
const leadingsSyntheticCommentRanges = accessors . secondAccessor && getLeadingCommentRangesOfNode ( accessors . secondAccessor , currentSourceFile ) ;
1390
1453
if ( leadingsSyntheticCommentRanges ) {
0 commit comments