Skip to content

Commit 5abb503

Browse files
committed
Generation of unique names
1 parent bcc4a85 commit 5abb503

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

‎apps/api-extractor/src/generators/DtsGenerator.ts‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Span } from './Span';
1414

1515
class Entry {
1616
public localName: string;
17+
public uniqueName: string | undefined = undefined;
1718
public followedSymbol: ts.Symbol;
1819
public exported: boolean = false;
1920

@@ -135,11 +136,14 @@ export default class DtsGenerator {
135136
entry.exported = true;
136137
}
137138

139+
this._makeUniqueNames();
140+
138141
this._entries.sort((a, b) => a.getSortKey().localeCompare(b.getSortKey()));
139142

140143
let content: string = '';
141144
for (const entry of this._entries) {
142145
for (const declaration of entry.followedSymbol.declarations) {
146+
content += '// ====> ' + entry.uniqueName + '\n';
143147
content += declaration.getText() + '\n';
144148
}
145149
}
@@ -149,6 +153,18 @@ export default class DtsGenerator {
149153
return fileContent;
150154
}
151155

156+
private _makeUniqueNames(): void {
157+
const usedNames: Set<string> = new Set<string>();
158+
for (const entry of this._entries) {
159+
let suffix: number = 1;
160+
entry.uniqueName = entry.localName;
161+
while (usedNames.has(entry.uniqueName)) {
162+
entry.uniqueName = entry.localName + '_' + ++suffix;
163+
}
164+
usedNames.add(entry.uniqueName);
165+
}
166+
}
167+
152168
private _fetchEntryForSymbol(symbol: ts.Symbol): Entry | undefined {
153169
const result: IFollowAliasesResult = DtsGenerator._followAliases(symbol, this._typeChecker);
154170
if (result.external) {
@@ -162,14 +178,19 @@ export default class DtsGenerator {
162178
return undefined;
163179
}
164180

165-
let entry: Entry = this._entriesBySymbol.get(result.symbol);
181+
let entry: Entry = this._entriesBySymbol.get(followedSymbol);
166182
if (entry) {
167183
return entry;
168184
}
169185

170-
entry = this._createEntry(symbol.name, result.symbol);
186+
entry = new Entry();
187+
entry.localName = symbol.name;
188+
entry.followedSymbol = followedSymbol;
189+
this._entries.push(entry);
190+
this._entriesBySymbol.set(followedSymbol, entry);
191+
console.log('======> ' + entry.localName);
171192

172-
for (const declaration of result.symbol.declarations) {
193+
for (const declaration of followedSymbol.declarations) {
173194
// console.log(PrettyPrinter.dumpTree(declaration));
174195
console.log('-------------------------------------');
175196
const span: Span = new Span(declaration);
@@ -212,14 +233,4 @@ export default class DtsGenerator {
212233

213234
this._fetchEntryForSymbol(symbol);
214235
}
215-
216-
private _createEntry(localName: string, followedSymbol: ts.Symbol): Entry {
217-
console.log('======> ' + localName);
218-
const entry: Entry = new Entry();
219-
entry.localName = localName;
220-
entry.followedSymbol = followedSymbol;
221-
this._entries.push(entry);
222-
this._entriesBySymbol.set(followedSymbol, entry);
223-
return entry;
224-
}
225236
}

0 commit comments

Comments
 (0)