Skip to content

Commit 601a22e

Browse files
committed
Added support for external doc json files inherited docs
1 parent e3f4ee2 commit 601a22e

14 files changed

Lines changed: 168 additions & 204 deletions

‎api-extractor/src/Analyzer.ts‎

Lines changed: 0 additions & 96 deletions
This file was deleted.

‎api-extractor/src/DebugRun.ts‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,47 @@
33

44
import * as ts from 'typescript';
55
import * as os from 'os';
6-
import Analyzer from './Analyzer';
6+
import * as path from 'path';
7+
import Extractor from './Extractor';
78
import ApiFileGenerator from './generators/ApiFileGenerator';
89
import ApiJsonGenerator from './generators/ApiJsonGenerator';
10+
import { IApiDefinitionReference } from './IApiDefinitionReference';
911

10-
const analyzer: Analyzer = new Analyzer(
11-
(message: string, fileName: string, lineNumber: number): void => {
12-
console.log(`ErrorHandler: ${message}` + os.EOL
13-
+ ` ${fileName}#${lineNumber}`);
14-
}
15-
);
16-
17-
/**
18-
* Debugging inheritdoc expression parser.
19-
* Analyzer on example2 is needed for testing the parser.
20-
*/
21-
analyzer.analyze({
22-
compilerOptions: {
23-
target: ts.ScriptTarget.ES5,
24-
module: ts.ModuleKind.CommonJS,
25-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
26-
experimentalDecorators: true,
27-
jsx: ts.JsxEmit.React,
28-
rootDir: './testInputs/example2'
29-
},
30-
entryPointFile: './testInputs/example2/index.ts',
31-
otherFiles: []
12+
const compilerOptions: ts.CompilerOptions = {
13+
target: ts.ScriptTarget.ES5,
14+
module: ts.ModuleKind.CommonJS,
15+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
16+
experimentalDecorators: true,
17+
jsx: ts.JsxEmit.React,
18+
rootDir: './testInputs/example2'
19+
};
20+
21+
const extractor: Extractor = new Extractor( {
22+
compilerOptions: compilerOptions,
23+
errorHandler:
24+
(message: string, fileName: string, lineNumber: number): void => {
25+
console.log(`ErrorHandler: ${message}` + os.EOL
26+
+ ` ${fileName}#${lineNumber}`);
27+
}
3228
});
3329

30+
extractor.loadExternalPackages(path.join(__dirname, '/external-api-json'));
31+
32+
extractor.analyze({entryPointFile: './testInputs/example2/index.ts',
33+
otherFiles: []});
34+
35+
const externalPackageApiRef: IApiDefinitionReference = {
36+
scopeName: '',
37+
packageName: 'es6-collections',
38+
exportName: '',
39+
memberName: ''
40+
};
41+
console.log(extractor.docItemLoader.getPackage(externalPackageApiRef));
42+
3443
const apiFileGenerator: ApiFileGenerator = new ApiFileGenerator();
35-
apiFileGenerator.writeApiFile('./lib/DebugRun.api.ts', analyzer);
44+
apiFileGenerator.writeApiFile('./lib/DebugRun.api.ts', extractor);
3645

3746
const apiJsonGenerator: ApiJsonGenerator = new ApiJsonGenerator();
38-
apiJsonGenerator.writeJsonFile('./lib/DebugRun.json', analyzer);
47+
apiJsonGenerator.writeJsonFile('./lib/DebugRun.json', extractor);
3948

4049
console.log('DebugRun completed.');

‎api-extractor/src/DocItemLoader.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export default class DocItemLoader {
100100
cachePackageName = apiDefinitionRef.packageName;
101101
}
102102
// Check if package exists in cache
103-
if (cachePackageName in this.cache) {
104-
return this.cache[cachePackageName];
103+
if (this.cache.has(cachePackageName)) {
104+
return this.cache.get(cachePackageName);
105105
}
106106

107107
// Doesn't exist in cache, attempt to load the json file
@@ -113,6 +113,14 @@ export default class DocItemLoader {
113113
`dist/${apiDefinitionRef.packageName}.api.json`
114114
);
115115

116+
return this.loadPackageIntoCache(packageJsonFilePath);
117+
}
118+
119+
/**
120+
* Loads the API documentation json file and validates that it conforms to our schema. If it does,
121+
* then the json file is saved in the cache and returned.
122+
*/
123+
public loadPackageIntoCache(packageJsonFilePath: string): IDocPackage {
116124
if (!fsx.existsSync(path.join(packageJsonFilePath))) {
117125
return undefined;
118126
}
@@ -132,8 +140,8 @@ export default class DocItemLoader {
132140
}
133141
);
134142

135-
// JSON schema has been found and validated
136-
this.cache[cachePackageName] = apiPackage;
143+
const packageName: string = path.basename(packageJsonFilePath).split('.').shift();
144+
this.cache.set(packageName, apiPackage);
137145
return apiPackage;
138146
}
139147
}

‎api-extractor/src/ExternalApiHelper.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ts from 'typescript';
22
import * as os from 'os';
33
import * as path from 'path';
44
import * as fs from 'fs';
5-
import Analyzer from './Analyzer';
5+
import Extractor from './Extractor';
66
import ApiJsonGenerator from './generators/ApiJsonGenerator';
77

88
/**
@@ -23,12 +23,22 @@ export default class ExternalApiHelper {
2323
* Ex: 'resources/external-api-json/es6-promise/index.t.ds'
2424
*/
2525
public static generateApiJson(rootDir: string, libFolder: string, externalPackageFilePath: string): void {
26-
const analyzer: Analyzer = new Analyzer(
26+
const compilerOptions: ts.CompilerOptions = {
27+
target: ts.ScriptTarget.ES5,
28+
module: ts.ModuleKind.CommonJS,
29+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
30+
experimentalDecorators: true,
31+
jsx: ts.JsxEmit.React,
32+
rootDir: rootDir
33+
};
34+
const extractor: Extractor = new Extractor( {
35+
compilerOptions: compilerOptions,
36+
errorHandler:
2737
(message: string, fileName: string, lineNumber: number): void => {
2838
console.log(`TypeScript error: ${message}` + os.EOL
2939
+ ` ${fileName}#${lineNumber}`);
3040
}
31-
);
41+
});
3242

3343
let outputPath: string = path.join(rootDir, libFolder);
3444
if (!fs.existsSync(outputPath)) {
@@ -44,21 +54,13 @@ export default class ExternalApiHelper {
4454
const outputApiJsonFilePath: string = path.join(outputPath, `${path.basename(externalPackageRootDir)}.api.json`);
4555
const entryPointFile: string = path.join(rootDir, externalPackageFilePath);
4656

47-
analyzer.analyze({
48-
compilerOptions: {
49-
target: ts.ScriptTarget.ES5,
50-
module: ts.ModuleKind.CommonJS,
51-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
52-
experimentalDecorators: true,
53-
jsx: ts.JsxEmit.React,
54-
rootDir: rootDir
55-
},
57+
extractor.analyze({
5658
entryPointFile: entryPointFile, // local/bundles/platform-exports.ts',
5759
otherFiles: []
5860
});
5961

6062
const apiJsonGenerator: ApiJsonGenerator = new ApiJsonGenerator();
61-
apiJsonGenerator.writeJsonFile(outputApiJsonFilePath, analyzer);
63+
apiJsonGenerator.writeJsonFile(outputApiJsonFilePath, extractor);
6264

6365
}
6466
}

‎api-extractor/src/TestRun.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33

44
import * as ts from 'typescript';
55
import * as path from 'path';
6-
import Analyzer from './Analyzer';
6+
import Extractor from './Extractor';
77
import ApiFileGenerator from './generators/ApiFileGenerator';
88

9-
const analyzer: Analyzer = new Analyzer();
10-
119
const inputFolder: string = './testInputs/example1';
1210
const outputFile: string = './lib/example1-output.ts';
1311

14-
analyzer.analyze({
15-
compilerOptions: {
16-
target: ts.ScriptTarget.ES5,
17-
module: ts.ModuleKind.CommonJS,
18-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
19-
rootDir: inputFolder
20-
},
12+
const compilerOptions: ts.CompilerOptions = {
13+
target: ts.ScriptTarget.ES5,
14+
module: ts.ModuleKind.CommonJS,
15+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
16+
rootDir: inputFolder
17+
};
18+
const extractor: Extractor = new Extractor({
19+
compilerOptions: compilerOptions,
20+
errorHandler: console.log
21+
});
22+
23+
extractor.analyze({
2124
entryPointFile: path.join(inputFolder, 'index.ts')
2225
});
2326

2427
const apiFileGenerator: ApiFileGenerator = new ApiFileGenerator();
25-
apiFileGenerator.writeApiFile(outputFile, analyzer);
28+
apiFileGenerator.writeApiFile(outputFile, extractor);

‎api-extractor/src/definitions/test/ApiDocumentation.test.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { assert } from 'chai';
44
import * as ts from 'typescript';
55
import * as path from 'path';
6-
import Analyzer from '../../Analyzer';
6+
import Extractor from '../../Extractor';
77
import ApiStructuredType from '../ApiStructuredType';
88
import ApiDocumentation, { ApiTag } from '../ApiDocumentation';
99
import { IApiDefinitionReference } from '../../IApiDefinitionReference';
@@ -20,31 +20,34 @@ function testErrorHandler(message: string, fileName: string, lineNumber: number)
2020
capturedErrors.push({ message, fileName, lineNumber });
2121
}
2222

23-
const analyzer: Analyzer = new Analyzer(testErrorHandler);
2423
const inputFolder: string = './testInputs/example2';
24+
const compilerOptions: ts.CompilerOptions = {
25+
target: ts.ScriptTarget.ES5,
26+
module: ts.ModuleKind.CommonJS,
27+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
28+
rootDir: inputFolder
29+
};
30+
const extractor: Extractor = new Extractor({
31+
compilerOptions: compilerOptions,
32+
errorHandler: testErrorHandler
33+
});
2534
let myDocumentedClass: ApiStructuredType;
2635

2736
/**
2837
* Dummy class wrapping ApiDocumentation to test its protected methods
2938
*/
3039
class TestApiDocumentation extends ApiDocumentation {
3140
constructor() {
32-
super(myDocumentedClass, analyzer.docItemLoader, (msg: string) => { return; });
41+
super(myDocumentedClass, extractor.docItemLoader, (msg: string) => { return; });
3342
}
3443
}
3544

3645
// Run the analyzer once to be used by unit tests
37-
analyzer.analyze({
38-
compilerOptions: {
39-
target: ts.ScriptTarget.ES5,
40-
module: ts.ModuleKind.CommonJS,
41-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
42-
rootDir: inputFolder
43-
},
46+
extractor.analyze({
4447
entryPointFile: path.join(inputFolder, 'index.ts')
4548
});
4649

47-
myDocumentedClass = analyzer.package.getSortedMemberItems()
50+
myDocumentedClass = extractor.package.getSortedMemberItems()
4851
.filter(apiItem => apiItem.name === 'MyDocumentedClass')[0] as ApiStructuredType;
4952

5053
describe('ApiDocumentation tests', function (): void {

‎api-extractor/src/generators/ApiFileGenerator.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'fs';
2-
import Analyzer from '../Analyzer';
2+
import Extractor from '../Extractor';
33
import ApiStructuredType, { ApiStructuredTypeKind } from '../definitions/ApiStructuredType';
44
import ApiEnum from '../definitions/ApiEnum';
55
import ApiEnumValue from '../definitions/ApiEnumValue';
@@ -40,17 +40,17 @@ export default class ApiFileGenerator extends ApiItemVisitor {
4040

4141
/**
4242
* Generates the report and writes it to disk.
43-
* @param reportFilename The output filename
44-
* @param analyzer An Analyzer object representing the input project.
43+
* @param reportFilename - The output filename
44+
* @param extractor - An Analyzer object representing the input project.
4545
*/
46-
public writeApiFile(reportFilename: string, analyzer: Analyzer): void {
47-
const fileContent: string = this.generateApiFileContent(analyzer);
46+
public writeApiFile(reportFilename: string, extractor: Extractor): void {
47+
const fileContent: string = this.generateApiFileContent(extractor);
4848
fs.writeFileSync(reportFilename, fileContent);
4949
}
5050

51-
public generateApiFileContent(analyzer: Analyzer): string {
51+
public generateApiFileContent(extractor: Extractor): string {
5252
// Normalize to CRLF
53-
this.visit(analyzer.package);
53+
this.visit(extractor.package);
5454
const fileContent: string = this._indentedWriter.toString().replace(/\r?\n/g, '\r\n');
5555
return fileContent;
5656
}

0 commit comments

Comments
 (0)