Skip to content

Commit e444346

Browse files
authored
Merge pull request microsoft#1169 from Microsoft/octogonz/ae-review-exports
[api-extractor] Improve the API review files to include import and export signatures
2 parents 0fccc1a + 83fba87 commit e444346

55 files changed

Lines changed: 472 additions & 280 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/api-extractor/build-tests.cmd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@ECHO OFF
22
@SETLOCAL
3-
rush build -t api-extractor-test-01 -t api-extractor-test-02 -t api-extractor-test-03 -t api-extractor-test-04 -t api-extractor-test-05 -t api-extractor-test-06
3+
rush build -t api-extractor-lib1-test -t api-extractor-lib2-test -t api-extractor-lib3-test -t api-extractor-scenarios -t api-extractor-test-01 -t api-extractor-test-02 -t api-extractor-test-03 -t api-extractor-test-04 -t api-documenter-test
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: 55 additions & 21 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

@@ -132,9 +137,35 @@ export class ReviewFileGenerator {
132137

133138
case ts.SyntaxKind.ExportKeyword:
134139
case ts.SyntaxKind.DefaultKeyword:
140+
case ts.SyntaxKind.DeclareKeyword:
141+
// Delete any explicit "export" or "declare" keywords -- we will re-add them below
135142
span.modification.skipAll();
136143
break;
137144

145+
case ts.SyntaxKind.InterfaceKeyword:
146+
case ts.SyntaxKind.ClassKeyword:
147+
case ts.SyntaxKind.EnumKeyword:
148+
case ts.SyntaxKind.NamespaceKeyword:
149+
case ts.SyntaxKind.ModuleKeyword:
150+
case ts.SyntaxKind.TypeKeyword:
151+
case ts.SyntaxKind.FunctionKeyword:
152+
// Replace the stuff we possibly deleted above
153+
let replacedModifiers: string = '';
154+
155+
if (entity.shouldInlineExport) {
156+
replacedModifiers = 'export ' + replacedModifiers;
157+
}
158+
159+
if (previousSpan && previousSpan.kind === ts.SyntaxKind.SyntaxList) {
160+
// If there is a previous span of type SyntaxList, then apply it before any other modifiers
161+
// (e.g. "abstract") that appear there.
162+
previousSpan.modification.prefix = replacedModifiers + previousSpan.modification.prefix;
163+
} else {
164+
// Otherwise just stick it in front of this span
165+
span.modification.prefix = replacedModifiers + span.modification.prefix;
166+
}
167+
break;
168+
138169
case ts.SyntaxKind.SyntaxList:
139170
if (span.parent) {
140171
if (AstDeclaration.isSupportedSyntaxKind(span.parent.kind)) {
@@ -165,9 +196,12 @@ export class ReviewFileGenerator {
165196
}
166197
const listPrefix: string = list.getSourceFile().text
167198
.substring(list.getStart(), list.declarations[0].getStart());
168-
span.modification.prefix = 'declare ' + listPrefix + span.modification.prefix;
169-
199+
span.modification.prefix = listPrefix + span.modification.prefix;
170200
span.modification.suffix = ';';
201+
202+
if (entity.shouldInlineExport) {
203+
span.modification.prefix = 'export ' + span.modification.prefix;
204+
}
171205
}
172206
break;
173207

‎build-tests/api-documenter-test/etc/api-documenter-test.api.md‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
```ts
66

77
// @public
8-
declare const constVariable: number;
8+
export const constVariable: number;
99

1010
// @public
11-
declare class DocBaseClass {
11+
export class DocBaseClass {
1212
}
1313

1414
// @public
15-
declare class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInterface2 {
15+
export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInterface2 {
1616
constructor(name: string);
1717
// @deprecated (undocumented)
1818
deprecatedExample(): void;
@@ -29,46 +29,46 @@ declare class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInte
2929
}
3030

3131
// @public
32-
declare enum DocEnum {
32+
export enum DocEnum {
3333
One = 1,
3434
Two = 2,
3535
Zero = 0
3636
}
3737

3838
// @public
39-
declare type ExampleTypeAlias = Promise<boolean>;
39+
export type ExampleTypeAlias = Promise<boolean>;
4040

4141
// @public
42-
declare function globalFunction(x: number): number;
42+
export function globalFunction(x: number): number;
4343

4444
// @public (undocumented)
45-
interface IDocInterface1 {
45+
export interface IDocInterface1 {
4646
regularProperty: SystemEvent;
4747
}
4848

4949
// @public (undocumented)
50-
interface IDocInterface2 extends IDocInterface1 {
50+
export interface IDocInterface2 extends IDocInterface1 {
5151
// @deprecated (undocumented)
5252
deprecatedExample(): void;
5353
}
5454

5555
// @public
56-
interface IDocInterface3 {
56+
export interface IDocInterface3 {
5757
(x: number): number;
5858
[x: string]: string;
5959
new (): IDocInterface1;
6060
}
6161

6262
// @public
63-
declare namespace OuterNamespace {
64-
namespace InnerNamespace {
65-
function nestedFunction(x: number): number;
63+
export namespace OuterNamespace {
64+
export namespace InnerNamespace {
65+
export function nestedFunction(x: number): number;
6666
}
6767
let nestedVariable: boolean;
6868
}
6969

7070
// @public
71-
declare class SystemEvent {
71+
export class SystemEvent {
7272
addHandler(handler: () => void): void;
7373
}
7474

‎build-tests/api-documenter-test/tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "commonjs",
66
"declaration": true,
77
"sourceMap": true,
8+
"declarationMap": true,
89
"experimentalDecorators": true,
910
"strictNullChecks": true,
1011
"types": [

‎build-tests/api-extractor-lib1-test/etc/api-extractor-lib1-test.api.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// Warning: (ae-forgotten-export) The symbol "Lib1ForgottenExport" needs to be exported by the entry point index.d.ts
88
//
99
// @public (undocumented)
10-
declare class Lib1Class extends Lib1ForgottenExport {
10+
export class Lib1Class extends Lib1ForgottenExport {
1111
}
1212

1313
// @alpha (undocumented)
14-
interface Lib1Interface {
14+
export interface Lib1Interface {
1515
}
1616

1717

‎build-tests/api-extractor-lib1-test/tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "commonjs",
66
"declaration": true,
77
"sourceMap": true,
8+
"declarationMap": true,
89
"experimentalDecorators": true,
910
"strictNullChecks": true,
1011
"types": [

‎build-tests/api-extractor-lib2-test/etc/api-extractor-lib2-test.api.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
```ts
66

77
// @public (undocumented)
8-
declare class Lib2Class {
8+
export class Lib2Class {
99
}
1010

1111
// @alpha (undocumented)
12-
interface Lib2Interface {
12+
export interface Lib2Interface {
1313
}
1414

1515

‎build-tests/api-extractor-lib2-test/tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "commonjs",
66
"declaration": true,
77
"sourceMap": true,
8+
"declarationMap": true,
89
"experimentalDecorators": true,
910
"strictNullChecks": true,
1011
"types": [

0 commit comments

Comments
 (0)