From 95ec5f8ca991bf8707f2d1ae8e04baab259f1e55 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 Jan 2018 09:39:27 -0800 Subject: [PATCH] Handle `undefined` input to firstDefined --- 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 519a9f4cf40d5..859f0aa1d8224 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 afdc65de51dee..cae9c84127229 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 7e0aefef13961..4c536ce3fd271 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);