forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExcerptBuilder.ts
More file actions
280 lines (240 loc) · 9.05 KB
/
Copy pathExcerptBuilder.ts
File metadata and controls
280 lines (240 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as ts from 'typescript';
import type { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
import {
ExcerptTokenKind,
type IExcerptToken,
type IExcerptTokenRange
} from '@microsoft/api-extractor-model';
import { Span } from '../analyzer/Span';
import type { DeclarationReferenceGenerator } from './DeclarationReferenceGenerator';
import type { AstDeclaration } from '../analyzer/AstDeclaration';
import { condenseTokens } from './condenseTokens';
/**
* Used to provide ExcerptBuilder with a list of nodes whose token range we want to capture.
*/
export interface IExcerptBuilderNodeTransform {
/**
* The node to process
*/
node: ts.Node;
/**
* A token range whose startIndex/endIndex will be overwritten with the indexes for the
* tokens corresponding to IExcerptBuilderNodeTransform.node
*/
captureTokenRange?: IExcerptTokenRange;
/**
* Text that will replace the text of the given node during emit.
*/
replacementText?: string;
}
/**
* Internal state for ExcerptBuilder
*/
interface IBuildSpanState {
referenceGenerator: DeclarationReferenceGenerator;
/**
* The AST node that we will traverse to extract tokens
*/
startingNode: ts.Node;
/**
* 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`.
*/
stopBeforeChildKind: ts.SyntaxKind | undefined;
transformsByNode: Map<ts.Node, IExcerptBuilderNodeTransform>;
/**
* Tracks whether the last appended token was a separator. If so, and we're in the middle of
* capturing a token range, then omit the separator from the range.
*/
lastAppendedTokenIsSeparator: boolean;
}
export class ExcerptBuilder {
/**
* Appends a blank line to the `excerptTokens` list.
* @param excerptTokens - The target token list to append to
*/
public static addBlankLine(excerptTokens: IExcerptToken[]): void {
let newlines: string = '\n\n';
// If the existing text already ended with a newline, then only append one newline
if (excerptTokens.length > 0) {
const previousText: string = excerptTokens[excerptTokens.length - 1].text;
if (/\n$/.test(previousText)) {
newlines = '\n';
}
}
excerptTokens.push({ kind: ExcerptTokenKind.Content, text: newlines });
}
/**
* Appends the signature for the specified `AstDeclaration` to the `excerptTokens` list.
* @param excerptTokens - The target token list to append to
* @param nodeTransforms - A list of child nodes whose token ranges we want to capture
*/
public static addDeclaration(
excerptTokens: IExcerptToken[],
astDeclaration: AstDeclaration,
nodeTransforms: IExcerptBuilderNodeTransform[],
referenceGenerator: DeclarationReferenceGenerator
): void {
let stopBeforeChildKind: ts.SyntaxKind | undefined = undefined;
switch (astDeclaration.declaration.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.EnumDeclaration:
case ts.SyntaxKind.InterfaceDeclaration:
// FirstPunctuation = "{"
stopBeforeChildKind = ts.SyntaxKind.FirstPunctuation;
break;
case ts.SyntaxKind.ModuleDeclaration:
// ModuleBlock = the "{ ... }" block
stopBeforeChildKind = ts.SyntaxKind.ModuleBlock;
break;
}
const span: Span = new Span(astDeclaration.declaration);
const transformsByNode: Map<ts.Node, IExcerptBuilderNodeTransform> = new Map();
const captureTokenRanges: IExcerptTokenRange[] = [];
for (const nodeTransform of nodeTransforms || []) {
transformsByNode.set(nodeTransform.node, nodeTransform);
if (nodeTransform.captureTokenRange) {
captureTokenRanges.push(nodeTransform.captureTokenRange);
}
}
_buildSpan(excerptTokens, span, {
referenceGenerator: referenceGenerator,
startingNode: span.node,
stopBeforeChildKind,
transformsByNode: transformsByNode,
lastAppendedTokenIsSeparator: false
});
condenseTokens(excerptTokens, captureTokenRanges);
}
public static createEmptyTokenRange(): IExcerptTokenRange {
return { startIndex: 0, endIndex: 0 };
}
}
/** @returns false if we encountered a token that causes iteration to stop. */
function _buildSpan(excerptTokens: IExcerptToken[], span: Span, state: IBuildSpanState): boolean {
if (span.kind === ts.SyntaxKind.JSDocComment) {
// Discard any comments
return true;
}
// Can this node start a excerpt?
const transform: IExcerptBuilderNodeTransform | undefined = state.transformsByNode.get(span.node);
let captureTokenRange: IExcerptTokenRange | undefined = undefined;
if (transform) {
captureTokenRange = transform.captureTokenRange;
if (transform.replacementText !== undefined) {
excerptTokens.push({
kind: ExcerptTokenKind.Content,
text: transform.replacementText
});
state.lastAppendedTokenIsSeparator = false;
if (captureTokenRange) {
captureTokenRange.startIndex = excerptTokens.length;
captureTokenRange.endIndex = captureTokenRange.startIndex + 1;
}
return true;
}
}
let excerptStartIndex: number = 0;
if (captureTokenRange) {
// We will assign capturedTokenRange.startIndex to be the index of the next token to be appended
excerptStartIndex = excerptTokens.length;
}
if (span.prefix) {
let canonicalReference: DeclarationReference | undefined = undefined;
if (span.kind === ts.SyntaxKind.Identifier) {
const name: ts.Identifier = span.node as ts.Identifier;
if (!_isDeclarationName(name)) {
canonicalReference = state.referenceGenerator.getDeclarationReferenceForIdentifier(name);
}
}
if (canonicalReference) {
_appendToken(excerptTokens, ExcerptTokenKind.Reference, span.prefix, canonicalReference);
} else {
_appendToken(excerptTokens, ExcerptTokenKind.Content, span.prefix);
}
state.lastAppendedTokenIsSeparator = false;
}
for (const child of span.children) {
if (span.node === state.startingNode) {
if (state.stopBeforeChildKind && child.kind === state.stopBeforeChildKind) {
// We reached a child whose kind is stopBeforeChildKind, so stop traversing
return false;
}
}
if (!_buildSpan(excerptTokens, child, state)) {
return false;
}
}
if (span.suffix) {
_appendToken(excerptTokens, ExcerptTokenKind.Content, span.suffix);
state.lastAppendedTokenIsSeparator = false;
}
if (span.separator) {
_appendToken(excerptTokens, ExcerptTokenKind.Content, span.separator);
state.lastAppendedTokenIsSeparator = true;
}
// Are we building a excerpt? If so, set its range
if (captureTokenRange) {
captureTokenRange.startIndex = excerptStartIndex;
// We will assign capturedTokenRange.startIndex to be the index after the last token
// that was appended so far. However, if the last appended token was a separator, omit
// it from the range.
let excerptEndIndex: number = excerptTokens.length;
if (state.lastAppendedTokenIsSeparator) {
excerptEndIndex--;
}
captureTokenRange.endIndex = excerptEndIndex;
}
return true;
}
function _appendToken(
excerptTokens: IExcerptToken[],
excerptTokenKind: ExcerptTokenKind,
text: string,
canonicalReference?: DeclarationReference
): void {
if (text.length === 0) {
return;
}
const excerptToken: IExcerptToken = { kind: excerptTokenKind, text: text };
if (canonicalReference !== undefined) {
excerptToken.canonicalReference = canonicalReference.toString();
}
excerptTokens.push(excerptToken);
}
function _isDeclarationName(name: ts.Identifier): boolean {
return _isDeclaration(name.parent) && name.parent.name === name;
}
function _isDeclaration(node: ts.Node): node is ts.NamedDeclaration {
switch (node.kind) {
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.EnumDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
case ts.SyntaxKind.ModuleDeclaration:
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.TypeAliasDeclaration:
case ts.SyntaxKind.TypeParameter:
case ts.SyntaxKind.EnumMember:
case ts.SyntaxKind.BindingElement:
return true;
default:
return false;
}
}