Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/typescript/src/api/async/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,21 @@ export class Checker {
return data ? this.objectRegistry.getOrCreateSymbol(data) : undefined;
}

/**
* Get the target symbol if instantiated, or the provided symbol otherwise.
*/
async getTargetSymbol(symbol: Symbol): Promise<Symbol> {
if (symbol.checkFlags & CheckFlags.Instantiated) {
const data = await this.client.apiRequest("getTargetSymbol", {
snapshot: this.snapshotId,
project: this.project.id,
symbol: symbol.id,
});
return this.objectRegistry.getOrCreateSymbol(data);
}
return symbol;
}

/**
* Fetch (once, then cache) the handle ids of the per-checker singleton
* symbols (unknown, undefined, arguments). These ids are stable for the life
Expand Down
1 change: 1 addition & 0 deletions packages/typescript/src/api/proto.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface APIMethodInfo {
getExportSpecifierLocalTargetSymbol: APIMethod<CheckerNodeParams, SymbolResponse | null>;
getAliasedSymbol: APIMethod<CheckerSymbolParams, SymbolResponse>;
getImmediateAliasedSymbol: APIMethod<CheckerSymbolParams, SymbolResponse | null>;
getTargetSymbol: APIMethod<CheckerSymbolParams, SymbolResponse>;
getFullyQualifiedName: APIMethod<CheckerSymbolParams, string>;
getExportsOfModule: APIMethod<CheckerSymbolParams, SymbolResponse[] | null>;
getMemberInModuleExports: APIMethod<GetMemberInModuleExportsParams, SymbolResponse | null>;
Expand Down
15 changes: 15 additions & 0 deletions packages/typescript/src/api/sync/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,21 @@ export class Checker {
return data ? this.objectRegistry.getOrCreateSymbol(data) : undefined;
}

/**
* Get the target symbol if instantiated, or the provided symbol otherwise.
*/
getTargetSymbol(symbol: Symbol): Symbol {
if (symbol.checkFlags & CheckFlags.Instantiated) {
const data = this.client.apiRequest("getTargetSymbol", {
snapshot: this.snapshotId,
project: this.project.id,
symbol: symbol.id,
});
return this.objectRegistry.getOrCreateSymbol(data);
}
return symbol;
}

/**
* Fetch (once, then cache) the handle ids of the per-checker singleton
* symbols (unknown, undefined, arguments). These ids are stable for the life
Expand Down
43 changes: 43 additions & 0 deletions packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getSynthesizedDeepClone,
InternalSymbolName,
isCallExpression,
isExpressionStatement,
isFunctionDeclaration,
isIdentifier,
isImportDeclaration,
Expand Down Expand Up @@ -403,6 +404,48 @@ describe("Checker - getImmediateAliasedSymbol", () => {
});
});

describe("Checker - getTargetSymbol", () => {
test("gets the target symbol of instantiated symbol", async () => {
const api = spawnAPI({
"/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }),
"/src/main.ts": `
class Base<T> {
private value!: T;
}
class Alpha extends Base<string> {}
class Bravo extends Base<string> {}

declare function test<T>(): void;
test<Alpha>();
test<Bravo>();
`,
});
try {
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
const project = snapshot.getProject("/tsconfig.json")!;
const sourceFile = await project.program.getSourceFile("/src/main.ts");
assert.ok(sourceFile);
const nodes: Array<Node> = [];
sourceFile.forEachChild(node => {
if (isExpressionStatement(node) && isCallExpression(node.expression) && node.expression.typeArguments) {
nodes.push(node.expression.typeArguments[0]);
}
});
const aType = await project.checker.getTypeAtLocation(nodes[0]);
const bType = await project.checker.getTypeAtLocation(nodes[1]);
const aProperty = (await project.checker.getPropertiesOfType(aType))[0];
const bProperty = (await project.checker.getPropertiesOfType(bType))[0];
assert.ok(aProperty);
assert.ok(bProperty);
assert.equal(aProperty === bProperty, false);
assert.equal(await project.checker.getTargetSymbol(aProperty) === await project.checker.getTargetSymbol(bProperty), true);
}
finally {
await api.close();
}
});
});

describe("Snapshot", () => {
test("updateSnapshot returns snapshot with projects", async () => {
const api = spawnAPI();
Expand Down
43 changes: 43 additions & 0 deletions packages/typescript/test/sync/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getSynthesizedDeepClone,
InternalSymbolName,
isCallExpression,
isExpressionStatement,
isFunctionDeclaration,
isIdentifier,
isImportDeclaration,
Expand Down Expand Up @@ -411,6 +412,48 @@ describe("Checker - getImmediateAliasedSymbol", () => {
});
});

describe("Checker - getTargetSymbol", () => {
test("gets the target symbol of instantiated symbol", () => {
const api = spawnAPI({
"/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }),
"/src/main.ts": `
class Base<T> {
private value!: T;
}
class Alpha extends Base<string> {}
class Bravo extends Base<string> {}

declare function test<T>(): void;
test<Alpha>();
test<Bravo>();
`,
});
try {
const snapshot = api.updateSnapshot({ openProject: "/tsconfig.json" });
const project = snapshot.getProject("/tsconfig.json")!;
const sourceFile = project.program.getSourceFile("/src/main.ts");
assert.ok(sourceFile);
const nodes: Array<Node> = [];
sourceFile.forEachChild(node => {
if (isExpressionStatement(node) && isCallExpression(node.expression) && node.expression.typeArguments) {
nodes.push(node.expression.typeArguments[0]);
}
});
const aType = project.checker.getTypeAtLocation(nodes[0]);
const bType = project.checker.getTypeAtLocation(nodes[1]);
const aProperty = (project.checker.getPropertiesOfType(aType))[0];
const bProperty = (project.checker.getPropertiesOfType(bType))[0];
assert.ok(aProperty);
assert.ok(bProperty);
assert.equal(aProperty === bProperty, false);
assert.equal(project.checker.getTargetSymbol(aProperty) === project.checker.getTargetSymbol(bProperty), true);
}
finally {
api.close();
}
});
});

describe("Snapshot", () => {
test("updateSnapshot returns snapshot with projects", () => {
const api = spawnAPI();
Expand Down
2 changes: 2 additions & 0 deletions tsc/internal/api/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const (
MethodGetExportSpecifierLocalTarget Method = "getExportSpecifierLocalTargetSymbol"
MethodGetAliasedSymbol Method = "getAliasedSymbol"
MethodGetImmediateAliasedSymbol Method = "getImmediateAliasedSymbol"
MethodGetTargetSymbol Method = "getTargetSymbol"
MethodGetFullyQualifiedName Method = "getFullyQualifiedName"
MethodGetExportsOfModule Method = "getExportsOfModule"
MethodGetMemberInModuleExports Method = "getMemberInModuleExports"
Expand Down Expand Up @@ -501,6 +502,7 @@ var unmarshalers = map[Method]func([]byte) (any, error){
MethodGetExportSpecifierLocalTarget: unmarshallerFor[CheckerNodeParams],
MethodGetAliasedSymbol: unmarshallerFor[CheckerSymbolParams],
MethodGetImmediateAliasedSymbol: unmarshallerFor[CheckerSymbolParams],
MethodGetTargetSymbol: unmarshallerFor[CheckerSymbolParams],
MethodGetFullyQualifiedName: unmarshallerFor[CheckerSymbolParams],
MethodGetExportsOfModule: unmarshallerFor[CheckerSymbolParams],
MethodGetMemberInModuleExports: unmarshallerFor[GetMemberInModuleExportsParams],
Expand Down
19 changes: 19 additions & 0 deletions tsc/internal/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ func (s *Session) HandleRequest(ctx context.Context, method string, params json.
return s.handleGetAliasedSymbol(ctx, parsed.(*CheckerSymbolParams))
case string(MethodGetImmediateAliasedSymbol):
return s.handleGetImmediateAliasedSymbol(ctx, parsed.(*CheckerSymbolParams))
case string(MethodGetTargetSymbol):
return s.handleMethodGetTargetSymbol(ctx, parsed.(*CheckerSymbolParams))
case string(MethodGetFullyQualifiedName):
return s.handleGetFullyQualifiedName(ctx, parsed.(*CheckerSymbolParams))
case string(MethodGetExportsOfModule):
Expand Down Expand Up @@ -3319,6 +3321,23 @@ func (s *Session) handleGetImmediateAliasedSymbol(ctx context.Context, params *C
return setup.newSymbolResponse(aliased), nil
}

// handleGetTargetSymbol returns the target symbol if the symbol is instantiated,
// otherwise returns the provided symbol.
func (s *Session) handleMethodGetTargetSymbol(ctx context.Context, params *CheckerSymbolParams) (*SymbolResponse, error) {
setup, err := s.setupChecker(ctx, params.Snapshot, params.Project)
if err != nil {
return nil, err
}
defer setup.done()

symbol, err := setup.resolveSymbolHandle(params.Symbol)
if err != nil {
return nil, err
}

return setup.newSymbolResponse(setup.checker.GetTargetSymbol(symbol)), nil
}

// handleGetExportsOfModule returns the resolved exports of a module symbol,
// including those introduced by `export *` and re-exports.
// @gen-proto-nullable
Expand Down
1 change: 0 additions & 1 deletion tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21777,7 +21777,6 @@ func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name s
func (c *Checker) getTargetSymbol(s *ast.Symbol) *ast.Symbol {
// if symbol is instantiated its flags are not copied from the 'target'
// so we'll need to get back original 'target' symbol to work with correct set of flags
// NOTE: cast to TransientSymbol should be safe because only TransientSymbols have CheckFlags.Instantiated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was relevant in the TypeScript code base, but not anymore.

if s != nil && s.CheckFlags&ast.CheckFlagsInstantiated != 0 {
return c.valueSymbolLinks.Get(s).target
}
Expand Down
4 changes: 4 additions & 0 deletions tsc/internal/checker/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (c *Checker) GetImmediateAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
return c.getImmediateAliasedSymbol(symbol)
}

func (c *Checker) GetTargetSymbol(symbol *ast.Symbol) *ast.Symbol {
return c.getTargetSymbol(symbol)
}

func (c *Checker) GetTypeOnlyAliasDeclaration(symbol *ast.Symbol) *ast.Node {
return c.getTypeOnlyAliasDeclaration(symbol)
}
Expand Down