Skip to content

Commit 1231038

Browse files
committed
Introduced Span.modification and Span.modify() to enable transformations involving multiple nodes in the tree
1 parent 8ad0e50 commit 1231038

1 file changed

Lines changed: 80 additions & 148 deletions

File tree

‎apps/api-extractor/src/generators/DtsGenerator.ts‎

Lines changed: 80 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,6 @@ interface IFollowAliasesResult {
4040
external: boolean;
4141
}
4242

43-
interface IWritingSpanContext {
44-
entry: Entry;
45-
}
46-
47-
interface IWritingSpanArgs {
48-
readonly span: Span;
49-
readonly previousSpan: Span | undefined;
50-
readonly parentSpan: Span | undefined;
51-
readonly kind: ts.SyntaxKind;
52-
readonly parentKind: ts.SyntaxKind;
53-
54-
prefix: string;
55-
suffix: string;
56-
57-
skipSeparatorBefore: boolean;
58-
skipChildrenAndSuffix: boolean;
59-
skipSeparatorAfter: boolean;
60-
61-
context: IWritingSpanContext;
62-
}
63-
6443
export default class DtsGenerator {
6544
private _extractor: Extractor;
6645
private _typeChecker: ts.TypeChecker;
@@ -180,13 +159,10 @@ export default class DtsGenerator {
180159
span.dump();
181160
console.log('-------------------------------------');
182161

183-
const context: IWritingSpanContext = {
184-
entry: entry
185-
};
162+
this._modifySpan(span, entry);
186163

187164
this._indentedWriter.writeLine();
188-
this._writeSpanTree(span, context);
189-
this._indentedWriter.writeLine();
165+
this._indentedWriter.writeLine(span.getModifiedText());
190166
}
191167
}
192168

@@ -195,132 +171,88 @@ export default class DtsGenerator {
195171
return fileContent;
196172
}
197173

198-
private _writeSpanTree(span: Span, context: IWritingSpanContext): void {
199-
this._writeSpanTreeHelper([span], undefined, context);
200-
}
201-
202-
private _writeSpanTreeHelper(children: Span[], parentSpan: Span|undefined, context: IWritingSpanContext): void {
203-
let previousArgs: IWritingSpanArgs | undefined = undefined;
204-
for (const child of children) {
205-
const args: IWritingSpanArgs = {
206-
span: child,
207-
previousSpan: previousArgs ? previousArgs.span : undefined,
208-
parentSpan: parentSpan,
209-
kind: child.node.kind,
210-
parentKind: child.node.parent ? child.node.parent.kind : ts.SyntaxKind.Unknown,
211-
212-
prefix: child.prefix,
213-
suffix: child.suffix,
214-
215-
skipSeparatorBefore: previousArgs ? previousArgs.skipSeparatorAfter : false,
216-
skipChildrenAndSuffix: false,
217-
skipSeparatorAfter: false,
218-
219-
context: context
220-
};
221-
222-
this._onWritingSpan(args);
223-
224-
this._writeSpanTreeHelper2(previousArgs, args);
225-
226-
previousArgs = args;
227-
}
228-
this._writeSpanTreeHelper2(previousArgs, undefined);
229-
}
230-
231-
private _writeSpanTreeHelper2(previousArgs: IWritingSpanArgs, args: IWritingSpanArgs|undefined): void {
232-
if (previousArgs) {
233-
this._indentedWriter.write(previousArgs.prefix);
234-
235-
if (!previousArgs.skipChildrenAndSuffix) {
236-
this._writeSpanTreeHelper(previousArgs.span.children, previousArgs.span, previousArgs.context);
237-
this._indentedWriter.write(previousArgs.suffix);
238-
}
239-
240-
if (!args || !args.skipSeparatorBefore) {
241-
this._indentedWriter.write(previousArgs.span.separator);
242-
}
243-
}
244-
}
245-
246-
private _onWritingSpan(args: IWritingSpanArgs): void {
247-
switch (args.kind) {
248-
case ts.SyntaxKind.Block:
249-
// Replace code blocks with a semicolon
250-
args.prefix = ';';
251-
args.skipSeparatorBefore = true;
252-
args.skipChildrenAndSuffix = true;
253-
break;
254-
255-
case ts.SyntaxKind.ExportKeyword:
256-
case ts.SyntaxKind.DefaultKeyword:
257-
case ts.SyntaxKind.DeclareKeyword:
258-
// Delete any explicit "export" keywords -- we will re-add them based on Entry.exported
259-
args.prefix = '';
260-
args.skipChildrenAndSuffix = true;
261-
args.skipSeparatorAfter = true;
262-
break;
263-
264-
case ts.SyntaxKind.InterfaceKeyword:
265-
case ts.SyntaxKind.ClassKeyword:
266-
case ts.SyntaxKind.EnumKeyword:
267-
case ts.SyntaxKind.NamespaceKeyword:
268-
case ts.SyntaxKind.ModuleKeyword:
269-
case ts.SyntaxKind.TypeKeyword:
270-
args.prefix = 'declare ' + args.prefix;
271-
if (args.context.entry.exported) {
272-
args.prefix = 'export ' + args.prefix;
273-
}
274-
break;
275-
276-
case ts.SyntaxKind.VariableDeclaration:
277-
if (!args.parentSpan) {
278-
// The VariableDeclaration node is part of a VariableDeclarationList, however
279-
// the Entry.followedSymbol points to the VariableDeclaration part because
280-
// multiple definitions might share the same VariableDeclarationList.
281-
//
282-
// Since we are emitting a separate declaration for each one, we need to look upwards
283-
// in the ts.Node tree and write a copy of the enclosing VariableDeclarationList
284-
// content (e.g. "var" from "var x=1, y=2").
285-
const list: ts.VariableDeclarationList = DtsGenerator._matchParent(args.span.node,
286-
[ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList]);
287-
if (!list) {
288-
throw new Error('Unsupported variable declaration');
174+
private _modifySpan(rootSpan: Span, entry: Entry): void {
175+
rootSpan.modify((span: Span, previousSpan: Span | undefined, parentSpan: Span | undefined) => {
176+
switch (span.kind) {
177+
case ts.SyntaxKind.Block:
178+
// Replace code blocks with a semicolon
179+
span.modification.prefix = ';';
180+
span.modification.skipChildren = true;
181+
span.modification.suffix = span.getLastInnerSeparator();
182+
if (previousSpan) {
183+
previousSpan.modification.skipSeparatorAfter = true;
289184
}
290-
const listPrefix: string = list.getSourceFile().text
291-
.substring(list.getStart(), list.declarations[0].getStart());
292-
args.prefix = 'declare ' + listPrefix + args.prefix;
293-
args.suffix = ';';
294-
}
295-
break;
296-
297-
case ts.SyntaxKind.Identifier:
298-
if (args.parentKind) {
299-
switch (args.parentKind) {
300-
case ts.SyntaxKind.ExpressionWithTypeArguments:
301-
case ts.SyntaxKind.TypeReference:
302-
303-
case ts.SyntaxKind.ClassDeclaration:
304-
case ts.SyntaxKind.InterfaceDeclaration:
305-
case ts.SyntaxKind.EnumDeclaration:
306-
case ts.SyntaxKind.TypeAliasDeclaration:
307-
case ts.SyntaxKind.ModuleDeclaration: // (namespaces are a type of module declaration)
308-
{
309-
const symbol: ts.Symbol = this._typeChecker.getSymbolAtLocation(args.span.node);
310-
if (!symbol) {
311-
throw new Error('Symbol not found');
312-
}
313-
314-
const entry: Entry = this._fetchEntryForSymbol(symbol);
315-
if (entry) {
316-
args.prefix = '/**/' + entry.uniqueName;
185+
break;
186+
187+
case ts.SyntaxKind.ExportKeyword:
188+
case ts.SyntaxKind.DefaultKeyword:
189+
case ts.SyntaxKind.DeclareKeyword:
190+
// Delete any explicit "export" keywords -- we will re-add them based on Entry.exported
191+
span.modification.skipAll();
192+
break;
193+
194+
case ts.SyntaxKind.InterfaceKeyword:
195+
case ts.SyntaxKind.ClassKeyword:
196+
case ts.SyntaxKind.EnumKeyword:
197+
case ts.SyntaxKind.NamespaceKeyword:
198+
case ts.SyntaxKind.ModuleKeyword:
199+
case ts.SyntaxKind.TypeKeyword:
200+
span.modification.prefix = 'declare ' + span.modification.prefix;
201+
if (entry.exported) {
202+
span.modification.prefix = 'export ' + span.modification.prefix;
203+
}
204+
break;
205+
206+
case ts.SyntaxKind.VariableDeclaration:
207+
if (!parentSpan) {
208+
// The VariableDeclaration node is part of a VariableDeclarationList, however
209+
// the Entry.followedSymbol points to the VariableDeclaration part because
210+
// multiple definitions might share the same VariableDeclarationList.
211+
//
212+
// Since we are emitting a separate declaration for each one, we need to look upwards
213+
// in the ts.Node tree and write a copy of the enclosing VariableDeclarationList
214+
// content (e.g. "var" from "var x=1, y=2").
215+
const list: ts.VariableDeclarationList = DtsGenerator._matchParent(span.node,
216+
[ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList]);
217+
if (!list) {
218+
throw new Error('Unsupported variable declaration');
219+
}
220+
const listPrefix: string = list.getSourceFile().text
221+
.substring(list.getStart(), list.declarations[0].getStart());
222+
span.modification.prefix = 'declare ' + listPrefix + span.modification.prefix;
223+
span.modification.suffix = ';';
224+
}
225+
break;
226+
227+
case ts.SyntaxKind.Identifier:
228+
if (parentSpan) {
229+
switch (parentSpan.kind) {
230+
case ts.SyntaxKind.ExpressionWithTypeArguments:
231+
case ts.SyntaxKind.TypeReference:
232+
233+
case ts.SyntaxKind.ClassDeclaration:
234+
case ts.SyntaxKind.InterfaceDeclaration:
235+
case ts.SyntaxKind.EnumDeclaration:
236+
case ts.SyntaxKind.TypeAliasDeclaration:
237+
case ts.SyntaxKind.ModuleDeclaration: // (namespaces are a type of module declaration)
238+
{
239+
const symbol: ts.Symbol = this._typeChecker.getSymbolAtLocation(span.node);
240+
if (!symbol) {
241+
throw new Error('Symbol not found');
242+
}
243+
244+
const referencedEntry: Entry = this._fetchEntryForSymbol(symbol);
245+
if (referencedEntry) {
246+
span.modification.prefix = '/**/' + referencedEntry.uniqueName;
247+
}
317248
}
318-
}
319-
break;
249+
break;
250+
}
320251
}
252+
break;
321253
}
322-
break;
323-
}
254+
}
255+
);
324256
}
325257

326258
private _makeUniqueNames(): void {

0 commit comments

Comments
 (0)