Allow relative imports of '.js' files when --noImplicitAny is disabled (#18489)

* Allow relative imports of '.js' files when `--noImplicitAny` is disabled

* Update baselines, and don't ignore a diagnostic about missing JSX
This commit is contained in:
Andy 2017-09-19 12:42:29 -07:00 committed by GitHub
parent 406d9abb5a
commit 0ae42ea3de
19 changed files with 89 additions and 45 deletions

View file

@ -1759,13 +1759,13 @@ namespace ts {
}
// May be an untyped module. If so, ignore resolutionDiagnostic.
if (resolvedModule && resolvedModule.isExternalLibraryImport && !extensionIsTypeScript(resolvedModule.extension)) {
if (resolvedModule && !extensionIsTypeScript(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) {
if (isForAugmentation) {
const diag = Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
}
else if (noImplicitAny && moduleNotFoundError) {
let errorInfo = chainDiagnosticMessages(/*details*/ undefined,
let errorInfo = !resolvedModule.isExternalLibraryImport ? undefined : chainDiagnosticMessages(/*details*/ undefined,
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
moduleReference);
errorInfo = chainDiagnosticMessages(errorInfo,

View file

@ -3146,10 +3146,6 @@
"category": "Error",
"code": 6142
},
"Module '{0}' was resolved to '{1}', but '--allowJs' is not set.": {
"category": "Error",
"code": 6143
},
"Module '{0}' was resolved as locally declared ambient module in file '{1}'.": {
"category": "Message",
"code": 6144

View file

@ -1846,7 +1846,8 @@ namespace ts {
}
const isFromNodeModulesSearch = resolution.isExternalLibraryImport;
const isJsFileFromNodeModules = isFromNodeModulesSearch && !extensionIsTypeScript(resolution.extension);
const isJsFile = !extensionIsTypeScript(resolution.extension);
const isJsFileFromNodeModules = isFromNodeModulesSearch && isJsFile;
const resolvedFileName = resolution.resolvedFileName;
if (isFromNodeModulesSearch) {
@ -1861,7 +1862,12 @@ namespace ts {
const elideImport = isJsFileFromNodeModules && currentNodeModulesDepth > maxNodeModuleJsDepth;
// Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs')
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
const shouldAddFile = resolvedFileName && !getResolutionDiagnostic(options, resolution) && !options.noResolve && i < file.imports.length && !elideImport;
const shouldAddFile = resolvedFileName
&& !getResolutionDiagnostic(options, resolution)
&& !options.noResolve
&& i < file.imports.length
&& !elideImport
&& !(isJsFile && !options.allowJs);
if (elideImport) {
modulesWithElidedImports.set(file.path, true);
@ -2236,7 +2242,7 @@ namespace ts {
return options.jsx ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set;
}
function needAllowJs() {
return options.allowJs ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_allowJs_is_not_set;
return options.allowJs || !options.noImplicitAny ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
}
}

View file

@ -1,18 +1,15 @@
/a.ts(1,17): error TS6142: Module './tsx' was resolved to '/tsx.tsx', but '--jsx' is not set.
/a.ts(2,17): error TS6142: Module './jsx' was resolved to '/jsx.jsx', but '--jsx' is not set.
/a.ts(3,16): error TS6143: Module './js' was resolved to '/js.js', but '--allowJs' is not set.
==== /a.ts (3 errors) ====
import tsx from "./tsx";
==== /a.ts (2 errors) ====
import tsx from "./tsx"; // Not allowed.
~~~~~~~
!!! error TS6142: Module './tsx' was resolved to '/tsx.tsx', but '--jsx' is not set.
import jsx from "./jsx";
import jsx from "./jsx"; // Not allowed.
~~~~~~~
!!! error TS6142: Module './jsx' was resolved to '/jsx.jsx', but '--jsx' is not set.
import js from "./js";
~~~~~~
!!! error TS6143: Module './js' was resolved to '/js.js', but '--allowJs' is not set.
import js from "./js"; // OK because it's an untyped module.
==== /tsx.tsx (0 errors) ====

View file

@ -7,9 +7,9 @@
//// [js.js]
//// [a.ts]
import tsx from "./tsx";
import jsx from "./jsx";
import js from "./js";
import tsx from "./tsx"; // Not allowed.
import jsx from "./jsx"; // Not allowed.
import js from "./js"; // OK because it's an untyped module.
//// [a.js]

View file

@ -1,12 +0,0 @@
/a.ts(1,17): error TS6143: Module './jsx' was resolved to '/jsx.jsx', but '--allowJs' is not set.
==== /a.ts (1 errors) ====
import jsx from "./jsx";
~~~~~~~
!!! error TS6143: Module './jsx' was resolved to '/jsx.jsx', but '--allowJs' is not set.
==== /jsx.jsx (0 errors) ====
// Test the error message if we have `--jsx` but not `--allowJw`.

View file

@ -1,7 +1,7 @@
//// [tests/cases/compiler/moduleResolutionWithExtensions_notSupported3.ts] ////
//// [jsx.jsx]
// Test the error message if we have `--jsx` but not `--allowJw`.
// If we have "--jsx" set and not "--allowJs", it's an implicit-any module.
//// [a.ts]

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import jsx from "./jsx";
>jsx : Symbol(jsx, Decl(a.ts, 0, 6))

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import jsx from "./jsx";
>jsx : any

View file

@ -0,0 +1,12 @@
//// [tests/cases/compiler/moduleResolution_relativeImportJsFile.ts] ////
//// [b.js]
export const x = 0;
//// [a.ts]
import * as b from "./b";
//// [a.js]
"use strict";
exports.__esModule = true;

View file

@ -0,0 +1,4 @@
=== /src/a.ts ===
import * as b from "./b";
>b : Symbol(b, Decl(a.ts, 0, 6))

View file

@ -0,0 +1,4 @@
=== /src/a.ts ===
import * as b from "./b";
>b : any

View file

@ -0,0 +1,11 @@
/src/a.ts(1,20): error TS7016: Could not find a declaration file for module './b'. '/src/b.js' implicitly has an 'any' type.
==== /src/a.ts (1 errors) ====
import * as b from "./b";
~~~~~
!!! error TS7016: Could not find a declaration file for module './b'. '/src/b.js' implicitly has an 'any' type.
==== /src/b.js (0 errors) ====
export const x = 0;

View file

@ -0,0 +1,12 @@
//// [tests/cases/compiler/moduleResolution_relativeImportJsFile_noImplicitAny.ts] ////
//// [b.js]
export const x = 0;
//// [a.ts]
import * as b from "./b";
//// [a.js]
"use strict";
exports.__esModule = true;

View file

@ -8,6 +8,6 @@
// @Filename: /js.js
// @Filename: /a.ts
import tsx from "./tsx";
import jsx from "./jsx";
import js from "./js";
import tsx from "./tsx"; // Not allowed.
import jsx from "./jsx"; // Not allowed.
import js from "./js"; // OK because it's an untyped module.

View file

@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @jsx: preserve
// @traceResolution: true
// Test the error message if we have `--jsx` but not `--allowJw`.
// If we have "--jsx" set and not "--allowJs", it's an implicit-any module.
// @Filename: /jsx.jsx

View file

@ -0,0 +1,7 @@
// @noImplicitReferences: true
// @Filename: /src/b.js
export const x = 0;
// @Filename: /src/a.ts
import * as b from "./b";

View file

@ -0,0 +1,8 @@
// @noImplicitReferences: true
// @noImplicitAny: true
// @Filename: /src/b.js
export const x = 0;
// @Filename: /src/a.ts
import * as b from "./b";

View file

@ -1,9 +0,0 @@
// @noImplicitReferences: true
// @currentDirectory: /
// This tests that untyped module imports don't happen with local imports.
// @filename: /foo.js
This file is not processed.
// @filename: /a.ts
import * as foo from "./foo";