Skip to content

Commit e369990

Browse files
[@typescript/api] Add a few more checker APIs (#2972)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e2eed29 commit e369990

14 files changed

Lines changed: 1532 additions & 4 deletions

File tree

Herebyfile.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ const enumDefs = [
313313
{ name: "SignatureFlags", goPrefix: "SignatureFlags", goFile: "internal/checker/types.go", outDir: "_packages/api/src/enums" },
314314
{ name: "SignatureKind", goPrefix: "SignatureKind", goFile: "internal/checker/types.go", outDir: "_packages/api/src/enums" },
315315
{ name: "ElementFlags", goPrefix: "ElementFlags", goFile: "internal/checker/types.go", outDir: "_packages/api/src/enums" },
316+
{ name: "TypePredicateKind", goPrefix: "TypePredicateKind", goFile: "internal/checker/types.go", outDir: "_packages/api/src/enums" },
316317
// @typescript/ast enums
317318
{ name: "SyntaxKind", goPrefix: "Kind", goFile: "internal/ast/kind.go", outDir: "_packages/ast/src/enums" },
318319
{ name: "NodeFlags", goPrefix: "NodeFlags", goFile: "internal/ast/nodeflags.go", outDir: "_packages/ast/src/enums" },

_packages/api/src/async/api.ts

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SignatureFlags } from "#enums/signatureFlags";
55
import { SignatureKind } from "#enums/signatureKind";
66
import { SymbolFlags } from "#enums/symbolFlags";
77
import { TypeFlags } from "#enums/typeFlags";
8+
import { TypePredicateKind } from "#enums/typePredicateKind";
89
import type {
910
Expression,
1011
Node,
@@ -39,11 +40,13 @@ import type {
3940
ConfigResponse,
4041
DocumentIdentifier,
4142
DocumentPosition,
43+
IndexInfoResponse,
4244
InitializeResponse,
4345
LSPUpdateSnapshotParams,
4446
ProjectResponse,
4547
SignatureResponse,
4648
SymbolResponse,
49+
TypePredicateResponse,
4750
TypeResponse,
4851
UpdateSnapshotParams,
4952
UpdateSnapshotResponse,
@@ -56,8 +59,12 @@ import {
5659
type ClientSpawnOptions,
5760
} from "./client.ts";
5861
import type {
62+
AssertsIdentifierTypePredicate,
63+
AssertsThisTypePredicate,
5964
ConditionalType,
65+
IdentifierTypePredicate,
6066
IndexedAccessType,
67+
IndexInfo,
6168
IndexType,
6269
InterfaceType,
6370
IntersectionType,
@@ -66,17 +73,20 @@ import type {
6673
StringMappingType,
6774
SubstitutionType,
6875
TemplateLiteralType,
76+
ThisTypePredicate,
6977
TupleType,
7078
Type,
7179
TypeParameter,
80+
TypePredicate,
81+
TypePredicateBase,
7282
TypeReference,
7383
UnionOrIntersectionType,
7484
UnionType,
7585
} from "./types.ts";
7686

77-
export { ElementFlags, ObjectFlags, SignatureFlags, SignatureKind, SymbolFlags, TypeFlags };
87+
export { ElementFlags, ObjectFlags, SignatureFlags, SignatureKind, SymbolFlags, TypeFlags, TypePredicateKind };
7888
export type { APIOptions, ClientSocketOptions, ClientSpawnOptions, DocumentIdentifier, DocumentPosition, LSPConnectionOptions };
79-
export type { ConditionalType, IndexedAccessType, IndexType, InterfaceType, IntersectionType, LiteralType, ObjectType, StringMappingType, SubstitutionType, TemplateLiteralType, TupleType, TypeParameter, TypeReference, UnionOrIntersectionType, UnionType };
89+
export type { AssertsIdentifierTypePredicate, AssertsThisTypePredicate, ConditionalType, IdentifierTypePredicate, IndexedAccessType, IndexInfo, IndexType, InterfaceType, IntersectionType, LiteralType, ObjectType, StringMappingType, SubstitutionType, TemplateLiteralType, ThisTypePredicate, TupleType, TypeParameter, TypePredicate, TypePredicateBase, TypeReference, UnionOrIntersectionType, UnionType };
8090
export { documentURIToFileName, fileNameToDocumentURI } from "../path.ts";
8191

8292
/** Type alias for the snapshot-scoped object registry */
@@ -601,6 +611,97 @@ export class Checker {
601611
flags,
602612
});
603613
}
614+
615+
async isContextSensitive(node: Node): Promise<boolean> {
616+
return this.client.apiRequest<boolean>("isContextSensitive", {
617+
snapshot: this.snapshotId,
618+
project: this.projectId,
619+
location: getNodeId(node),
620+
});
621+
}
622+
623+
async getReturnTypeOfSignature(signature: Signature): Promise<Type | undefined> {
624+
const data = await this.client.apiRequest<TypeResponse | null>("getReturnTypeOfSignature", {
625+
snapshot: this.snapshotId,
626+
project: this.projectId,
627+
signature: signature.id,
628+
});
629+
return data ? this.objectRegistry.getOrCreateType(data) : undefined;
630+
}
631+
632+
async getRestTypeOfSignature(signature: Signature): Promise<Type | undefined> {
633+
const data = await this.client.apiRequest<TypeResponse | null>("getRestTypeOfSignature", {
634+
snapshot: this.snapshotId,
635+
project: this.projectId,
636+
signature: signature.id,
637+
});
638+
return data ? this.objectRegistry.getOrCreateType(data) : undefined;
639+
}
640+
641+
async getTypePredicateOfSignature(signature: Signature): Promise<TypePredicate | undefined> {
642+
const data = await this.client.apiRequest<TypePredicateResponse | null>("getTypePredicateOfSignature", {
643+
snapshot: this.snapshotId,
644+
project: this.projectId,
645+
signature: signature.id,
646+
});
647+
if (!data) return undefined;
648+
return {
649+
kind: data.kind,
650+
parameterIndex: data.parameterIndex,
651+
parameterName: data.parameterName,
652+
type: data.type ? this.objectRegistry.getOrCreateType(data.type) : undefined,
653+
} as TypePredicate;
654+
}
655+
656+
async getBaseTypes(type: Type): Promise<readonly Type[]> {
657+
const data = await this.client.apiRequest<TypeResponse[] | null>("getBaseTypes", {
658+
snapshot: this.snapshotId,
659+
project: this.projectId,
660+
type: type.id,
661+
});
662+
return data ? data.map(d => this.objectRegistry.getOrCreateType(d)) : [];
663+
}
664+
665+
async getPropertiesOfType(type: Type): Promise<readonly Symbol[]> {
666+
const data = await this.client.apiRequest<SymbolResponse[] | null>("getPropertiesOfType", {
667+
snapshot: this.snapshotId,
668+
project: this.projectId,
669+
type: type.id,
670+
});
671+
return data ? data.map(d => this.objectRegistry.getOrCreateSymbol(d)) : [];
672+
}
673+
674+
async getIndexInfosOfType(type: Type): Promise<readonly IndexInfo[]> {
675+
const data = await this.client.apiRequest<IndexInfoResponse[] | null>("getIndexInfosOfType", {
676+
snapshot: this.snapshotId,
677+
project: this.projectId,
678+
type: type.id,
679+
});
680+
if (!data) return [];
681+
return data.map(d => ({
682+
keyType: this.objectRegistry.getOrCreateType(d.keyType),
683+
valueType: this.objectRegistry.getOrCreateType(d.valueType),
684+
isReadonly: d.isReadonly ?? false,
685+
}));
686+
}
687+
688+
async getConstraintOfTypeParameter(type: Type): Promise<Type | undefined> {
689+
const data = await this.client.apiRequest<TypeResponse | null>("getConstraintOfTypeParameter", {
690+
snapshot: this.snapshotId,
691+
project: this.projectId,
692+
type: type.id,
693+
});
694+
return data ? this.objectRegistry.getOrCreateType(data) : undefined;
695+
}
696+
697+
async getTypeArguments(type: Type): Promise<readonly Type[]> {
698+
const data = await this.client.apiRequest<TypeResponse[] | null>("getTypeArguments", {
699+
snapshot: this.snapshotId,
700+
project: this.projectId,
701+
type: type.id,
702+
});
703+
return data ? data.map(d => this.objectRegistry.getOrCreateType(d)) : [];
704+
}
604705
}
605706

606707
export class Emitter {

_packages/api/src/async/types.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ElementFlags } from "#enums/elementFlags";
22
import type { ObjectFlags } from "#enums/objectFlags";
33
import type { TypeFlags } from "#enums/typeFlags";
4+
import type { TypePredicateKind } from "#enums/typePredicateKind";
45
import type { Symbol } from "./api.ts";
56

67
/**
@@ -122,3 +123,54 @@ export interface StringMappingType extends Type {
122123
/** Get the mapped type */
123124
getTarget(): Promise<Type>;
124125
}
126+
127+
/** Base for all type predicates */
128+
export interface TypePredicateBase {
129+
readonly kind: TypePredicateKind;
130+
readonly type: Type | undefined;
131+
}
132+
133+
/** `this is T` */
134+
export interface ThisTypePredicate extends TypePredicateBase {
135+
readonly kind: TypePredicateKind.This;
136+
readonly parameterName: undefined;
137+
readonly parameterIndex: undefined;
138+
readonly type: Type;
139+
}
140+
141+
/** `x is T` */
142+
export interface IdentifierTypePredicate extends TypePredicateBase {
143+
readonly kind: TypePredicateKind.Identifier;
144+
readonly parameterName: string;
145+
readonly parameterIndex: number;
146+
readonly type: Type;
147+
}
148+
149+
/** `asserts this is T` */
150+
export interface AssertsThisTypePredicate extends TypePredicateBase {
151+
readonly kind: TypePredicateKind.AssertsThis;
152+
readonly parameterName: undefined;
153+
readonly parameterIndex: undefined;
154+
readonly type: Type | undefined;
155+
}
156+
157+
/** `asserts x is T` */
158+
export interface AssertsIdentifierTypePredicate extends TypePredicateBase {
159+
readonly kind: TypePredicateKind.AssertsIdentifier;
160+
readonly parameterName: string;
161+
readonly parameterIndex: number;
162+
readonly type: Type | undefined;
163+
}
164+
165+
/** A type predicate — e.g. `x is T` or `asserts x is T` */
166+
export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertsThisTypePredicate | AssertsIdentifierTypePredicate;
167+
168+
/** An index signature — e.g. `[key: string]: T` */
169+
export interface IndexInfo {
170+
/** The index key type (e.g. string or number) */
171+
readonly keyType: Type;
172+
/** The index value type */
173+
readonly valueType: Type;
174+
/** Whether the index signature is readonly */
175+
readonly isReadonly: boolean;
176+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3+
// !!! THIS FILE IS AUTO-GENERATED — DO NOT EDIT !!!
4+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5+
//
6+
// Source: internal/checker/types.go
7+
// Regenerate: npx hereby generate:enums
8+
//
9+
export enum TypePredicateKind {
10+
This = 0,
11+
Identifier = 1,
12+
AssertsThis = 2,
13+
AssertsIdentifier = 3,
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3+
// !!! THIS FILE IS AUTO-GENERATED — DO NOT EDIT !!!
4+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5+
//
6+
// Source: internal/checker/types.go
7+
// Regenerate: npx hereby generate:enums
8+
//
9+
export var TypePredicateKind: any;
10+
(function (TypePredicateKind) {
11+
TypePredicateKind[TypePredicateKind["This"] = 0] = "This";
12+
TypePredicateKind[TypePredicateKind["Identifier"] = 1] = "Identifier";
13+
TypePredicateKind[TypePredicateKind["AssertsThis"] = 2] = "AssertsThis";
14+
TypePredicateKind[TypePredicateKind["AssertsIdentifier"] = 3] = "AssertsIdentifier";
15+
})(TypePredicateKind || (TypePredicateKind = {}));

_packages/api/src/proto.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,16 @@ export interface SignatureResponse {
166166
thisParameter?: string;
167167
target?: string;
168168
}
169+
170+
export interface TypePredicateResponse {
171+
kind: number;
172+
parameterIndex: number;
173+
parameterName?: string;
174+
type?: TypeResponse;
175+
}
176+
177+
export interface IndexInfoResponse {
178+
keyType: TypeResponse;
179+
valueType: TypeResponse;
180+
isReadonly?: boolean;
181+
}

0 commit comments

Comments
 (0)