Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-extractor] Fix issue where API signatures were sometimes truncated #1253

Merged
merged 2 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/api-extractor/src/generators/ApiModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class ApiModelGenerator {

const excerptTokens: IExcerptToken[] = ExcerptBuilder.build({
startingNode: astDeclaration.declaration,
nodeToStopAt: ts.SyntaxKind.FirstPunctuation, // FirstPunctuation = "{"
stopBeforeChildKind: ts.SyntaxKind.FirstPunctuation, // FirstPunctuation = "{"
nodesToCapture
});
const docComment: tsdoc.DocComment | undefined = this._collector.fetchMetadata(astDeclaration).tsdocComment;
Expand Down Expand Up @@ -328,7 +328,7 @@ export class ApiModelGenerator {
if (apiEnum === undefined) {
const excerptTokens: IExcerptToken[] = ExcerptBuilder.build({
startingNode: astDeclaration.declaration,
nodeToStopAt: ts.SyntaxKind.FirstPunctuation // FirstPunctuation = "{"
stopBeforeChildKind: ts.SyntaxKind.FirstPunctuation // FirstPunctuation = "{"
});

const docComment: tsdoc.DocComment | undefined = this._collector.fetchMetadata(astDeclaration).tsdocComment;
Expand Down Expand Up @@ -467,7 +467,7 @@ export class ApiModelGenerator {

const excerptTokens: IExcerptToken[] = ExcerptBuilder.build({
startingNode: astDeclaration.declaration,
nodeToStopAt: ts.SyntaxKind.FirstPunctuation, // FirstPunctuation = "{"
stopBeforeChildKind: ts.SyntaxKind.FirstPunctuation, // FirstPunctuation = "{"
nodesToCapture
});

Expand Down Expand Up @@ -563,7 +563,7 @@ export class ApiModelGenerator {
if (apiNamespace === undefined) {
const excerptTokens: IExcerptToken[] = ExcerptBuilder.build({
startingNode: astDeclaration.declaration,
nodeToStopAt: ts.SyntaxKind.ModuleBlock // ModuleBlock = the "{ ... }" block
stopBeforeChildKind: ts.SyntaxKind.ModuleBlock // ModuleBlock = the "{ ... }" block
});

const docComment: tsdoc.DocComment | undefined = this._collector.fetchMetadata(astDeclaration).tsdocComment;
Expand Down
32 changes: 21 additions & 11 deletions apps/api-extractor/src/generators/ExcerptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ export interface ISignatureBuilderOptions {
/**
* The AST node that we will traverse to extract tokens
*/

startingNode: ts.Node;

/**
* An AST node to stop at (e.g. the "{" after a class declaration).
* If omitted, then all child nodes for `startingNode` will be processed
* Normally, the excerpt will include all child nodes for `startingNode`; whereas if `childKindToStopBefore`
* is specified, then the node traversal will stop before (i.e. excluding) the first immediate child
* of `startingNode` with the specified syntax kind.
*
* @remarks
* For example, suppose the signature is `interface X: Y { z: string }`. The token `{` has syntax kind
* `ts.SyntaxKind.FirstPunctuation`, so we can specify that to truncate the excerpt to `interface X: Y`.
*/
nodeToStopAt?: ts.SyntaxKind;
stopBeforeChildKind?: ts.SyntaxKind;

/**
* A list of child nodes whose token ranges we want to capture
Expand All @@ -50,7 +55,9 @@ export interface ISignatureBuilderOptions {
* Internal state for ExcerptBuilder
*/
interface IBuildSpanState {
nodeToStopAt?: ts.SyntaxKind;
startingNode: ts.Node;
stopBeforeChildKind: ts.SyntaxKind | undefined;

tokenRangesByNode: Map<ts.Node, IExcerptTokenRange>;

/**
Expand All @@ -75,7 +82,8 @@ export class ExcerptBuilder {
const excerptTokens: IExcerptToken[] = [];

ExcerptBuilder._buildSpan(excerptTokens, span, {
nodeToStopAt: options.nodeToStopAt,
startingNode: options.startingNode,
stopBeforeChildKind: options.stopBeforeChildKind,
tokenRangesByNode,
disableMergingForNextToken: false
});
Expand All @@ -88,11 +96,6 @@ export class ExcerptBuilder {
}

private static _buildSpan(excerptTokens: IExcerptToken[], span: Span, state: IBuildSpanState): boolean {

if (state.nodeToStopAt && span.kind === state.nodeToStopAt) {
return false;
}

if (span.kind === ts.SyntaxKind.JSDocComment) {
// Discard any comments
return true;
Expand All @@ -119,6 +122,13 @@ export class ExcerptBuilder {
}

for (const child of span.children) {
if (span.node === state.startingNode) {
if (state.stopBeforeChildKind && child.kind === state.stopBeforeChildKind) {
// We reached the a child whose kind is stopBeforeChildKind, so stop traversing
return false;
}
}

if (!this._buildSpan(excerptTokens, child, state)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "Fix an issue where API signatures were sometimes truncated in the .api.json file (GitHub #1249)",
"type": "patch"
}
],
"packageName": "@microsoft/api-extractor",
"email": "[email protected]"
}