Skip to content

Commit a17a0a8

Browse files
authored
Merge pull request microsoft#65 from Microsoft/dagaeta/package-comments
Added description for packages implementation
2 parents 7efe683 + 35dea39 commit a17a0a8

19 files changed

Lines changed: 179 additions & 65 deletions

‎api-extractor/src/Analyzer.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as ts from 'typescript';
22
import ApiPackage from './definitions/ApiPackage';
3-
import TypeScriptHelpers from './TypeScriptHelpers';
43
import DocItemLoader from './DocItemLoader';
54

65
export type ApiErrorHandler = (message: string, fileName: string, lineNumber: number) => void;
@@ -83,9 +82,8 @@ export default class Analyzer {
8382
if (!rootFile) {
8483
throw new Error('Unable to load file: ' + options.entryPointFile);
8584
}
86-
const rootFileSymbol: ts.Symbol = TypeScriptHelpers.getSymbolForDeclaration(rootFile);
8785

88-
this.package = new ApiPackage(this, rootFileSymbol);
86+
this.package = new ApiPackage(this, rootFile);
8987
}
9088

9189
/**

‎api-extractor/src/DebugRun.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ analyzer.analyze({
2525
moduleResolution: ts.ModuleResolutionKind.NodeJs,
2626
experimentalDecorators: true,
2727
jsx: ts.JsxEmit.React,
28-
rootDir: 'D:/GitRepos/sp-client/spfx-core/sp-codepart-base'
28+
rootDir: './testInputs/example2'
2929
},
30-
entryPointFile: 'D:/GitRepos/sp-client/spfx-core/sp-codepart-base/src/index.ts',
30+
entryPointFile: './testInputs/example2/index.ts',
3131
otherFiles: []
3232
});
3333

‎api-extractor/src/PrettyPrinter.ts‎

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

33
import * as ts from 'typescript';
4-
54
/**
65
* Some helper functions for formatting certain TypeScript Compiler API expressions.
76
*/
@@ -10,23 +9,28 @@ export default class PrettyPrinter {
109
* Used for debugging only. This dumps the TypeScript Compiler's abstract syntax tree.
1110
*/
1211
public static dumpTree(node: ts.Node, indent: string = ''): void {
13-
if (node.kind === ts.SyntaxKind.FromKeyword) {
14-
console.log('**');
15-
}
16-
1712
const kindName: string = ts.SyntaxKind[node.kind];
18-
let trimmedText: string = node.getText()
19-
.replace(/[\r\n]/g, ' ')
20-
.replace(/\s+/g, ' ')
21-
.trim();
13+
let trimmedText: string;
14+
15+
try {
16+
trimmedText = node.getText()
17+
.replace(/[\r\n]/g, ' ')
18+
.replace(/\s+/g, ' ')
19+
.trim();
2220

23-
if (trimmedText.length > 100) {
24-
trimmedText = trimmedText.substr(0, 97) + '...';
21+
if (trimmedText.length > 100) {
22+
trimmedText = trimmedText.substr(0, 97) + '...';
23+
}
24+
} catch (e) {
25+
trimmedText = '(error getting text)';
2526
}
2627

27-
console.log(`${indent}${kindName}: [${trimmedText}]`);
28-
for (const childNode of node.getChildren()) {
29-
PrettyPrinter.dumpTree(childNode, indent + ' ');
28+
try {
29+
for (const childNode of node.getChildren()) {
30+
PrettyPrinter.dumpTree(childNode, indent + ' ');
31+
}
32+
} catch (e) {
33+
// sometimes getChildren() throws an exception
3034
}
3135
}
3236

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import DocItemLoader from '../DocItemLoader';
1010
import { IApiDefinitionReference } from '../IApiDefinitionReference';
1111
import Token, { TokenType } from '../Token';
1212
import Tokenizer from '../Tokenizer';
13+
import ApiPackage from './ApiPackage';
1314

1415
/**
1516
* An "API Tag" is a custom JSDoc tag which indicates whether an ApiItem definition
@@ -22,27 +23,27 @@ export enum ApiTag {
2223
/**
2324
* No API Tag was specified in the JSDoc summary.
2425
*/
25-
None,
26+
None = 0,
2627
/**
2728
* The API was documented as internal, i.e. not callable by third party developers.
2829
*/
29-
Internal,
30+
Internal = 1,
3031
/**
3132
* The API was documented as "alpha." This status is not generally used. See the
3233
* ApiPrinciplesAndProcess.md for details.
3334
*/
34-
Alpha,
35+
Alpha = 2,
3536
/**
3637
* The API was documented as callable by third party developers, but at their own risk.
3738
* Web parts that call beta APIs should only be used for experimentation, because the Web Part
3839
* will break if Microsoft changes the API signature later.
3940
*/
40-
Beta,
41+
Beta = 3,
4142
/**
4243
* The API was documented as callable by third party developers, with a guarantee that Microsoft will
4344
* never make any breaking changes once the API is published.
4445
*/
45-
Public
46+
Public = 4
4647
}
4748

4849
/**
@@ -253,8 +254,10 @@ export default class ApiDocumentation {
253254
}
254255

255256
protected _getJsDocs(apiItem: ApiItem): string {
256-
const sourceFile: ts.SourceFile = apiItem.getDeclaration().getSourceFile();
257-
const jsDoc: string = TypeScriptHelpers.getJsDocComments(apiItem.getDeclaration(), this.reportError);
257+
if (!apiItem.jsdocNode) {
258+
return '';
259+
}
260+
const jsDoc: string = TypeScriptHelpers.getJsDocComments(apiItem.jsdocNode, this.reportError);
258261

259262
// Eliminate tags and then count the English letters. Are there at least 10 letters of text?
260263
// If not, we consider the definition to be "missingDocumentation".
@@ -402,6 +405,14 @@ export default class ApiDocumentation {
402405
this.reportError('More than one API Tag was specified');
403406
}
404407

408+
if (this.apiItem instanceof ApiPackage) {
409+
if (apiTagCount > 0) {
410+
const tag: string = '@' + ApiTag[this.apiTag].toLowerCase();
411+
this.reportError(`The ${tag} tag is not allowed on the package, which is always public`);
412+
}
413+
this.apiTag = ApiTag.Public;
414+
}
415+
405416
if (this.preapproved) {
406417
if (this.apiTag !== ApiTag.Internal) {
407418
this.reportError('The @preapproved tag may only be applied to @internal defintions');

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default class ApiEnum extends ApiItemContainer {
1717
const memberOptions: IApiItemOptions = {
1818
analyzer: this.analyzer,
1919
declaration: memberDeclaration,
20-
declarationSymbol: memberSymbol
20+
declarationSymbol: memberSymbol,
21+
jsdocNode: memberDeclaration
2122
};
2223

2324
this.addMemberItem(new ApiEnumValue(memberOptions));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class ApiFunction extends ApiItem {
2929
this.params.push(new ApiParameter({
3030
analyzer: this.analyzer,
3131
declaration: param,
32-
declarationSymbol: declarationSymbol
32+
declarationSymbol: declarationSymbol,
33+
jsdocNode: param
3334
}, docComment));
3435
}
3536
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,27 @@ import ApiDocumentation from './ApiDocumentation';
99
* This interface is used to pass options between constructors for ApiItem child classes.
1010
*/
1111
export interface IApiItemOptions {
12+
/**
13+
* The associated Analyzer object for this ApiItem
14+
*/
1215
analyzer: Analyzer;
16+
/**
17+
* The declaration node for the main syntax item that this ApiItem is associated with.
18+
*/
1319
declaration: ts.Declaration;
20+
/**
21+
* The semantic information for the declaration.
22+
*/
1423
declarationSymbol: ts.Symbol;
24+
/**
25+
* The declaration node that contains the JSDoc comments for this ApiItem.
26+
* In most cases this is the same as `declaration`, but for ApiPackage it will be
27+
* a separate node under the root.
28+
*/
29+
jsdocNode: ts.Node;
30+
/**
31+
* The symbol used to export this ApiItem from the ApiPackage.
32+
*/
1533
exportSymbol?: ts.Symbol;
1634
}
1735

@@ -41,6 +59,13 @@ abstract class ApiItem {
4159
*/
4260
public warnings: string[];
4361

62+
/**
63+
* The declaration node that contains the JSDoc comments for this ApiItem.
64+
* In most cases this is the same as `declaration`, but for ApiPackage it will be
65+
* a separate node under the root.
66+
*/
67+
public jsdocNode: ts.Node;
68+
4469
public documentation: ApiDocumentation;
4570

4671
/**
@@ -79,6 +104,7 @@ abstract class ApiItem {
79104
this.reportError = this.reportError.bind(this);
80105

81106
this.declaration = options.declaration;
107+
this.jsdocNode = options.jsdocNode;
82108
this._errorNode = options.declaration;
83109
this.warnings = [];
84110

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export default class ApiMember extends ApiItem {
6666
const typeLiteralOptions: IApiItemOptions = {
6767
analyzer: this.analyzer,
6868
declaration: propertyTypeDeclaration,
69-
declarationSymbol: propertyTypeSymbol
69+
declarationSymbol: propertyTypeSymbol,
70+
jsdocNode: propertyTypeDeclaration
7071
};
7172

7273
this.typeLiteral = new ApiStructuredType(typeLiteralOptions);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export default class ApiMethod extends ApiMember {
3030
this.params.push(new ApiParameter({
3131
analyzer: this.analyzer,
3232
declaration: param,
33-
declarationSymbol: declarationSymbol
33+
declarationSymbol: declarationSymbol,
34+
jsdocNode: param
3435
}, docComment));
3536
}
3637
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,40 @@ import ApiEnum from './ApiEnum';
77
import ApiFunction from './ApiFunction';
88
import { IApiItemOptions } from './ApiItem';
99
import ApiItemContainer from './ApiItemContainer';
10+
import TypeScriptHelpers from '../TypeScriptHelpers';
1011

1112
/**
1213
* This class is part of the ApiItem abstract syntax tree. It represents the top-level
1314
* exports for an Rush package. This object acts as the root of the Analyzer's tree.
1415
*/
1516
export default class ApiPackage extends ApiItemContainer {
16-
constructor(analyzer: Analyzer, rootFileSymbol: ts.Symbol) {
17-
super({
17+
private static _getOptions(analyzer: Analyzer, rootFile: ts.SourceFile): IApiItemOptions {
18+
const rootFileSymbol: ts.Symbol = TypeScriptHelpers.getSymbolForDeclaration(rootFile);
19+
let statement: ts.VariableStatement;
20+
let foundDescription: ts.Node = undefined;
21+
22+
for (const statementNode of rootFile.statements) {
23+
if (statementNode.kind === ts.SyntaxKind.VariableStatement) {
24+
statement = statementNode as ts.VariableStatement;
25+
for (const statementDeclaration of statement.declarationList.declarations) {
26+
if (statementDeclaration.name.getText() === 'packageDescription') {
27+
foundDescription = statement;
28+
}
29+
}
30+
}
31+
}
32+
33+
return {
1834
analyzer,
1935
declaration: rootFileSymbol.declarations[0],
20-
declarationSymbol: rootFileSymbol
21-
});
36+
declarationSymbol: rootFileSymbol,
37+
jsdocNode: foundDescription
38+
};
39+
}
40+
constructor(analyzer: Analyzer, rootFile: ts.SourceFile) {
41+
super(ApiPackage._getOptions(analyzer, rootFile));
2242

23-
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(rootFileSymbol);
43+
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(this.declarationSymbol);
2444
if (exportSymbols) {
2545
for (const exportSymbol of exportSymbols) {
2646
const followedSymbol: ts.Symbol = this.followAliases(exportSymbol);
@@ -35,6 +55,7 @@ export default class ApiPackage extends ApiItemContainer {
3555
analyzer: this.analyzer,
3656
declaration,
3757
declarationSymbol: followedSymbol,
58+
jsdocNode: declaration,
3859
exportSymbol
3960
};
4061

0 commit comments

Comments
 (0)