Skip to content

Commit f02de8f

Browse files
committed
Various PR feedback
1 parent bf6a97f commit f02de8f

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

‎apps/api-extractor/src/extractor/Extractor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ export class Extractor {
285285

286286
const dtsFilename: string = path.resolve(this._absoluteRootFolder,
287287
this._actualConfig.packageTypings.outputFolder, this._actualConfig.packageTypings.internalFilename);
288-
this._monitoredLogger.logVerbose(`Writing package typings: ${dtsFilename}`);
288+
289+
this._monitoredLogger.logVerbose(`Writing package typings: ${dtsFilename}`);
289290

290291
fsx.mkdirsSync(path.dirname(dtsFilename));
291292

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class Entry {
4343
public getSortKey(): string {
4444
if (!this._sortKey) {
4545
if (this.localName.substr(0, 1) === '_') {
46-
// Removes the leading underscore, for example: "_example" --> "example*"
46+
// Removes the leading underscore, for example: "_example" --> "example*"
4747
// This causes internal definitions to sort alphabetically with regular definitions.
4848
// The star is appended to preserve uniqueness, since "*" is not a legal identifier character.
49-
this._sortKey = this.localName.substr(1) + ' ';
49+
this._sortKey = this.localName.substr(1) + '*';
5050
} else {
5151
this._sortKey = this.localName;
5252
}
@@ -96,26 +96,40 @@ export default class PackageTypingsGenerator {
9696
private readonly _entries: Entry[] = [];
9797

9898
/**
99-
* Walks up the tree from the given starting node. If each parent matches the expected kind
100-
* from parentKinds, then the matching node is returned. Otherwise, undefined is returned.
99+
* Returns an ancestor of "node", such that the ancestor, any intermediary nodes,
100+
* and the starting node match a list of expected kinds. Undefined is returned
101+
* if there aren't enough ancestors, or if the kinds are incorrect.
102+
*
103+
* For example, suppose child "C" has parents A --> B --> C.
104+
*
105+
* Calling _matchAncestor(C, [ExportSpecifier, NamedExports, ExportDeclaration])
106+
* would return A only if A is of kind ExportSpecifier, B is of kind NamedExports,
107+
* and C is of kind ExportDeclaration.
108+
*
109+
* Calling _matchAncestor(C, [ExportDeclaration]) would return C.
101110
*/
102-
private static _matchParent<T extends ts.Node>(node: ts.Node, parentKinds: ts.SyntaxKind[]): T | undefined {
103-
let current: ts.Node | undefined = node;
111+
private static _matchAncestor<T extends ts.Node>(node: ts.Node, kindsToMatch: ts.SyntaxKind[]): T | undefined {
112+
// (slice(0) clones an array)
113+
const reversedParentKinds: ts.SyntaxKind[] = kindsToMatch.slice(0).reverse();
104114

105-
let i: number = 0;
106-
while (true) { // tslint:disable-line:no-constant-condition
107-
if (!current || current.kind !== parentKinds[i]) {
108-
return undefined;
109-
}
115+
let current: ts.Node | undefined = undefined;
110116

111-
if (i >= parentKinds.length - 1) {
112-
break;
117+
for (const parentKind of reversedParentKinds) {
118+
if (!current) {
119+
// The first time through, start with node
120+
current = node;
121+
} else {
122+
// Then walk the parents
123+
current = current.parent;
113124
}
114125

115-
++i;
116-
current = current.parent;
126+
// If we ran out of items, or if the kind doesn't match, then fail
127+
if (!current || current.kind !== parentKind) {
128+
return undefined;
129+
}
117130
}
118131

132+
// If we matched everything, then return the node that matched the last parentKinds item
119133
return current as T;
120134
}
121135

@@ -139,8 +153,8 @@ export default class PackageTypingsGenerator {
139153
// Is it an export declaration?
140154
if (currentAlias.declarations) {
141155
const exportDeclaration: ts.ExportDeclaration | undefined
142-
= PackageTypingsGenerator._matchParent<ts.ExportDeclaration>(currentAlias.declarations[0],
143-
[ts.SyntaxKind.ExportSpecifier, ts.SyntaxKind.NamedExports, ts.SyntaxKind.ExportDeclaration]);
156+
= PackageTypingsGenerator._matchAncestor<ts.ExportDeclaration>(currentAlias.declarations[0],
157+
[ts.SyntaxKind.ExportDeclaration, ts.SyntaxKind.NamedExports, ts.SyntaxKind.ExportSpecifier]);
144158

145159
if (exportDeclaration && exportDeclaration.moduleSpecifier) {
146160
// Example: " '@microsoft/sp-lodash-subset'" or " './MyClass'"
@@ -213,14 +227,8 @@ export default class PackageTypingsGenerator {
213227
for (const entry of this._entries) {
214228
if (entry.followedSymbol) {
215229
for (const declaration of entry.followedSymbol.declarations || []) {
216-
// console.log(PrettyPrinter.dumpTree(declaration));
217-
218-
// console.log(declaration.getText());
219-
// console.log('=====================================');
220230

221231
const span: Span = new Span(declaration);
222-
// console.log(span.getDump());
223-
// console.log('-------------------------------------');
224232

225233
this._modifySpan(span, entry);
226234

@@ -269,8 +277,8 @@ export default class PackageTypingsGenerator {
269277
// Since we are emitting a separate declaration for each one, we need to look upwards
270278
// in the ts.Node tree and write a copy of the enclosing VariableDeclarationList
271279
// content (e.g. "var" from "var x=1, y=2").
272-
const list: ts.VariableDeclarationList | undefined = PackageTypingsGenerator._matchParent(span.node,
273-
[ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList]);
280+
const list: ts.VariableDeclarationList | undefined = PackageTypingsGenerator._matchAncestor(span.node,
281+
[ts.SyntaxKind.VariableDeclarationList, ts.SyntaxKind.VariableDeclaration]);
274282
if (!list) {
275283
throw new Error('Unsupported variable declaration');
276284
}

0 commit comments

Comments
 (0)