From 7c53a1deb28775dc0a57509d653c93dbfe41fd76 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 18 Oct 2016 14:22:43 -0700 Subject: [PATCH] Instead of getResolutionOrDiagnostic, use getResolutionDiagnostic and avoid using resolution.resolvedFileName if the diagnostic is defined. --- src/compiler/checker.ts | 10 +++++----- src/compiler/program.ts | 26 ++++++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ebf86484c45..aec9cbec41d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1366,8 +1366,8 @@ namespace ts { } const resolvedModule = getResolvedModule(getSourceFileOfNode(location), moduleReference); - const resolvedOrDiagnostic = resolvedModule && getResolutionOrDiagnostic(compilerOptions, resolvedModule); - const sourceFile = typeof resolvedOrDiagnostic === "string" && resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName); + const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule); + const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName); if (sourceFile) { if (sourceFile.symbol) { // merged symbol is module declaration symbol combined with all augmentations @@ -1389,8 +1389,8 @@ namespace ts { if (moduleNotFoundError) { // report errors only if it was requested - if (resolvedOrDiagnostic && typeof resolvedOrDiagnostic !== "string") { - error(errorNode, resolvedOrDiagnostic.diag, moduleName, resolvedOrDiagnostic.file); + if (resolutionDiagnostic) { + error(errorNode, resolutionDiagnostic, moduleName, resolvedModule.resolvedFileName); } else { const tsExtension = tryExtractTypeScriptExtension(moduleName); @@ -8543,7 +8543,7 @@ namespace ts { // An evolving array type tracks the element types that have so far been seen in an // 'x.push(value)' or 'x[n] = value' operation along the control flow graph. Evolving // array types are ultimately converted into manifest array types (using getFinalArrayType) - // and never escape the getFlowTypeOfReference function. + // and never escape the getFlowTypeOfReference function. function createEvolvingArrayType(elementType: Type): AnonymousType { const result = createObjectType(TypeFlags.Anonymous); result.elementType = elementType; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 43bc877bc58..71ad88fea5a 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1318,10 +1318,8 @@ namespace ts { } const isFromNodeModulesSearch = resolution.isExternalLibraryImport; - const isJsFileFromNodeModules = !resolution.resolvedTsFileName; - const resolvedOrDiagnostic = getResolutionOrDiagnostic(options, resolution); - // Ignore it if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs') -- this will be reported by the checker, so just return undefined for now. - const resolvedFileName = typeof resolvedOrDiagnostic === "string" ? resolvedOrDiagnostic : undefined; + const isJsFileFromNodeModules = isFromNodeModulesSearch && !resolution.resolvedTsFileName; + const resolvedFileName = resolution.resolvedFileName; if (isFromNodeModulesSearch) { currentNodeModulesDepth++; @@ -1333,7 +1331,8 @@ namespace ts { // - module name comes from the list of imports // - it's not a top level JavaScript module that exceeded the search max const elideImport = isJsFileFromNodeModules && currentNodeModulesDepth > maxNodeModuleJsDepth; - const shouldAddFile = resolvedFileName && !options.noResolve && i < file.imports.length && !elideImport; + // Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs') + const shouldAddFile = resolvedFileName && !getResolutionDiagnostic(options, resolution) && !options.noResolve && i < file.imports.length && !elideImport; if (elideImport) { modulesWithElidedImports[file.path] = true; @@ -1580,24 +1579,23 @@ namespace ts { /* @internal */ /** - * Extracts the file name from a ResolvedModule, or returns a DiagnosticMessage if we are not set up to use that kind of file. + * Returns a DiagnosticMessage if we can't use a resolved module due to its extension. * The DiagnosticMessage's parameters are the imported module name, and the filename it resolved to. */ - export function getResolutionOrDiagnostic(options: CompilerOptions, { resolvedTsFileName: ts, resolvedJsFileName: js }: ResolvedModule): string | { file: string, diag: DiagnosticMessage } { + export function getResolutionDiagnostic(options: CompilerOptions, { resolvedTsFileName: ts, resolvedJsFileName: js }: ResolvedModule): DiagnosticMessage | undefined { if (ts) { - return !options.jsx && fileExtensionIs(ts, ".tsx") - ? { file: ts, diag: Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set } - : ts; + return !options.jsx && fileExtensionIs(ts, ".tsx") ? Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set : undefined; } else { if (!options.allowJs) { - return { file: js!, diag: Diagnostics.Module_0_was_resolved_to_1_but_allowJs_is_not_set }; + return Diagnostics.Module_0_was_resolved_to_1_but_allowJs_is_not_set; } else if (!options.jsx && fileExtensionIs(js!, ".jsx")) { - return { file: js!, diag: Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set }; + return Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set; + } + else { + return undefined; } - else - return js!; } } }