Skip to content

Commit f48ece5

Browse files
committed
ReviewFileGenerator now emits imports/exports using a similar representation as DtsRollupGenerator
1 parent f4f2208 commit f48ece5

3 files changed

Lines changed: 114 additions & 53 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as ts from 'typescript';
5+
6+
import { InternalError } from '@microsoft/node-core-library';
7+
import { CollectorEntity } from '../collector/CollectorEntity';
8+
import { AstImport, AstImportKind } from '../analyzer/AstImport';
9+
import { StringWriter } from './StringWriter';
10+
import { Collector } from '../collector/Collector';
11+
12+
/**
13+
* Some common code shared between DtsRollupGenerator and ReviewFileGenerator.
14+
*/
15+
export class DtsEmitHelpers {
16+
public static emitImport(stringWriter: StringWriter, collectorEntity: CollectorEntity, astImport: AstImport): void {
17+
switch (astImport.importKind) {
18+
case AstImportKind.NamedImport:
19+
if (collectorEntity.nameForEmit !== astImport.exportName) {
20+
stringWriter.write(`import { ${astImport.exportName} as ${collectorEntity.nameForEmit} }`);
21+
} else {
22+
stringWriter.write(`import { ${astImport.exportName} }`);
23+
}
24+
stringWriter.writeLine(` from '${astImport.modulePath}';`);
25+
break;
26+
case AstImportKind.StarImport:
27+
stringWriter.writeLine(`import * as ${collectorEntity.nameForEmit} from '${astImport.modulePath}';`);
28+
break;
29+
case AstImportKind.EqualsImport:
30+
stringWriter.writeLine(`import ${collectorEntity.nameForEmit} = require('${astImport.modulePath}');`);
31+
break;
32+
default:
33+
throw new InternalError('Unimplemented AstImportKind');
34+
}
35+
}
36+
37+
public static emitNamedExport(stringWriter: StringWriter, exportName: string,
38+
collectorEntity: CollectorEntity): void {
39+
40+
if (exportName === ts.InternalSymbolName.Default) {
41+
stringWriter.writeLine(`export default ${collectorEntity.nameForEmit};`);
42+
} else if (collectorEntity.nameForEmit !== exportName) {
43+
stringWriter.writeLine(`export { ${collectorEntity.nameForEmit} as ${exportName} }`);
44+
} else {
45+
stringWriter.writeLine(`export { ${exportName} }`);
46+
}
47+
}
48+
49+
public static emitStarExports(stringWriter: StringWriter, collector: Collector): void {
50+
if (collector.starExportedExternalModulePaths.length > 0) {
51+
stringWriter.writeLine();
52+
for (const starExportedExternalModulePath of collector.starExportedExternalModulePaths) {
53+
stringWriter.writeLine(`export * from "${starExportedExternalModulePath}";`);
54+
}
55+
}
56+
}
57+
}

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

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import { ReleaseTag } from '@microsoft/api-extractor-model';
1010
import { Collector } from '../collector/Collector';
1111
import { TypeScriptHelpers } from '../analyzer/TypeScriptHelpers';
1212
import { Span, SpanModification } from '../analyzer/Span';
13-
import { AstImport, AstImportKind } from '../analyzer/AstImport';
13+
import { AstImport } from '../analyzer/AstImport';
1414
import { CollectorEntity } from '../collector/CollectorEntity';
1515
import { AstDeclaration } from '../analyzer/AstDeclaration';
1616
import { DeclarationMetadata } from '../collector/DeclarationMetadata';
1717
import { AstSymbol } from '../analyzer/AstSymbol';
1818
import { SymbolMetadata } from '../collector/SymbolMetadata';
1919
import { StringWriter } from './StringWriter';
20+
import { DtsEmitHelpers } from './DtsEmitHelpers';
2021

2122
/**
2223
* Used with DtsRollupGenerator.writeTypingsFile()
@@ -90,25 +91,7 @@ export class DtsRollupGenerator {
9091
const releaseTag: ReleaseTag = symbolMetadata ? symbolMetadata.releaseTag : ReleaseTag.None;
9192

9293
if (this._shouldIncludeReleaseTag(releaseTag, dtsKind)) {
93-
switch (astImport.importKind) {
94-
case AstImportKind.NamedImport:
95-
if (entity.nameForEmit !== astImport.exportName) {
96-
stringWriter.write(`import { ${astImport.exportName} as ${entity.nameForEmit} }`);
97-
} else {
98-
stringWriter.write(`import { ${astImport.exportName} }`);
99-
}
100-
stringWriter.writeLine(` from '${astImport.modulePath}';`);
101-
break;
102-
case AstImportKind.StarImport:
103-
stringWriter.writeLine(`import * as ${entity.nameForEmit} from '${astImport.modulePath}';`);
104-
break;
105-
case AstImportKind.EqualsImport:
106-
stringWriter.writeLine(`import ${entity.nameForEmit} = require('${astImport.modulePath}');`);
107-
break;
108-
default:
109-
throw new InternalError('Unimplemented AstImportKind');
110-
}
111-
94+
DtsEmitHelpers.emitImport(stringWriter, entity, astImport);
11295
}
11396
}
11497
}
@@ -139,23 +122,12 @@ export class DtsRollupGenerator {
139122

140123
if (!entity.shouldInlineExport) {
141124
for (const exportName of entity.exportNames) {
142-
if (exportName === ts.InternalSymbolName.Default) {
143-
stringWriter.writeLine(`export default ${entity.nameForEmit};`);
144-
} else if (entity.nameForEmit !== exportName) {
145-
stringWriter.writeLine(`export { ${entity.nameForEmit} as ${exportName} }`);
146-
} else {
147-
stringWriter.writeLine(`export { ${exportName} }`);
148-
}
125+
DtsEmitHelpers.emitNamedExport(stringWriter, exportName, entity);
149126
}
150127
}
151128
}
152129

153-
if (collector.starExportedExternalModulePaths.length > 0) {
154-
stringWriter.writeLine();
155-
for (const starExportedExternalModulePath of collector.starExportedExternalModulePaths) {
156-
stringWriter.writeLine(`export * from "${starExportedExternalModulePath}";`);
157-
}
158-
}
130+
DtsEmitHelpers.emitStarExports(stringWriter, collector);
159131

160132
// Emit "export { }" which is a special directive that prevents consumers from importing declarations
161133
// that don't have an explicit "export" modifier.

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

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AstImport } from '../analyzer/AstImport';
1616
import { AstSymbol } from '../analyzer/AstSymbol';
1717
import { ExtractorMessage } from '../api/ExtractorMessage';
1818
import { StringWriter } from './StringWriter';
19+
import { DtsEmitHelpers } from './DtsEmitHelpers';
1920

2021
export class ReviewFileGenerator {
2122
/**
@@ -45,6 +46,20 @@ export class ReviewFileGenerator {
4546
// Write the opening delimiter for the Markdown code fence
4647
stringWriter.writeLine('```ts\n');
4748

49+
// Emit the imports
50+
let importsEmitted: boolean = false;
51+
for (const entity of collector.entities) {
52+
if (entity.astEntity instanceof AstImport) {
53+
DtsEmitHelpers.emitImport(stringWriter, entity, entity.astEntity);
54+
importsEmitted = true;
55+
}
56+
}
57+
58+
if (importsEmitted) {
59+
stringWriter.writeLine();
60+
}
61+
62+
// Emit the regular declarations
4863
for (const entity of collector.entities) {
4964
if (entity.exported) {
5065
if (entity.astEntity instanceof AstSymbol) {
@@ -58,30 +73,18 @@ export class ReviewFileGenerator {
5873
span.writeModifiedText(stringWriter.stringBuilder);
5974
stringWriter.writeLine('\n');
6075
}
61-
} else {
62-
// This definition is reexported from another package, so write it as an "export" line
63-
// In general, we don't report on external packages; if that's important we assume API Extractor
64-
// would be enabled for the upstream project. But see GitHub issue #896 for a possible exception.
65-
const astImport: AstImport = entity.astEntity;
66-
67-
if (astImport.exportName === '*') {
68-
stringWriter.write(`export * as ${entity.nameForEmit}`);
69-
} else if (entity.nameForEmit !== astImport.exportName) {
70-
stringWriter.write(`export { ${astImport.exportName} as ${entity.nameForEmit} }`);
71-
} else {
72-
stringWriter.write(`export { ${astImport.exportName} }`);
76+
}
77+
78+
for (const exportName of entity.exportNames) {
79+
if (!entity.shouldInlineExport) {
80+
DtsEmitHelpers.emitNamedExport(stringWriter, exportName, entity);
81+
stringWriter.writeLine();
7382
}
74-
stringWriter.writeLine(` from '${astImport.modulePath}';`);
7583
}
7684
}
7785
}
7886

79-
if (collector.starExportedExternalModulePaths.length > 0) {
80-
stringWriter.writeLine();
81-
for (const starExportedExternalModulePath of collector.starExportedExternalModulePaths) {
82-
stringWriter.writeLine(`export * from "${starExportedExternalModulePath}";`);
83-
}
84-
}
87+
DtsEmitHelpers.emitStarExports(stringWriter, collector);
8588

8689
// Write the unassociated warnings at the bottom of the file
8790
const unassociatedMessages: ExtractorMessage[] = collector.messageRouter
@@ -120,6 +123,8 @@ export class ReviewFileGenerator {
120123
return;
121124
}
122125

126+
const previousSpan: Span | undefined = span.previousSibling;
127+
123128
let recurseChildren: boolean = true;
124129
let sortChildren: boolean = false;
125130

@@ -135,6 +140,30 @@ export class ReviewFileGenerator {
135140
span.modification.skipAll();
136141
break;
137142

143+
case ts.SyntaxKind.InterfaceKeyword:
144+
case ts.SyntaxKind.ClassKeyword:
145+
case ts.SyntaxKind.EnumKeyword:
146+
case ts.SyntaxKind.NamespaceKeyword:
147+
case ts.SyntaxKind.ModuleKeyword:
148+
case ts.SyntaxKind.TypeKeyword:
149+
case ts.SyntaxKind.FunctionKeyword:
150+
// Replace the stuff we possibly deleted above
151+
let replacedModifiers: string = '';
152+
153+
if (entity.shouldInlineExport) {
154+
replacedModifiers = 'export ' + replacedModifiers;
155+
}
156+
157+
if (previousSpan && previousSpan.kind === ts.SyntaxKind.SyntaxList) {
158+
// If there is a previous span of type SyntaxList, then apply it before any other modifiers
159+
// (e.g. "abstract") that appear there.
160+
previousSpan.modification.prefix = replacedModifiers + previousSpan.modification.prefix;
161+
} else {
162+
// Otherwise just stick it in front of this span
163+
span.modification.prefix = replacedModifiers + span.modification.prefix;
164+
}
165+
break;
166+
138167
case ts.SyntaxKind.SyntaxList:
139168
if (span.parent) {
140169
if (AstDeclaration.isSupportedSyntaxKind(span.parent.kind)) {
@@ -166,8 +195,11 @@ export class ReviewFileGenerator {
166195
const listPrefix: string = list.getSourceFile().text
167196
.substring(list.getStart(), list.declarations[0].getStart());
168197
span.modification.prefix = 'declare ' + listPrefix + span.modification.prefix;
169-
170198
span.modification.suffix = ';';
199+
200+
if (entity.shouldInlineExport) {
201+
span.modification.prefix = 'export ' + span.modification.prefix;
202+
}
171203
}
172204
break;
173205

0 commit comments

Comments
 (0)