Skip to content

Commit f5a247c

Browse files
[api] Checker query APIs (#65589)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent c8a00cb commit f5a247c

9 files changed

Lines changed: 953 additions & 15 deletions

File tree

‎packages/typescript/src/api/async/api.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ import { TypeFormatFlags } from "../../../enums/typeFormatFlags.js";
2222
import { TypePredicateKind } from "../../../enums/typePredicateKind.js";
2323
import {
2424
type __String,
25+
type ArrayLiteralExpression,
2526
type CallLikeExpression,
2627
type Declaration,
2728
type EntityName,
2829
type Expression,
2930
type FileReference,
3031
type Identifier,
32+
type ImportTypeNode,
3133
type IndexSignatureDeclaration,
3234
ModifierFlags,
3335
type NamedTupleMember,
3436
type Node,
37+
type ObjectLiteralExpression,
3538
type ParameterDeclaration,
3639
type PathKey,
40+
type PropertyAccessExpression,
41+
type QualifiedName,
3742
type RootedDirectoryPath,
3843
type RootedFilePath,
3944
type SourceFile,
@@ -2343,6 +2348,63 @@ function encodeNumberLiteralValue(value: number): number | string {
23432348
}
23442349

23452350
export class Checker {
2351+
/** Get the original symbols underlying a synthetic, instantiated, or mapped property. */
2352+
async getRootSymbols(symbol: Symbol): Promise<readonly Symbol[]> {
2353+
const data = await this.client.apiRequest("getRootSymbols", {
2354+
snapshot: this.snapshotId,
2355+
project: this.project.handle,
2356+
symbol: symbol.reference,
2357+
});
2358+
return data ? data.map(symbol => this.objectRegistry.getOrCreateSymbol(symbol)) : [];
2359+
}
2360+
2361+
/** Get inferred or explicit signature type arguments; empty when the signature has no instantiation. */
2362+
async getTypeArgumentsForResolvedSignature(signature: Signature): Promise<readonly Type[]> {
2363+
return signature.getTypeArguments();
2364+
}
2365+
2366+
/** Get the source type of an object/array assignment pattern, including nested patterns and for-of initializers. */
2367+
async getTypeOfAssignmentPattern(node: ObjectLiteralExpression | ArrayLiteralExpression): Promise<Type | undefined> {
2368+
const data = await this.client.apiRequest("getTypeOfAssignmentPattern", {
2369+
snapshot: this.snapshotId,
2370+
project: this.project.handle,
2371+
location: getNodeRequestHandle(node),
2372+
});
2373+
return data ? this.objectRegistry.getOrCreateType(data) : undefined;
2374+
}
2375+
2376+
/** Get the distinct constructor parameter and instance property symbols of a parameter property. */
2377+
async getSymbolsOfParameterPropertyDeclaration(node: ParameterDeclaration): Promise<{ parameter: Symbol; property: Symbol; }> {
2378+
const data = await this.client.apiRequest("getSymbolsOfParameterPropertyDeclaration", {
2379+
snapshot: this.snapshotId,
2380+
project: this.project.handle,
2381+
location: getNodeRequestHandle(node),
2382+
});
2383+
return {
2384+
parameter: this.objectRegistry.getOrCreateSymbol(data.parameter),
2385+
property: this.objectRegistry.getOrCreateSymbol(data.property),
2386+
};
2387+
}
2388+
2389+
/** Whether the named property exists and is accessible at this access location. */
2390+
async isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName | ImportTypeNode, propertyName: string): Promise<boolean> {
2391+
return this.client.apiRequest("isValidPropertyAccess", {
2392+
snapshot: this.snapshotId,
2393+
project: this.project.handle,
2394+
location: getNodeRequestHandle(node),
2395+
propertyName,
2396+
});
2397+
}
2398+
2399+
/** Whether a parameter may be omitted, including defaults and untyped IIFE parameters. */
2400+
async isOptionalParameter(node: ParameterDeclaration): Promise<boolean> {
2401+
return this.client.apiRequest("isOptionalParameter", {
2402+
snapshot: this.snapshotId,
2403+
project: this.project.handle,
2404+
location: getNodeRequestHandle(node),
2405+
});
2406+
}
2407+
23462408
private snapshotId: number;
23472409
private project: Project;
23482410
private objectRegistry: ProjectObjectRegistry;
@@ -4228,6 +4290,7 @@ export class Signature {
42284290
readonly thisParameter?: CompactSymbolReference | undefined;
42294291
readonly target?: number | undefined;
42304292
private returnType: number | false;
4293+
private typeArguments: readonly number[] | false;
42314294

42324295
constructor(data: SignatureResponse, project: Project, objectRegistry: ProjectObjectRegistry) {
42334296
this.id = data.id;
@@ -4241,6 +4304,7 @@ export class Signature {
42414304
this.thisParameter = data.thisParameter;
42424305
this.target = data.target;
42434306
this.returnType = false;
4307+
this.typeArguments = false;
42444308
}
42454309

42464310
get declaration(): NodeHandle<Declaration> | undefined {
@@ -4254,6 +4318,13 @@ export class Signature {
42544318
return this.objectRegistry.fetchTypes(this, "getTypeParametersOfSignature", this.typeParameters) as Promise<readonly TypeParameter[]>;
42554319
}
42564320

4321+
/** Get inferred or explicit type arguments; empty when this signature has no instantiation. */
4322+
async getTypeArguments(): Promise<readonly Type[]> {
4323+
const result = await this.objectRegistry.fetchTypes(this, "getTypeArgumentsForResolvedSignature", this.typeArguments || undefined);
4324+
this.typeArguments = result.map(type => type.id);
4325+
return result;
4326+
}
4327+
42574328
async getParameters(): Promise<readonly Symbol[]> {
42584329
return this.objectRegistry.fetchSymbols(this, "getParametersOfSignature", this.parameters);
42594330
}

‎packages/typescript/src/api/proto.generated.ts‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@ export interface APIMethodInfo {
125125
getNameTypeOfMappedType: APIMethod<GetTypePropertyParams, TypeResponse | null>;
126126
getTemplateTypeOfMappedType: APIMethod<GetTypePropertyParams, TypeResponse>;
127127
getTypeParametersOfSignature: APIMethod<GetSignaturePropertyParams, TypeResponse[] | null>;
128+
getTypeArgumentsForResolvedSignature: APIMethod<GetSignaturePropertyParams, TypeResponse[]>;
128129
getParametersOfSignature: APIMethod<GetSignaturePropertyParams, SymbolResponse[] | null>;
129130
getThisParameterOfSignature: APIMethod<GetSignaturePropertyParams, SymbolResponse | null>;
130131
getTargetOfSignature: APIMethod<GetSignaturePropertyParams, SignatureResponse | null>;
132+
isOptionalParameter: APIMethod<CheckerNodeParams, boolean>;
133+
getTypeOfAssignmentPattern: APIMethod<CheckerNodeParams, TypeResponse | null>;
134+
isValidPropertyAccess: APIMethod<IsValidPropertyAccessParams, boolean>;
135+
getSymbolsOfParameterPropertyDeclaration: APIMethod<CheckerNodeParams, ParameterPropertySymbolsResponse>;
131136
getContextualType: APIMethod<GetContextualTypeParams, TypeResponse | null>;
132137
getContextualTypeForArgument: APIMethod<GetContextualTypeForArgumentParams, TypeResponse | null>;
133138
getAwaitedType: APIMethod<CheckerTypeParams, TypeResponse | null>;
@@ -188,6 +193,7 @@ export interface APIMethodInfo {
188193
getImmediateAliasedSymbol: APIMethod<CheckerSymbolParams, SymbolResponse | null>;
189194
getTargetSymbol: APIMethod<CheckerSymbolParams, SymbolResponse>;
190195
getMergedSymbol: APIMethod<CheckerSymbolParams, SymbolResponse>;
196+
getRootSymbols: APIMethod<CheckerSymbolParams, SymbolResponse[]>;
191197
getSymbolOfNode: APIMethod<CheckerNodeParams, SymbolResponse | null>;
192198
getSymbolOfDeclarationForChecker: APIMethod<CheckerNodeParams, SymbolResponse | null>;
193199
getParentOfSymbolForChecker: APIMethod<CheckerSymbolParams, SymbolResponse | null>;
@@ -807,6 +813,25 @@ export interface GetSignaturePropertyParams {
807813
objectId: number;
808814
}
809815

816+
/** CheckerNodeParams are parameters for checker methods that operate on a node location. */
817+
export interface CheckerNodeParams {
818+
snapshot: number;
819+
project: number;
820+
location: string;
821+
}
822+
823+
export interface IsValidPropertyAccessParams {
824+
snapshot: number;
825+
project: number;
826+
location: string;
827+
propertyName: string;
828+
}
829+
830+
export interface ParameterPropertySymbolsResponse {
831+
parameter: SymbolResponse;
832+
property: SymbolResponse;
833+
}
834+
810835
/** GetContextualTypeParams returns the contextual type for a node. */
811836
export interface GetContextualTypeParams {
812837
snapshot: number;
@@ -873,13 +898,6 @@ export interface GetIntrinsicTypeParams {
873898
project: number;
874899
}
875900

876-
/** CheckerNodeParams are parameters for checker methods that operate on a node location. */
877-
export interface CheckerNodeParams {
878-
snapshot: number;
879-
project: number;
880-
location: string;
881-
}
882-
883901
export interface GetPrivateIdentifierPropertyOfTypeParams {
884902
snapshot: number;
885903
project: number;
@@ -1387,6 +1405,7 @@ export interface BatchRequest {
13871405
| "getResolvedTypeReferenceDirectiveFromTypeReferenceDirective"
13881406
| "getRestTypeOfSignature"
13891407
| "getReturnTypeOfSignature"
1408+
| "getRootSymbols"
13901409
| "getSemanticDiagnostics"
13911410
| "getShorthandAssignmentValueSymbol"
13921411
| "getSignatureFromDeclaration"
@@ -1408,6 +1427,7 @@ export interface BatchRequest {
14081427
| "getSymbolsAtLocations"
14091428
| "getSymbolsAtPositions"
14101429
| "getSymbolsInScope"
1430+
| "getSymbolsOfParameterPropertyDeclaration"
14111431
| "getSymbolsOfSourceFiles"
14121432
| "getSyntacticDiagnostics"
14131433
| "getTargetOfSignature"
@@ -1419,10 +1439,12 @@ export interface BatchRequest {
14191439
| "getTrueType"
14201440
| "getTrueTypeOfConditionalType"
14211441
| "getTypeArguments"
1442+
| "getTypeArgumentsForResolvedSignature"
14221443
| "getTypeAtLocation"
14231444
| "getTypeAtLocations"
14241445
| "getTypeAtPosition"
14251446
| "getTypeFromTypeNode"
1447+
| "getTypeOfAssignmentPattern"
14261448
| "getTypeOfPropertyOfType"
14271449
| "getTypeOfSymbol"
14281450
| "getTypeOfSymbolAtLocation"
@@ -1446,8 +1468,10 @@ export interface BatchRequest {
14461468
| "isArrayType"
14471469
| "isContextSensitive"
14481470
| "isImplementationOfOverload"
1471+
| "isOptionalParameter"
14491472
| "isReadonlySymbol"
14501473
| "isTypeAssignableTo"
1474+
| "isValidPropertyAccess"
14511475
| "parseCommandLine"
14521476
| "parseConfigFile"
14531477
| "parseConfigFileTextToJson"
@@ -1591,6 +1615,7 @@ export interface BatchResponse {
15911615
| "getResolvedTypeReferenceDirectiveFromTypeReferenceDirective"
15921616
| "getRestTypeOfSignature"
15931617
| "getReturnTypeOfSignature"
1618+
| "getRootSymbols"
15941619
| "getSemanticDiagnostics"
15951620
| "getShorthandAssignmentValueSymbol"
15961621
| "getSignatureFromDeclaration"
@@ -1612,6 +1637,7 @@ export interface BatchResponse {
16121637
| "getSymbolsAtLocations"
16131638
| "getSymbolsAtPositions"
16141639
| "getSymbolsInScope"
1640+
| "getSymbolsOfParameterPropertyDeclaration"
16151641
| "getSymbolsOfSourceFiles"
16161642
| "getSyntacticDiagnostics"
16171643
| "getTargetOfSignature"
@@ -1623,10 +1649,12 @@ export interface BatchResponse {
16231649
| "getTrueType"
16241650
| "getTrueTypeOfConditionalType"
16251651
| "getTypeArguments"
1652+
| "getTypeArgumentsForResolvedSignature"
16261653
| "getTypeAtLocation"
16271654
| "getTypeAtLocations"
16281655
| "getTypeAtPosition"
16291656
| "getTypeFromTypeNode"
1657+
| "getTypeOfAssignmentPattern"
16301658
| "getTypeOfPropertyOfType"
16311659
| "getTypeOfSymbol"
16321660
| "getTypeOfSymbolAtLocation"
@@ -1650,8 +1678,10 @@ export interface BatchResponse {
16501678
| "isArrayType"
16511679
| "isContextSensitive"
16521680
| "isImplementationOfOverload"
1681+
| "isOptionalParameter"
16531682
| "isReadonlySymbol"
16541683
| "isTypeAssignableTo"
1684+
| "isValidPropertyAccess"
16551685
| "parseCommandLine"
16561686
| "parseConfigFile"
16571687
| "parseConfigFileTextToJson"

0 commit comments

Comments
 (0)