X Tutup
Skip to content

Commit e67b37a

Browse files
committed
PR feedback: Clarify how well-known symbols get decoded
1 parent db76045 commit e67b37a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

apps/api-extractor/src/analyzer/AstSymbolTable.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ export class AstSymbolTable {
229229
public static getLocalNameForSymbol(symbol: ts.Symbol): string {
230230
const symbolName: string = symbol.name;
231231

232-
if (TypeScriptHelpers.isWellKnownSymbolName(symbolName)) {
233-
// TypeScript binds well-known ECMAScript symbols like "Symbol.iterator" as "__@iterator".
234-
// This converts a string like "__@iterator" into the property name "[Symbol.iterator]".
235-
return `[Symbol.${symbolName.slice(3)}]`;
232+
// TypeScript binds well-known ECMAScript symbols like "[Symbol.iterator]" as "__@iterator".
233+
// Decode it back into "[Symbol.iterator]".
234+
const wellKnownSymbolName: string | undefined = TypeScriptHelpers.tryDecodeWellKnownSymbolName(symbolName);
235+
if (wellKnownSymbolName) {
236+
return wellKnownSymbolName;
236237
}
237238

238239
const isUniqueSymbol: boolean = TypeScriptHelpers.isUniqueSymbolName(symbolName);

apps/api-extractor/src/analyzer/TypeScriptHelpers.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,23 @@ export class TypeScriptHelpers {
212212

213213
// Matches TypeScript's encoded names for well-known ECMAScript symbols like
214214
// "__@iterator" or "__@toStringTag".
215-
private static readonly _wellKnownSymbolNameRegExp: RegExp = /^__@\w+$/;
215+
private static readonly _wellKnownSymbolNameRegExp: RegExp = /^__@(\w+)$/;
216216

217217
/**
218-
* Returns whether the provided name was generated for a built-in ECMAScript symbol.
218+
* Decodes the names that the compiler generates for a built-in ECMAScript symbol.
219+
*
220+
* @remarks
221+
* TypeScript binds well-known ECMAScript symbols like `[Symbol.iterator]` as `__@iterator`.
222+
* If `name` is of this form, then `tryGetWellKnownSymbolName()` converts it back into e.g. `[Symbol.iterator]`.
223+
* If the string does not start with `__@` then `undefined` is returned.
219224
*/
220-
public static isWellKnownSymbolName(name: string): boolean {
221-
return TypeScriptHelpers._wellKnownSymbolNameRegExp.test(name);
225+
public static tryDecodeWellKnownSymbolName(name: string): string | undefined {
226+
const match: RegExpExecArray | null = TypeScriptHelpers._wellKnownSymbolNameRegExp.exec(name);
227+
if (match) {
228+
const identifier: string = match[1];
229+
return `[Symbol.${identifier}]`;
230+
}
231+
return undefined;
222232
}
223233

224234
// Matches TypeScript's encoded names for late-bound symbols derived from `unique symbol` declarations

0 commit comments

Comments
 (0)
X Tutup