Skip to content

Commit a73ca90

Browse files
authored
Remove getAllAccessorDeclarations from the EmitResolver (microsoft#57993)
1 parent 97dc5f0 commit a73ca90

File tree

6 files changed

+65
-34
lines changed

6 files changed

+65
-34
lines changed

src/compiler/checker.ts

+52-21
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ import {
235235
GenericType,
236236
GetAccessorDeclaration,
237237
getAliasDeclarationFromName,
238-
getAllAccessorDeclarations,
239238
getAllJSDocTags,
240239
getAllowSyntheticDefaultImports,
241240
getAncestor,
@@ -8507,7 +8506,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85078506
}
85088507

85098508
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)));
85118510
}
85128511

85138512
function existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing: TypeNode, type: Type) {
@@ -8530,7 +8529,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85308529
const declWithExistingAnnotation = getDeclarationWithTypeAnnotation(symbol, getEnclosingDeclarationIgnoringFakeScope(enclosingDeclaration));
85318530
if (declWithExistingAnnotation && !isFunctionLikeDeclaration(declWithExistingAnnotation) && !isGetAccessorDeclaration(declWithExistingAnnotation)) {
85328531
// try to reuse the existing annotation
8533-
const existing = getEffectiveTypeAnnotationNode(declWithExistingAnnotation)!;
8532+
const existing = getNonlocalEffectiveTypeAnnotationNode(declWithExistingAnnotation)!;
85348533
const result = tryReuseExistingTypeNode(context, existing, type, declWithExistingAnnotation, addUndefined, includePrivateSymbol, bundled);
85358534
if (result) {
85368535
return result;
@@ -8583,7 +8582,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85838582
const typePredicate = getTypePredicateOfSignature(signature);
85848583
const type = getReturnTypeOfSignature(signature);
85858584
if (!isErrorType(type) && context.enclosingDeclaration) {
8586-
const annotation = signature.declaration && getEffectiveReturnTypeNode(signature.declaration);
8585+
const annotation = signature.declaration && getNonlocalEffectiveReturnTypeAnnotationNode(signature.declaration);
85878586
const enclosingDeclarationIgnoringFakeScope = getEnclosingDeclarationIgnoringFakeScope(context.enclosingDeclaration);
85888587
if (!!findAncestor(annotation, n => n === enclosingDeclarationIgnoringFakeScope) && annotation) {
85898588
const annotated = getTypeFromTypeNode(annotation);
@@ -48696,6 +48695,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4869648695
return isFunctionLike(declaration) || isExportAssignment(declaration) || isVariableLike(declaration);
4869748696
}
4869848697

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+
4869948714
function getPossibleTypeNodeReuseExpression(declaration: DeclarationWithPotentialInnerNodeReuse) {
4870048715
return isFunctionLike(declaration) && !isSetAccessor(declaration)
4870148716
? getSingleReturnExpression(declaration)
@@ -48704,7 +48719,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4870448719
: !!(declaration as HasInitializer).initializer
4870548720
? (declaration as HasInitializer & typeof declaration).initializer
4870648721
: isParameter(declaration) && isSetAccessor(declaration.parent)
48707-
? getSingleReturnExpression(getAllAccessorDeclarations(getSymbolOfDeclaration(declaration.parent)?.declarations, declaration.parent).getAccessor)
48722+
? getSingleReturnExpression(getAllAccessorDeclarationsForDeclaration(declaration.parent).getAccessor)
4870848723
: undefined;
4870948724
}
4871048725

@@ -48894,6 +48909,37 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4889448909
}
4889548910
}
4889648911

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+
4889748943
function createResolver(): EmitResolver {
4889848944
return {
4889948945
getReferencedExportContainer,
@@ -48949,21 +48995,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4894948995
},
4895048996
getJsxFactoryEntity,
4895148997
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-
},
4896748998
isBindingCapturedByNode: (node, decl) => {
4896848999
const parseNode = getParseTreeNode(node);
4896949000
const parseDecl = getParseTreeNode(decl);
@@ -49280,7 +49311,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4928049311
}
4928149312
}
4928249313
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);
4928449315
if (hasDecorators(accessors.firstAccessor) && node === accessors.secondAccessor) {
4928549316
return grammarErrorOnFirstToken(node, Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name);
4928649317
}

src/compiler/emitter.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,6 @@ export const notImplementedResolver: EmitResolver = {
11121112
isLiteralConstDeclaration: notImplemented,
11131113
getJsxFactoryEntity: notImplemented,
11141114
getJsxFragmentFactoryEntity: notImplemented,
1115-
getAllAccessorDeclarations: notImplemented,
11161115
isBindingCapturedByNode: notImplemented,
11171116
getDeclarationStatementsForSourceFile: notImplemented,
11181117
isImportRequiredByAugmentation: notImplemented,

src/compiler/transformers/declarations.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
FunctionTypeNode,
5151
GeneratedIdentifierFlags,
5252
GetAccessorDeclaration,
53+
getAllAccessorDeclarations,
5354
getCommentRange,
5455
getDirectoryPath,
5556
getEffectiveBaseTypeNode,
@@ -119,6 +120,7 @@ import {
119120
isMethodSignature,
120121
isModifier,
121122
isModuleDeclaration,
123+
isObjectLiteralExpression,
122124
isOmittedExpression,
123125
isPrivateIdentifier,
124126
isSemicolonClassElement,
@@ -728,7 +730,7 @@ export function transformDeclarations(context: TransformationContext) {
728730
if (!isPrivate) {
729731
const valueParameter = getSetAccessorValueParameter(input);
730732
if (valueParameter) {
731-
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input));
733+
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, getAllAccessorDeclarations(isObjectLiteralExpression(input.parent) ? input.parent.properties : input.parent.members, input));
732734
newValueParameter = ensureParameter(valueParameter, /*modifierMask*/ undefined, accessorType);
733735
}
734736
}
@@ -1037,7 +1039,7 @@ export function transformDeclarations(context: TransformationContext) {
10371039
if (isPrivateIdentifier(input.name)) {
10381040
return cleanup(/*returnValue*/ undefined);
10391041
}
1040-
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input));
1042+
const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, getAllAccessorDeclarations(isObjectLiteralExpression(input.parent) ? input.parent.properties : input.parent.members, input));
10411043
return cleanup(factory.updateGetAccessorDeclaration(
10421044
input,
10431045
ensureModifiers(input),

src/compiler/transformers/ts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ export function transformTypeScript(context: TransformationContext) {
11221122
if (typeSerializer) {
11231123
let decorators: Decorator[] | undefined;
11241124
if (shouldAddTypeMetadata(node)) {
1125-
const typeMetadata = emitHelpers().createMetadataHelper("design:type", typeSerializer.serializeTypeOfNode({ currentLexicalScope, currentNameScope: container }, node));
1125+
const typeMetadata = emitHelpers().createMetadataHelper("design:type", typeSerializer.serializeTypeOfNode({ currentLexicalScope, currentNameScope: container }, node, container));
11261126
decorators = append(decorators, factory.createDecorator(typeMetadata));
11271127
}
11281128
if (shouldAddParamTypesMetadata(node)) {
@@ -1141,7 +1141,7 @@ export function transformTypeScript(context: TransformationContext) {
11411141
if (typeSerializer) {
11421142
let properties: ObjectLiteralElementLike[] | undefined;
11431143
if (shouldAddTypeMetadata(node)) {
1144-
const typeProperty = factory.createPropertyAssignment("type", factory.createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, factory.createToken(SyntaxKind.EqualsGreaterThanToken), typeSerializer.serializeTypeOfNode({ currentLexicalScope, currentNameScope: container }, node)));
1144+
const typeProperty = factory.createPropertyAssignment("type", factory.createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, factory.createToken(SyntaxKind.EqualsGreaterThanToken), typeSerializer.serializeTypeOfNode({ currentLexicalScope, currentNameScope: container }, node, container)));
11451145
properties = append(properties, typeProperty);
11461146
}
11471147
if (shouldAddParamTypesMetadata(node)) {

src/compiler/transformers/typeSerializer.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface RuntimeTypeSerializer {
115115
* Serializes the type of a node for use with decorator type metadata.
116116
* @param node The node that should have its type serialized.
117117
*/
118-
serializeTypeOfNode(serializerContext: RuntimeTypeSerializerContext, node: PropertyDeclaration | ParameterDeclaration | AccessorDeclaration | ClassLikeDeclaration | MethodDeclaration): Expression;
118+
serializeTypeOfNode(serializerContext: RuntimeTypeSerializerContext, node: PropertyDeclaration | ParameterDeclaration | AccessorDeclaration | ClassLikeDeclaration | MethodDeclaration, container: ClassLikeDeclaration): Expression;
119119
/**
120120
* Serializes the types of the parameters of a node for use with decorator type metadata.
121121
* @param node The node that should have its parameter types serialized.
@@ -145,7 +145,7 @@ export function createRuntimeTypeSerializer(context: TransformationContext): Run
145145

146146
return {
147147
serializeTypeNode: (serializerContext, node) => setSerializerContextAnd(serializerContext, serializeTypeNode, node),
148-
serializeTypeOfNode: (serializerContext, node) => setSerializerContextAnd(serializerContext, serializeTypeOfNode, node),
148+
serializeTypeOfNode: (serializerContext, node, container) => setSerializerContextAnd(serializerContext, serializeTypeOfNode, node, container),
149149
serializeParameterTypesOfNode: (serializerContext, node, container) => setSerializerContextAnd(serializerContext, serializeParameterTypesOfNode, node, container),
150150
serializeReturnTypeOfNode: (serializerContext, node) => setSerializerContextAnd(serializerContext, serializeReturnTypeOfNode, node),
151151
};
@@ -166,8 +166,8 @@ export function createRuntimeTypeSerializer(context: TransformationContext): Run
166166
return result;
167167
}
168168

169-
function getAccessorTypeNode(node: AccessorDeclaration) {
170-
const accessors = resolver.getAllAccessorDeclarations(node);
169+
function getAccessorTypeNode(node: AccessorDeclaration, container: ClassLikeDeclaration) {
170+
const accessors = getAllAccessorDeclarations(container.members, node);
171171
return accessors.setAccessor && getSetAccessorTypeAnnotationNode(accessors.setAccessor)
172172
|| accessors.getAccessor && getEffectiveReturnTypeNode(accessors.getAccessor);
173173
}
@@ -176,14 +176,14 @@ export function createRuntimeTypeSerializer(context: TransformationContext): Run
176176
* Serializes the type of a node for use with decorator type metadata.
177177
* @param node The node that should have its type serialized.
178178
*/
179-
function serializeTypeOfNode(node: PropertyDeclaration | ParameterDeclaration | AccessorDeclaration | ClassLikeDeclaration | MethodDeclaration): SerializedTypeNode {
179+
function serializeTypeOfNode(node: PropertyDeclaration | ParameterDeclaration | AccessorDeclaration | ClassLikeDeclaration | MethodDeclaration, container: ClassLikeDeclaration): SerializedTypeNode {
180180
switch (node.kind) {
181181
case SyntaxKind.PropertyDeclaration:
182182
case SyntaxKind.Parameter:
183183
return serializeTypeNode(node.type);
184184
case SyntaxKind.SetAccessor:
185185
case SyntaxKind.GetAccessor:
186-
return serializeTypeNode(getAccessorTypeNode(node));
186+
return serializeTypeNode(getAccessorTypeNode(node, container));
187187
case SyntaxKind.ClassDeclaration:
188188
case SyntaxKind.ClassExpression:
189189
case SyntaxKind.MethodDeclaration:
@@ -217,7 +217,7 @@ export function createRuntimeTypeSerializer(context: TransformationContext): Run
217217
expressions.push(serializeTypeNode(getRestParameterElementType(parameter.type)));
218218
}
219219
else {
220-
expressions.push(serializeTypeOfNode(parameter));
220+
expressions.push(serializeTypeOfNode(parameter, container));
221221
}
222222
}
223223
}

src/compiler/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5635,7 +5635,6 @@ export interface EmitResolver {
56355635
isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean;
56365636
getJsxFactoryEntity(location?: Node): EntityName | undefined;
56375637
getJsxFragmentFactoryEntity(location?: Node): EntityName | undefined;
5638-
getAllAccessorDeclarations(declaration: AccessorDeclaration): AllAccessorDeclarations;
56395638
isBindingCapturedByNode(node: Node, decl: VariableDeclaration | BindingElement): boolean;
56405639
getDeclarationStatementsForSourceFile(node: SourceFile, flags: NodeBuilderFlags, tracker: SymbolTracker, bundled?: boolean): Statement[] | undefined;
56415640
isImportRequiredByAugmentation(decl: ImportDeclaration): boolean;

0 commit comments

Comments
 (0)