Skip to content

Commit 11fb3ef

Browse files
committed
Added description for packages implementation
1 parent 7efe683 commit 11fb3ef

16 files changed

Lines changed: 131 additions & 30 deletions

‎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' //'D:/GitRepos/sp-client/spfx-core/sp-codepart-base'
2929
},
30-
entryPointFile: 'D:/GitRepos/sp-client/spfx-core/sp-codepart-base/src/index.ts',
30+
entryPointFile: './testInputs/example2/index.ts', // 'D:/GitRepos/sp-client/spfx-core/sp-codepart-base/src/index.ts',
3131
otherFiles: []
3232
});
3333

‎api-extractor/src/PrettyPrinter.ts‎

Lines changed: 25 additions & 12 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-
4+
import TypeScriptHelpers from './TypeScriptHelpers';
55
/**
66
* Some helper functions for formatting certain TypeScript Compiler API expressions.
77
*/
@@ -10,23 +10,36 @@ export default class PrettyPrinter {
1010
* Used for debugging only. This dumps the TypeScript Compiler's abstract syntax tree.
1111
*/
1212
public static dumpTree(node: ts.Node, indent: string = ''): void {
13-
if (node.kind === ts.SyntaxKind.FromKeyword) {
14-
console.log('**');
15-
}
13+
try {
14+
const jsdoc: string = TypeScriptHelpers.getJsDocComments(node, console.log);
15+
if (jsdoc && jsdoc.trim()) {
16+
console.log('COMMENT=' + jsdoc);
17+
}
18+
} catch (e) {} /* tslint:disable-line:no-empty */
1619

1720
const kindName: string = ts.SyntaxKind[node.kind];
18-
let trimmedText: string = node.getText()
19-
.replace(/[\r\n]/g, ' ')
20-
.replace(/\s+/g, ' ')
21-
.trim();
21+
let trimmedText: string;
22+
23+
try {
24+
trimmedText = node.getText()
25+
.replace(/[\r\n]/g, ' ')
26+
.replace(/\s+/g, ' ')
27+
.trim();
2228

23-
if (trimmedText.length > 100) {
24-
trimmedText = trimmedText.substr(0, 97) + '...';
29+
if (trimmedText.length > 100) {
30+
trimmedText = trimmedText.substr(0, 97) + '...';
31+
}
32+
} catch (e) {
33+
trimmedText = '(error getting text)';
2534
}
2635

2736
console.log(`${indent}${kindName}: [${trimmedText}]`);
28-
for (const childNode of node.getChildren()) {
29-
PrettyPrinter.dumpTree(childNode, indent + ' ');
37+
try {
38+
for (const childNode of node.getChildren()) {
39+
PrettyPrinter.dumpTree(childNode, indent + ' ');
40+
}
41+
} catch (e) {
42+
// sometimes getChildren() throws an exception
3043
}
3144
}
3245

‎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`);
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: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,38 @@ 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, rootFileSymbol: ts.Symbol): IApiItemOptions {
18+
let statement: ts.VariableStatement;
19+
let foundDescription: ts.Node = undefined;
20+
for (const statementNode of (rootFileSymbol.declarations[0] as ts.SourceFile).statements) {
21+
if (statementNode.kind === ts.SyntaxKind.VariableStatement) {
22+
statement = statementNode as ts.VariableStatement;
23+
for (const statementDeclaration of statement.declarationList.declarations) {
24+
if (statementDeclaration.name.getText() === 'packageDescription') {
25+
// The following line is useful for debugging
26+
// console.log('FOUND: ' + TypeScriptHelpers.getJsDocComments(statement, console.log));
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, rootFileSymbol: ts.Symbol) {
41+
super(ApiPackage._getOptions(analyzer, rootFileSymbol));
2242

2343
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(rootFileSymbol);
2444
if (exportSymbols) {
@@ -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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ export default class ApiStructuredType extends ApiItemContainer {
182182
const memberOptions: IApiItemOptions = {
183183
analyzer: this.analyzer,
184184
declaration: memberDeclaration,
185-
declarationSymbol: memberSymbol
185+
declarationSymbol: memberSymbol,
186+
jsdocNode: memberDeclaration
186187
};
187188

188189
if (memberSymbol.flags & (

0 commit comments

Comments
 (0)