Skip to content

Commit e3f4ee2

Browse files
committed
Renamed Analyzer to Extractor
1 parent a17a0a8 commit e3f4ee2

9 files changed

Lines changed: 152 additions & 24 deletions

File tree

‎api-extractor/src/Extractor.ts‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import * as ts from 'typescript';
2+
import * as fsx from 'fs-extra';
3+
import * as path from 'path';
4+
import ApiPackage from './definitions/ApiPackage';
5+
import DocItemLoader from './DocItemLoader';
6+
7+
export type ApiErrorHandler = (message: string, fileName: string, lineNumber: number) => void;
8+
9+
/**
10+
* Options for Extractor contructor.
11+
*/
12+
export interface IExtractorOptions {
13+
/**
14+
* Configuration for the TypeScript compiler. The most important options to set are:
15+
*
16+
* - target: ts.ScriptTarget.ES5
17+
* - module: ts.ModuleKind.CommonJS
18+
* - moduleResolution: ts.ModuleResolutionKind.NodeJs
19+
* - rootDir: inputFolder
20+
*/
21+
compilerOptions: ts.CompilerOptions;
22+
23+
errorHandler?: ApiErrorHandler;
24+
}
25+
26+
/**
27+
* Options for Extractor.analyze()
28+
*/
29+
export interface IApiAnalyzerOptions {
30+
/**
31+
* The entry point for the project. This should correspond to the "main" field
32+
* from NPM's package.json file. If it is a relative path, it will be relative to
33+
* the project folder described by IApiAnalyzerOptions.compilerOptions.
34+
*/
35+
entryPointFile: string;
36+
37+
/**
38+
* This can be used to specify other files that should be processed by the TypeScript compiler
39+
* for some reason, e.g. a "typings/tsd.d.ts" file. It is NOT necessary to specify files that
40+
* are explicitly imported/required by the entryPointFile, since the compiler will trace
41+
* (the transitive closure of) ordinary dependencies.
42+
*/
43+
otherFiles?: string[];
44+
}
45+
46+
/**
47+
* The main entry point for the "api-extractor" utility. The Analyzer object invokes the
48+
* TypeScript Compiler API to analyze a project, and constructs the ApiItem
49+
* abstract syntax tree.
50+
*/
51+
export default class Extractor {
52+
public errorHandler: ApiErrorHandler;
53+
public typeChecker: ts.TypeChecker;
54+
public package: ApiPackage;
55+
/**
56+
* One DocItemLoader is needed per analyzer to look up external API members
57+
* as needed.
58+
*/
59+
public docItemLoader: DocItemLoader;
60+
61+
private _compilerOptions: ts.CompilerOptions;
62+
63+
/**
64+
* The default implementation of ApiErrorHandler, which merely writes to console.log().
65+
*/
66+
public static defaultErrorHandler(message: string, fileName: string, lineNumber: number): void {
67+
console.log(`ERROR: [${fileName}:${lineNumber}] ${message}`);
68+
}
69+
70+
constructor(options: IExtractorOptions) {
71+
this._compilerOptions = options.compilerOptions;
72+
this.docItemLoader = new DocItemLoader(options.compilerOptions.rootDir);
73+
this.errorHandler = options.errorHandler || Extractor.defaultErrorHandler;
74+
}
75+
76+
/**
77+
* Analyzes the specified project.
78+
*/
79+
public analyze(options: IApiAnalyzerOptions): void {
80+
const rootFiles: string[] = [options.entryPointFile].concat(options.otherFiles || []);
81+
82+
const program: ts.Program = ts.createProgram(rootFiles, this._compilerOptions);
83+
84+
// This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations)
85+
// with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday
86+
// compile errors that would result from a full compilation.
87+
for (const diagnostic of program.getSemanticDiagnostics()) {
88+
this.reportError('TypeScript: ' + diagnostic.messageText, diagnostic.file, diagnostic.start);
89+
}
90+
91+
this.typeChecker = program.getTypeChecker();
92+
93+
const rootFile: ts.SourceFile = program.getSourceFile(options.entryPointFile);
94+
if (!rootFile) {
95+
throw new Error('Unable to load file: ' + options.entryPointFile);
96+
}
97+
98+
this.package = new ApiPackage(this, rootFile);
99+
}
100+
101+
/**
102+
* Reports an error message to the registered ApiErrorHandler.
103+
*/
104+
public reportError(message: string, sourceFile: ts.SourceFile, start: number): void {
105+
const lineNumber: number = sourceFile.getLineAndCharacterOfPosition(start).line;
106+
this.errorHandler(message, sourceFile.fileName, lineNumber);
107+
}
108+
109+
/**
110+
* Scans for external package api files and loads them into the docItemLoader member before
111+
* any API analyzation begins.
112+
*
113+
* @param externalJsonCollectionPath - path to to the folder that contains all the external api json files.
114+
* Ex: if externalJsonPath is './resources', then in that folder
115+
* are 'es6-collections.api.json', 'es6-promise.api.json', etc.
116+
*/
117+
public loadExternalPackages(externalJsonCollectionPath: string): void {
118+
if (!externalJsonCollectionPath) {
119+
return;
120+
}
121+
122+
const files: string[] = fsx.readdirSync(externalJsonCollectionPath);
123+
files.forEach(file => {
124+
const externalJsonFilePath: string = path.join(externalJsonCollectionPath, file);
125+
this.docItemLoader.loadPackageIntoCache(externalJsonFilePath);
126+
});
127+
}
128+
}

‎api-extractor/src/definitions/ApiEnum.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class ApiEnum extends ApiItemContainer {
1515
const memberSymbol: ts.Symbol = TypeScriptHelpers.getSymbolForDeclaration(memberDeclaration);
1616

1717
const memberOptions: IApiItemOptions = {
18-
analyzer: this.analyzer,
18+
extractor: this.extractor,
1919
declaration: memberDeclaration,
2020
declarationSymbol: memberSymbol,
2121
jsdocNode: memberDeclaration

‎api-extractor/src/definitions/ApiFunction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ApiFunction extends ApiItem {
2727
const docComment: string = declarationSymbol && this.documentation && this.documentation.paramDocs ?
2828
this.documentation.paramDocs.get(declarationSymbol.name) : '';
2929
this.params.push(new ApiParameter({
30-
analyzer: this.analyzer,
30+
extractor: this.extractor,
3131
declaration: param,
3232
declarationSymbol: declarationSymbol,
3333
jsdocNode: param

‎api-extractor/src/definitions/ApiItem.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
/* tslint:disable:no-constant-condition */
33

44
import * as ts from 'typescript';
5-
import Analyzer from '../Analyzer';
5+
import Extractor from '../Extractor';
66
import ApiDocumentation from './ApiDocumentation';
77

88
/**
99
* This interface is used to pass options between constructors for ApiItem child classes.
1010
*/
1111
export interface IApiItemOptions {
1212
/**
13-
* The associated Analyzer object for this ApiItem
13+
* The associated Extractor object for this ApiItem
1414
*/
15-
analyzer: Analyzer;
15+
extractor: Extractor;
1616
/**
1717
* The declaration node for the main syntax item that this ApiItem is associated with.
1818
*/
@@ -53,7 +53,7 @@ abstract class ApiItem {
5353
public name: string;
5454

5555
/**
56-
* A list of analyzer warnings that were reported using ApiItem.reportWarning().
56+
* A list of extractor warnings that were reported using ApiItem.reportWarning().
5757
* Whereas an "error" will break the build, a "warning" will merely be tracked in
5858
* the API file produced by ApiFileGenerator.
5959
*/
@@ -69,9 +69,9 @@ abstract class ApiItem {
6969
public documentation: ApiDocumentation;
7070

7171
/**
72-
* The Analyzer object that acts as the root of the abstract syntax tree that this item belongs to.
72+
* The Extractor object that acts as the root of the abstract syntax tree that this item belongs to.
7373
*/
74-
protected analyzer: Analyzer;
74+
protected extractor: Extractor;
7575

7676
/**
7777
* Syntax information from the TypeScript Compiler API, corresponding to the place
@@ -108,14 +108,14 @@ abstract class ApiItem {
108108
this._errorNode = options.declaration;
109109
this.warnings = [];
110110

111-
this.analyzer = options.analyzer;
111+
this.extractor = options.extractor;
112112
this.declarationSymbol = options.declarationSymbol;
113113
this.exportSymbol = options.exportSymbol || this.declarationSymbol;
114114

115115
this.name = this.exportSymbol.name || '???';
116-
this.typeChecker = this.analyzer.typeChecker;
116+
this.typeChecker = this.extractor.typeChecker;
117117

118-
this.documentation = new ApiDocumentation(this, options.analyzer.docItemLoader, this.reportError);
118+
this.documentation = new ApiDocumentation(this, options.extractor.docItemLoader, this.reportError);
119119
}
120120

121121
/**
@@ -172,11 +172,11 @@ abstract class ApiItem {
172172
}
173173

174174
/**
175-
* Reports an error through the ApiErrorHandler interface that was registered with the Analyzer,
175+
* Reports an error through the ApiErrorHandler interface that was registered with the Extractor,
176176
* adding the filename and line number information for the declaration of this ApiItem.
177177
*/
178178
protected reportError(message: string): void {
179-
this.analyzer.reportError(message, this._errorNode.getSourceFile(), this._errorNode.getStart());
179+
this.extractor.reportError(message, this._errorNode.getSourceFile(), this._errorNode.getStart());
180180
}
181181

182182
/**

‎api-extractor/src/definitions/ApiMember.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default class ApiMember extends ApiItem {
6464
const propertyTypeSymbol: ts.Symbol = TypeScriptHelpers.getSymbolForDeclaration(propertyTypeDeclaration);
6565

6666
const typeLiteralOptions: IApiItemOptions = {
67-
analyzer: this.analyzer,
67+
extractor: this.extractor,
6868
declaration: propertyTypeDeclaration,
6969
declarationSymbol: propertyTypeSymbol,
7070
jsdocNode: propertyTypeDeclaration

‎api-extractor/src/definitions/ApiMethod.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class ApiMethod extends ApiMember {
2828
const docComment: string = declarationSymbol && this.documentation && this.documentation.paramDocs ?
2929
this.documentation.paramDocs.get(declarationSymbol.name) : '';
3030
this.params.push(new ApiParameter({
31-
analyzer: this.analyzer,
31+
extractor: this.extractor,
3232
declaration: param,
3333
declarationSymbol: declarationSymbol,
3434
jsdocNode: param

‎api-extractor/src/definitions/ApiPackage.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable:no-bitwise */
22

33
import * as ts from 'typescript';
4-
import Analyzer from '../Analyzer';
4+
import Extractor from '../Extractor';
55
import ApiStructuredType from './ApiStructuredType';
66
import ApiEnum from './ApiEnum';
77
import ApiFunction from './ApiFunction';
@@ -11,10 +11,10 @@ import TypeScriptHelpers from '../TypeScriptHelpers';
1111

1212
/**
1313
* This class is part of the ApiItem abstract syntax tree. It represents the top-level
14-
* exports for an Rush package. This object acts as the root of the Analyzer's tree.
14+
* exports for an Rush package. This object acts as the root of the Extractor's tree.
1515
*/
1616
export default class ApiPackage extends ApiItemContainer {
17-
private static _getOptions(analyzer: Analyzer, rootFile: ts.SourceFile): IApiItemOptions {
17+
private static _getOptions(extractor: Extractor, rootFile: ts.SourceFile): IApiItemOptions {
1818
const rootFileSymbol: ts.Symbol = TypeScriptHelpers.getSymbolForDeclaration(rootFile);
1919
let statement: ts.VariableStatement;
2020
let foundDescription: ts.Node = undefined;
@@ -31,14 +31,14 @@ export default class ApiPackage extends ApiItemContainer {
3131
}
3232

3333
return {
34-
analyzer,
34+
extractor,
3535
declaration: rootFileSymbol.declarations[0],
3636
declarationSymbol: rootFileSymbol,
3737
jsdocNode: foundDescription
3838
};
3939
}
40-
constructor(analyzer: Analyzer, rootFile: ts.SourceFile) {
41-
super(ApiPackage._getOptions(analyzer, rootFile));
40+
constructor(extractor: Extractor, rootFile: ts.SourceFile) {
41+
super(ApiPackage._getOptions(extractor, rootFile));
4242

4343
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(this.declarationSymbol);
4444
if (exportSymbols) {
@@ -52,7 +52,7 @@ export default class ApiPackage extends ApiItemContainer {
5252

5353
for (const declaration of followedSymbol.declarations) {
5454
const options: IApiItemOptions = {
55-
analyzer: this.analyzer,
55+
extractor: this.extractor,
5656
declaration,
5757
declarationSymbol: followedSymbol,
5858
jsdocNode: declaration,

‎api-extractor/src/definitions/ApiStructuredType.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export default class ApiStructuredType extends ApiItemContainer {
180180
this._processedMemberNames.add(memberSymbol.name);
181181

182182
const memberOptions: IApiItemOptions = {
183-
analyzer: this.analyzer,
183+
extractor: this.extractor,
184184
declaration: memberDeclaration,
185185
declarationSymbol: memberSymbol,
186186
jsdocNode: memberDeclaration

‎api-extractor/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare const packageDescription: void;
77

8-
export { default as Analyzer, IApiAnalyzerOptions, ApiErrorHandler } from './Analyzer';
8+
export { default as Extractor, IExtractorOptions, IApiAnalyzerOptions, ApiErrorHandler } from './Extractor';
99
export { default as ApiFileGenerator } from './generators/ApiFileGenerator';
1010
export { default as ApiJsonGenerator } from './generators/ApiJsonGenerator';
1111
export { default as ExternalApiHelper } from './ExternalApiHelper';

0 commit comments

Comments
 (0)