Skip to content

Commit c554fc1

Browse files
andrewbranchCopilot
andcommitted
Expose private symbol display names through Symbol.name
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 7321f26 commit c554fc1

7 files changed

Lines changed: 61 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,7 +3444,7 @@ export class Symbol {
34443444
}
34453445
/** The escaped (`__String`) name, used as the key in member/export tables. */
34463446
readonly escapedName: __String;
3447-
/** The display name (escaped underscores removed). */
3447+
/** The display name, including the source spelling of private identifiers. */
34483448
readonly name: string;
34493449
readonly flags: SymbolFlags;
34503450
readonly checkFlags: CheckFlags;
@@ -3466,7 +3466,7 @@ export class Symbol {
34663466
this.reference = data.reference;
34673467

34683468
this.escapedName = data.name as __String;
3469-
this.name = unescapeLeadingUnderscores(data.name as __String);
3469+
this.name = data.displayName ?? unescapeLeadingUnderscores(data.name as __String);
34703470
this.flags = data.flags;
34713471
this.checkFlags = data.checkFlags ?? 0;
34723472
this.declarationHandles = data.declarations;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ export interface GetSymbolOfDeclarationParams {
283283
export interface SymbolResponse {
284284
reference: SymbolReference;
285285
name: string;
286+
displayName?: string | undefined;
286287
flags: number;
287288
checkFlags?: number | undefined;
288289
declarations?: string[] | undefined;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7086,7 +7086,7 @@ export class Symbol {
70867086
}
70877087
/** The escaped (`__String`) name, used as the key in member/export tables. */
70887088
readonly escapedName: __String;
7089-
/** The display name (escaped underscores removed). */
7089+
/** The display name, including the source spelling of private identifiers. */
70907090
readonly name: string;
70917091
readonly flags: SymbolFlags;
70927092
readonly checkFlags: CheckFlags;
@@ -7108,7 +7108,7 @@ export class Symbol {
71087108
this.reference = data.reference;
71097109

71107110
this.escapedName = data.name as __String;
7111-
this.name = unescapeLeadingUnderscores(data.name as __String);
7111+
this.name = data.displayName ?? unescapeLeadingUnderscores(data.name as __String);
71127112
this.flags = data.flags;
71137113
this.checkFlags = data.checkFlags ?? 0;
71147114
this.declarationHandles = data.declarations;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
isJSDoc,
2020
isJSDocParameterTag,
2121
isJsxSelfClosingElement,
22+
isMethodDeclaration,
2223
isModuleDeclaration,
2324
isNamedImports,
2425
isObjectLiteralExpression,
@@ -7171,6 +7172,31 @@ notCallable();
71717172
});
71727173

71737174
describe("Symbol - escaped names and tables", { concurrency }, () => {
7175+
test("private member display names do not expose unique symbol table keys", async () => {
7176+
await using api = spawnAPI({
7177+
"/tsconfig.json": "{}",
7178+
"/src/main.ts": "export class A { #value = 1; #method() {} } export class B { #value = 2; }",
7179+
});
7180+
const snapshot = await api.createSnapshot({ openProjects: ["/tsconfig.json"] });
7181+
const project = snapshot.getConfiguredProject("/tsconfig.json")!;
7182+
const source = await project.program.getSourceFile("/src/main.ts");
7183+
assert.ok(source);
7184+
const symbols = [];
7185+
for (const declaration of source.statements.filter(isClassDeclaration)) {
7186+
for (const member of declaration.members) {
7187+
if (!(isPropertyDeclaration(member) || isMethodDeclaration(member)) || !isPrivateIdentifier(member.name)) continue;
7188+
const symbol = await project.checker.getSymbolAtLocation(member.name);
7189+
assert.ok(symbol);
7190+
assert.strictEqual(symbol.name, member.name.text);
7191+
assert.notStrictEqual(symbol.escapedName, symbol.name);
7192+
symbols.push(symbol);
7193+
}
7194+
}
7195+
assert.strictEqual(symbols.length, 3);
7196+
assert.notStrictEqual(symbols[0].escapedName, symbols[2].escapedName);
7197+
assert.strictEqual(symbols[0].name, symbols[2].name);
7198+
});
7199+
71747200
test("getExports/getMembers return a cached Map keyed by escaped name", async () => {
71757201
await using api = spawnAPI({
71767202
"/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }),

‎packages/typescript/test/sync/api.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
isJSDoc,
2828
isJSDocParameterTag,
2929
isJsxSelfClosingElement,
30+
isMethodDeclaration,
3031
isModuleDeclaration,
3132
isNamedImports,
3233
isObjectLiteralExpression,
@@ -6929,6 +6930,31 @@ notCallable();
69296930
});
69306931

69316932
describe("Symbol - escaped names and tables", { concurrency }, () => {
6933+
test("private member display names do not expose unique symbol table keys", () => {
6934+
using api = spawnAPI({
6935+
"/tsconfig.json": "{}",
6936+
"/src/main.ts": "export class A { #value = 1; #method() {} } export class B { #value = 2; }",
6937+
});
6938+
const snapshot = api.createSnapshot({ openProjects: ["/tsconfig.json"] });
6939+
const project = snapshot.getConfiguredProject("/tsconfig.json")!;
6940+
const source = project.program.getSourceFile("/src/main.ts");
6941+
assert.ok(source);
6942+
const symbols = [];
6943+
for (const declaration of source.statements.filter(isClassDeclaration)) {
6944+
for (const member of declaration.members) {
6945+
if (!(isPropertyDeclaration(member) || isMethodDeclaration(member)) || !isPrivateIdentifier(member.name)) continue;
6946+
const symbol = project.checker.getSymbolAtLocation(member.name);
6947+
assert.ok(symbol);
6948+
assert.strictEqual(symbol.name, member.name.text);
6949+
assert.notStrictEqual(symbol.escapedName, symbol.name);
6950+
symbols.push(symbol);
6951+
}
6952+
}
6953+
assert.strictEqual(symbols.length, 3);
6954+
assert.notStrictEqual(symbols[0].escapedName, symbols[2].escapedName);
6955+
assert.strictEqual(symbols[0].name, symbols[2].name);
6956+
});
6957+
69326958
test("getExports/getMembers return a cached Map keyed by escaped name", () => {
69336959
using api = spawnAPI({
69346960
"/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }),

‎tsc/internal/api/proto.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ type GetSymbolsAtLocationsParams struct {
11711171
type SymbolResponse struct {
11721172
Reference SymbolReference `json:"reference"`
11731173
Name string `json:"name"`
1174+
DisplayName string `json:"displayName,omitempty"`
11741175
Flags uint32 `json:"flags"`
11751176
CheckFlags uint32 `json:"checkFlags,omitzero"`
11761177
Declarations []NodeHandle `json:"declarations,omitempty"`

‎tsc/internal/api/session.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ func buildSymbolResponse(symbol *ast.Symbol, reference SymbolReference, owner *a
254254
Parent: newSymbolReference(symbol.Parent()),
255255
ExportSymbol: newSymbolReference(symbol.ExportSymbol()),
256256
}
257+
if name := ast.SymbolName(symbol); name != symbol.Name() {
258+
resp.DisplayName = name
259+
}
257260
if owner != nil {
258261
// A client resolves a file-owned symbol's relationships through its own source file.
259262
compilerdebug.Assert(symbol.Parent() == nil || symbolOwnerFile(symbol.Parent()) == owner, "File-owned symbol parent belongs to another owner")

0 commit comments

Comments
 (0)