Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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) ====

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== /a.ts ===
import jsx from "./jsx";
>jsx : Symbol(jsx, Decl(a.ts, 0, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== /a.ts ===
import jsx from "./jsx";
>jsx : any

12 changes: 12 additions & 0 deletions tests/baselines/reference/moduleResolution_relativeImportJsFile.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== /src/a.ts ===
import * as b from "./b";
>b : Symbol(b, Decl(a.ts, 0, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== /src/a.ts ===
import * as b from "./b";
>b : any

Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 7 additions & 0 deletions tests/cases/compiler/moduleResolution_relativeImportJsFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @noImplicitReferences: true

// @Filename: /src/b.js
export const x = 0;

// @Filename: /src/a.ts
import * as b from "./b";
Original file line number Diff line number Diff line change
@@ -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";

This file was deleted.