From d46653a2ac1f744c0abc437dc2144226346050e6 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 19 Jan 2018 10:10:43 -0800 Subject: [PATCH] Handle `undefined` input to firstDefined (#21300) --- src/compiler/checker.ts | 2 +- src/compiler/core.ts | 4 ++++ src/services/codefixes/importFixes.ts | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 519a9f4cf40..859f0aa1d82 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6562,7 +6562,7 @@ namespace ts { // b) It references `arguments` somewhere const lastParam = lastOrUndefined(declaration.parameters); const lastParamTags = lastParam && getJSDocParameterTags(lastParam); - const lastParamVariadicType = lastParamTags && firstDefined(lastParamTags, p => + const lastParamVariadicType = firstDefined(lastParamTags, p => p.typeExpression && isJSDocVariadicType(p.typeExpression.type) ? p.typeExpression.type : undefined); if (!lastParamVariadicType && !containsArgumentsReference(declaration)) { return false; diff --git a/src/compiler/core.ts b/src/compiler/core.ts index afdc65de51d..cae9c841272 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -183,6 +183,10 @@ namespace ts { /** Like `forEach`, but suitable for use with numbers and strings (which may be falsy). */ export function firstDefined(array: ReadonlyArray | undefined, callback: (element: T, index: number) => U | undefined): U | undefined { + if (array === undefined) { + return undefined; + } + for (let i = 0; i < array.length; i++) { const result = callback(array[i], i); if (result !== undefined) { diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 7e0aefef139..4c536ce3fd2 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -467,7 +467,7 @@ namespace ts.codefix { addJsExtension: boolean, ): string | undefined { const roots = getEffectiveTypeRoots(options, host); - return roots && firstDefined(roots, unNormalizedTypeRoot => { + return firstDefined(roots, unNormalizedTypeRoot => { const typeRoot = toPath(unNormalizedTypeRoot, /*basePath*/ undefined, getCanonicalFileName); if (startsWith(moduleFileName, typeRoot)) { return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), options, addJsExtension);