Skip to content

Commit 69e804e

Browse files
committed
Fix strict-null-checks
1 parent 17e35f0 commit 69e804e

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export default class DtsGenerator {
5151
* Walks up the tree from the given starting node. If each parent matches the expected kind
5252
* from parentKinds, then the matching node is returned. Otherwise, undefined is returned.
5353
*/
54-
private static _matchParent<T extends ts.Node>(node: ts.Node, parentKinds: ts.SyntaxKind[]): T {
55-
let current: ts.Node = node;
54+
private static _matchParent<T extends ts.Node>(node: ts.Node, parentKinds: ts.SyntaxKind[]): T | undefined {
55+
let current: ts.Node | undefined = node;
5656

5757
let i: number = 0;
5858
while (true) { // tslint:disable-line:no-constant-condition
@@ -86,11 +86,11 @@ export default class DtsGenerator {
8686

8787
// Is it an export declaration?
8888
if (currentAlias.declarations) {
89-
const exportDeclaration: ts.ExportDeclaration = DtsGenerator._matchParent<ts.ExportDeclaration>(
90-
currentAlias.declarations[0],
89+
const exportDeclaration: ts.ExportDeclaration | undefined
90+
= DtsGenerator._matchParent<ts.ExportDeclaration>(currentAlias.declarations[0],
9191
[ts.SyntaxKind.ExportSpecifier, ts.SyntaxKind.NamedExports, ts.SyntaxKind.ExportDeclaration]);
9292

93-
if (exportDeclaration) {
93+
if (exportDeclaration && exportDeclaration.moduleSpecifier) {
9494
// Example: " '@microsoft/sp-lodash-subset'" or " './MyClass'"
9595
const moduleSpecifier: string = exportDeclaration.moduleSpecifier.getFullText();
9696

@@ -148,20 +148,22 @@ export default class DtsGenerator {
148148
this._entries.sort((a, b) => a.getSortKey().localeCompare(b.getSortKey()));
149149

150150
for (const entry of this._entries) {
151-
for (const declaration of entry.followedSymbol.declarations) {
152-
// console.log(PrettyPrinter.dumpTree(declaration));
151+
if (entry.followedSymbol) {
152+
for (const declaration of entry.followedSymbol.declarations || []) {
153+
// console.log(PrettyPrinter.dumpTree(declaration));
153154

154-
// console.log(declaration.getText());
155-
// console.log('=====================================');
155+
// console.log(declaration.getText());
156+
// console.log('=====================================');
156157

157-
const span: Span = new Span(declaration);
158-
console.log(span.getDump());
159-
console.log('-------------------------------------');
158+
const span: Span = new Span(declaration);
159+
console.log(span.getDump());
160+
console.log('-------------------------------------');
160161

161-
this._modifySpan(span, entry);
162+
this._modifySpan(span, entry);
162163

163-
this._indentedWriter.writeLine();
164-
this._indentedWriter.writeLine(span.getModifiedText());
164+
this._indentedWriter.writeLine();
165+
this._indentedWriter.writeLine(span.getModifiedText());
166+
}
165167
}
166168
}
167169

@@ -211,7 +213,7 @@ export default class DtsGenerator {
211213
// Since we are emitting a separate declaration for each one, we need to look upwards
212214
// in the ts.Node tree and write a copy of the enclosing VariableDeclarationList
213215
// content (e.g. "var" from "var x=1, y=2").
214-
const list: ts.VariableDeclarationList = DtsGenerator._matchParent(span.node,
216+
const list: ts.VariableDeclarationList | undefined = DtsGenerator._matchParent(span.node,
215217
[ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList]);
216218
if (!list) {
217219
throw new Error('Unsupported variable declaration');
@@ -235,12 +237,12 @@ export default class DtsGenerator {
235237
case ts.SyntaxKind.TypeAliasDeclaration:
236238
case ts.SyntaxKind.ModuleDeclaration: // (namespaces are a type of module declaration)
237239
{
238-
const symbol: ts.Symbol = this._typeChecker.getSymbolAtLocation(span.node);
240+
const symbol: ts.Symbol | undefined = this._typeChecker.getSymbolAtLocation(span.node);
239241
if (!symbol) {
240242
throw new Error('Symbol not found');
241243
}
242244

243-
const referencedEntry: Entry = this._fetchEntryForSymbol(symbol);
245+
const referencedEntry: Entry | undefined = this._fetchEntryForSymbol(symbol);
244246
if (referencedEntry) {
245247
span.modification.prefix = '/**/' + referencedEntry.uniqueName;
246248
}
@@ -279,7 +281,7 @@ export default class DtsGenerator {
279281
return undefined;
280282
}
281283

282-
let entry: Entry = this._entriesBySymbol.get(followedSymbol);
284+
let entry: Entry | undefined = this._entriesBySymbol.get(followedSymbol);
283285
if (entry) {
284286
return entry;
285287
}
@@ -291,7 +293,7 @@ export default class DtsGenerator {
291293
this._entriesBySymbol.set(followedSymbol, entry);
292294
console.log('======> ' + entry.localName);
293295

294-
for (const declaration of followedSymbol.declarations) {
296+
for (const declaration of followedSymbol.declarations || []) {
295297
// console.log(PrettyPrinter.dumpTree(declaration));
296298
// console.log('-------------------------------------');
297299
// console.log(declaration.getText());
@@ -315,7 +317,7 @@ export default class DtsGenerator {
315317
case ts.SyntaxKind.ExpressionWithTypeArguments:
316318
case ts.SyntaxKind.TypeReference:
317319
{
318-
const symbol: ts.Symbol = this._typeChecker.getSymbolAtLocation(node);
320+
const symbol: ts.Symbol | undefined = this._typeChecker.getSymbolAtLocation(node);
319321
if (!symbol) {
320322
throw new Error('Symbol not found');
321323
}

0 commit comments

Comments
 (0)