Skip to content

Commit 251e31f

Browse files
clean code
1 parent c036f68 commit 251e31f

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

apps/api-extractor/src/generators/ApiReportGenerator.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ export class ApiReportGenerator {
309309
}
310310
if (
311311
ts.isModuleDeclaration(astDeclaration.declaration) &&
312-
!astDeclaration.declaration.modifiers?.includes(span.node as ts.Modifier)
312+
(TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.ExportDeclaration) ||
313+
TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.VariableStatement))
313314
) {
314-
// Keep it as is for nested declarations. Handle special cases inside namespace. e.g.:
315-
// - export declaration: "export {};", "export { A as B };"
316-
// - variable declaration: "export const a: number, b: number;"
315+
// Keep it as is for nested declarations.
316+
// (special cases inside namespace. e.g. "export {};", "export const a: number;")
317317
break;
318318
}
319319

@@ -392,6 +392,10 @@ export class ApiReportGenerator {
392392
}
393393
break;
394394

395+
case ts.SyntaxKind.ExportSpecifier:
396+
DtsEmitHelpers.modifyExportSpecifierSpan(collector, span);
397+
break;
398+
395399
case ts.SyntaxKind.Identifier:
396400
const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(
397401
span.node as ts.Identifier
@@ -404,17 +408,6 @@ export class ApiReportGenerator {
404408
}
405409

406410
span.modification.prefix = referencedEntity.nameForEmit;
407-
408-
if (
409-
ts.isExportSpecifier(span.node.parent) &&
410-
!span.node.parent.propertyName &&
411-
span.node.getText().trim() !== referencedEntity.nameForEmit
412-
) {
413-
// For "export { A }" (inside namespace), if "A" is renamed (e.g. renamed to "B"),
414-
// we need to emit as "export { B as A }" instead
415-
span.modification.prefix += ' as ' + span.node.getText().trim();
416-
}
417-
418411
// For debugging:
419412
// span.modification.prefix += '/*R=FIX*/';
420413
} else {

apps/api-extractor/src/generators/DtsEmitHelpers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ export class DtsEmitHelpers {
9393
}
9494
}
9595

96+
public static modifyExportSpecifierSpan(collector: Collector, span: Span): void {
97+
// For "export { A }" (inside namespace), we need to emit as "export { AcutalName as A }"
98+
const node: ts.ExportSpecifier = span.node as ts.ExportSpecifier;
99+
if (!node.propertyName) {
100+
if (!ts.isIdentifier(node.name)) {
101+
throw new Error(
102+
`Export specifier name should be an identifier, but got ${ts.SyntaxKind[node.name.kind]}.\n` +
103+
SourceFileLocationFormatter.formatDeclaration(node)
104+
);
105+
}
106+
107+
const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(node.name);
108+
if (!referencedEntity?.nameForEmit) {
109+
throw new InternalError('referencedEntry or referencedEntry.nameForEmit is undefined');
110+
}
111+
112+
const exportName: string = node.name.getText().trim();
113+
if (referencedEntity.nameForEmit !== exportName) {
114+
span.modification.skipAll();
115+
span.modification.prefix = `${referencedEntity.nameForEmit} as ${span.getText()}`;
116+
}
117+
}
118+
}
119+
96120
public static modifyImportTypeSpan(
97121
collector: Collector,
98122
span: Span,

apps/api-extractor/src/generators/DtsRollupGenerator.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ export class DtsRollupGenerator {
277277
}
278278
if (
279279
ts.isModuleDeclaration(astDeclaration.declaration) &&
280-
!astDeclaration.declaration.modifiers?.includes(span.node as ts.Modifier)
280+
(TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.ExportDeclaration) ||
281+
TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.VariableStatement))
281282
) {
282-
// Keep it as is for nested declarations. Handle special cases inside namespace. e.g.:
283-
// - export declaration: "export {};", "export { A as B };"
284-
// - variable declaration: "export const a: number, b: number;"
283+
// Keep it as is for nested declarations.
284+
// (special cases inside namespace. e.g. "export {};", "export const a: number;")
285285
break;
286286
}
287287

@@ -366,6 +366,10 @@ export class DtsRollupGenerator {
366366
}
367367
break;
368368

369+
case ts.SyntaxKind.ExportSpecifier:
370+
DtsEmitHelpers.modifyExportSpecifierSpan(collector, span);
371+
break;
372+
369373
case ts.SyntaxKind.Identifier:
370374
{
371375
const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(
@@ -379,17 +383,6 @@ export class DtsRollupGenerator {
379383
}
380384

381385
span.modification.prefix = referencedEntity.nameForEmit;
382-
383-
if (
384-
ts.isExportSpecifier(span.node.parent) &&
385-
!span.node.parent.propertyName &&
386-
span.node.getText().trim() !== referencedEntity.nameForEmit
387-
) {
388-
// For "export { A }" (inside namespace), if "A" is renamed (e.g. renamed to "B"),
389-
// we need to emit as "export { B as A }" instead
390-
span.modification.prefix += ' as ' + span.node.getText().trim();
391-
}
392-
393386
// For debugging:
394387
// span.modification.prefix += '/*R=FIX*/';
395388
} else {

build-tests/api-extractor-scenarios/src/namespaceDeclaration/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @public */
12
export namespace PartalExportedNS {
23
export const var1 = 1,
34
var2 = 2;
@@ -7,16 +8,19 @@ export namespace PartalExportedNS {
78
}
89
}
910

11+
/** @public */
1012
export namespace AllExportedNS {
1113
export class ExportedClass {}
1214
export interface ExportedInterface {}
1315
}
1416

17+
/** @public */
1518
export declare namespace ReexportNS {
1619
export { RenamedClass };
1720
export { RenamedClass as RenamedClass3 };
1821
}
1922

23+
/** @public */
2024
class RenamedClass {}
2125

2226
export { RenamedClass as FinalRenamedClass };

0 commit comments

Comments
 (0)