@@ -235,7 +235,6 @@ import {
235
235
GenericType,
236
236
GetAccessorDeclaration,
237
237
getAliasDeclarationFromName,
238
- getAllAccessorDeclarations,
239
238
getAllJSDocTags,
240
239
getAllowSyntheticDefaultImports,
241
240
getAncestor,
@@ -8507,7 +8506,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
8507
8506
}
8508
8507
8509
8508
function getDeclarationWithTypeAnnotation(symbol: Symbol, enclosingDeclaration: Node | undefined) {
8510
- return symbol.declarations && find(symbol.declarations, s => !!getEffectiveTypeAnnotationNode (s) && (!enclosingDeclaration || !!findAncestor(s, n => n === enclosingDeclaration)));
8509
+ return symbol.declarations && find(symbol.declarations, s => !!getNonlocalEffectiveTypeAnnotationNode (s) && (!enclosingDeclaration || !!findAncestor(s, n => n === enclosingDeclaration)));
8511
8510
}
8512
8511
8513
8512
function existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing: TypeNode, type: Type) {
@@ -8530,7 +8529,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
8530
8529
const declWithExistingAnnotation = getDeclarationWithTypeAnnotation(symbol, getEnclosingDeclarationIgnoringFakeScope(enclosingDeclaration));
8531
8530
if (declWithExistingAnnotation && !isFunctionLikeDeclaration(declWithExistingAnnotation) && !isGetAccessorDeclaration(declWithExistingAnnotation)) {
8532
8531
// try to reuse the existing annotation
8533
- const existing = getEffectiveTypeAnnotationNode (declWithExistingAnnotation)!;
8532
+ const existing = getNonlocalEffectiveTypeAnnotationNode (declWithExistingAnnotation)!;
8534
8533
const result = tryReuseExistingTypeNode(context, existing, type, declWithExistingAnnotation, addUndefined, includePrivateSymbol, bundled);
8535
8534
if (result) {
8536
8535
return result;
@@ -8583,7 +8582,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
8583
8582
const typePredicate = getTypePredicateOfSignature(signature);
8584
8583
const type = getReturnTypeOfSignature(signature);
8585
8584
if (!isErrorType(type) && context.enclosingDeclaration) {
8586
- const annotation = signature.declaration && getEffectiveReturnTypeNode (signature.declaration);
8585
+ const annotation = signature.declaration && getNonlocalEffectiveReturnTypeAnnotationNode (signature.declaration);
8587
8586
const enclosingDeclarationIgnoringFakeScope = getEnclosingDeclarationIgnoringFakeScope(context.enclosingDeclaration);
8588
8587
if (!!findAncestor(annotation, n => n === enclosingDeclarationIgnoringFakeScope) && annotation) {
8589
8588
const annotated = getTypeFromTypeNode(annotation);
@@ -48696,6 +48695,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
48696
48695
return isFunctionLike(declaration) || isExportAssignment(declaration) || isVariableLike(declaration);
48697
48696
}
48698
48697
48698
+ function getAllAccessorDeclarationsForDeclaration(accessor: AccessorDeclaration): AllAccessorDeclarations {
48699
+ accessor = getParseTreeNode(accessor, isGetOrSetAccessorDeclaration)!; // TODO: GH#18217
48700
+ const otherKind = accessor.kind === SyntaxKind.SetAccessor ? SyntaxKind.GetAccessor : SyntaxKind.SetAccessor;
48701
+ const otherAccessor = getDeclarationOfKind<AccessorDeclaration>(getSymbolOfDeclaration(accessor), otherKind);
48702
+ const firstAccessor = otherAccessor && (otherAccessor.pos < accessor.pos) ? otherAccessor : accessor;
48703
+ const secondAccessor = otherAccessor && (otherAccessor.pos < accessor.pos) ? accessor : otherAccessor;
48704
+ const setAccessor = accessor.kind === SyntaxKind.SetAccessor ? accessor : otherAccessor as SetAccessorDeclaration;
48705
+ const getAccessor = accessor.kind === SyntaxKind.GetAccessor ? accessor : otherAccessor as GetAccessorDeclaration;
48706
+ return {
48707
+ firstAccessor,
48708
+ secondAccessor,
48709
+ setAccessor,
48710
+ getAccessor,
48711
+ };
48712
+ }
48713
+
48699
48714
function getPossibleTypeNodeReuseExpression(declaration: DeclarationWithPotentialInnerNodeReuse) {
48700
48715
return isFunctionLike(declaration) && !isSetAccessor(declaration)
48701
48716
? getSingleReturnExpression(declaration)
@@ -48704,7 +48719,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
48704
48719
: !!(declaration as HasInitializer).initializer
48705
48720
? (declaration as HasInitializer & typeof declaration).initializer
48706
48721
: isParameter(declaration) && isSetAccessor(declaration.parent)
48707
- ? getSingleReturnExpression(getAllAccessorDeclarations(getSymbolOfDeclaration(declaration.parent)?.declarations, declaration.parent).getAccessor)
48722
+ ? getSingleReturnExpression(getAllAccessorDeclarationsForDeclaration( declaration.parent).getAccessor)
48708
48723
: undefined;
48709
48724
}
48710
48725
@@ -48894,6 +48909,37 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
48894
48909
}
48895
48910
}
48896
48911
48912
+ function getNonlocalEffectiveTypeAnnotationNode(node: Node) {
48913
+ const direct = getEffectiveTypeAnnotationNode(node);
48914
+ if (direct) {
48915
+ return direct;
48916
+ }
48917
+ if (node.kind === SyntaxKind.Parameter && node.parent.kind === SyntaxKind.SetAccessor) {
48918
+ const other = getAllAccessorDeclarationsForDeclaration(node.parent as SetAccessorDeclaration).getAccessor;
48919
+ if (other) {
48920
+ return getEffectiveReturnTypeNode(other);
48921
+ }
48922
+ }
48923
+ return undefined;
48924
+ }
48925
+
48926
+ function getNonlocalEffectiveReturnTypeAnnotationNode(node: SignatureDeclaration | JSDocSignature) {
48927
+ const direct = getEffectiveReturnTypeNode(node);
48928
+ if (direct) {
48929
+ return direct;
48930
+ }
48931
+ if (node.kind === SyntaxKind.GetAccessor) {
48932
+ const other = getAllAccessorDeclarationsForDeclaration(node).setAccessor;
48933
+ if (other) {
48934
+ const param = getSetAccessorValueParameter(other);
48935
+ if (param) {
48936
+ return getEffectiveTypeAnnotationNode(param);
48937
+ }
48938
+ }
48939
+ }
48940
+ return undefined;
48941
+ }
48942
+
48897
48943
function createResolver(): EmitResolver {
48898
48944
return {
48899
48945
getReferencedExportContainer,
@@ -48949,21 +48995,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
48949
48995
},
48950
48996
getJsxFactoryEntity,
48951
48997
getJsxFragmentFactoryEntity,
48952
- getAllAccessorDeclarations(accessor: AccessorDeclaration): AllAccessorDeclarations {
48953
- accessor = getParseTreeNode(accessor, isGetOrSetAccessorDeclaration)!; // TODO: GH#18217
48954
- const otherKind = accessor.kind === SyntaxKind.SetAccessor ? SyntaxKind.GetAccessor : SyntaxKind.SetAccessor;
48955
- const otherAccessor = getDeclarationOfKind<AccessorDeclaration>(getSymbolOfDeclaration(accessor), otherKind);
48956
- const firstAccessor = otherAccessor && (otherAccessor.pos < accessor.pos) ? otherAccessor : accessor;
48957
- const secondAccessor = otherAccessor && (otherAccessor.pos < accessor.pos) ? accessor : otherAccessor;
48958
- const setAccessor = accessor.kind === SyntaxKind.SetAccessor ? accessor : otherAccessor as SetAccessorDeclaration;
48959
- const getAccessor = accessor.kind === SyntaxKind.GetAccessor ? accessor : otherAccessor as GetAccessorDeclaration;
48960
- return {
48961
- firstAccessor,
48962
- secondAccessor,
48963
- setAccessor,
48964
- getAccessor,
48965
- };
48966
- },
48967
48998
isBindingCapturedByNode: (node, decl) => {
48968
48999
const parseNode = getParseTreeNode(node);
48969
49000
const parseDecl = getParseTreeNode(decl);
@@ -49280,7 +49311,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
49280
49311
}
49281
49312
}
49282
49313
else if (legacyDecorators && (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor)) {
49283
- const accessors = getAllAccessorDeclarations((node.parent as ClassDeclaration).members, node as AccessorDeclaration);
49314
+ const accessors = getAllAccessorDeclarationsForDeclaration( node as AccessorDeclaration);
49284
49315
if (hasDecorators(accessors.firstAccessor) && node === accessors.secondAccessor) {
49285
49316
return grammarErrorOnFirstToken(node, Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name);
49286
49317
}
0 commit comments