Skip to content

Commit 0ae42ea

Browse files
author
Andy
authored
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
1 parent 406d9ab commit 0ae42ea

19 files changed

+89
-45
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,13 +1759,13 @@ namespace ts {
17591759
}
17601760

17611761
// May be an untyped module. If so, ignore resolutionDiagnostic.
1762-
if (resolvedModule && resolvedModule.isExternalLibraryImport && !extensionIsTypeScript(resolvedModule.extension)) {
1762+
if (resolvedModule && !extensionIsTypeScript(resolvedModule.extension) && resolutionDiagnostic === undefined || resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type) {
17631763
if (isForAugmentation) {
17641764
const diag = Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
17651765
error(errorNode, diag, moduleReference, resolvedModule.resolvedFileName);
17661766
}
17671767
else if (noImplicitAny && moduleNotFoundError) {
1768-
let errorInfo = chainDiagnosticMessages(/*details*/ undefined,
1768+
let errorInfo = !resolvedModule.isExternalLibraryImport ? undefined : chainDiagnosticMessages(/*details*/ undefined,
17691769
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
17701770
moduleReference);
17711771
errorInfo = chainDiagnosticMessages(errorInfo,

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,10 +3146,6 @@
31463146
"category": "Error",
31473147
"code": 6142
31483148
},
3149-
"Module '{0}' was resolved to '{1}', but '--allowJs' is not set.": {
3150-
"category": "Error",
3151-
"code": 6143
3152-
},
31533149
"Module '{0}' was resolved as locally declared ambient module in file '{1}'.": {
31543150
"category": "Message",
31553151
"code": 6144

src/compiler/program.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,8 @@ namespace ts {
18461846
}
18471847

18481848
const isFromNodeModulesSearch = resolution.isExternalLibraryImport;
1849-
const isJsFileFromNodeModules = isFromNodeModulesSearch && !extensionIsTypeScript(resolution.extension);
1849+
const isJsFile = !extensionIsTypeScript(resolution.extension);
1850+
const isJsFileFromNodeModules = isFromNodeModulesSearch && isJsFile;
18501851
const resolvedFileName = resolution.resolvedFileName;
18511852

18521853
if (isFromNodeModulesSearch) {
@@ -1861,7 +1862,12 @@ namespace ts {
18611862
const elideImport = isJsFileFromNodeModules && currentNodeModulesDepth > maxNodeModuleJsDepth;
18621863
// Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs')
18631864
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
1864-
const shouldAddFile = resolvedFileName && !getResolutionDiagnostic(options, resolution) && !options.noResolve && i < file.imports.length && !elideImport;
1865+
const shouldAddFile = resolvedFileName
1866+
&& !getResolutionDiagnostic(options, resolution)
1867+
&& !options.noResolve
1868+
&& i < file.imports.length
1869+
&& !elideImport
1870+
&& !(isJsFile && !options.allowJs);
18651871

18661872
if (elideImport) {
18671873
modulesWithElidedImports.set(file.path, true);
@@ -2236,7 +2242,7 @@ namespace ts {
22362242
return options.jsx ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set;
22372243
}
22382244
function needAllowJs() {
2239-
return options.allowJs ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_allowJs_is_not_set;
2245+
return options.allowJs || !options.noImplicitAny ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
22402246
}
22412247
}
22422248

tests/baselines/reference/moduleResolutionWithExtensions_notSupported.errors.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
/a.ts(1,17): error TS6142: Module './tsx' was resolved to '/tsx.tsx', but '--jsx' is not set.
22
/a.ts(2,17): error TS6142: Module './jsx' was resolved to '/jsx.jsx', but '--jsx' is not set.
3-
/a.ts(3,16): error TS6143: Module './js' was resolved to '/js.js', but '--allowJs' is not set.
43

54

6-
==== /a.ts (3 errors) ====
7-
import tsx from "./tsx";
5+
==== /a.ts (2 errors) ====
6+
import tsx from "./tsx"; // Not allowed.
87
~~~~~~~
98
!!! error TS6142: Module './tsx' was resolved to '/tsx.tsx', but '--jsx' is not set.
10-
import jsx from "./jsx";
9+
import jsx from "./jsx"; // Not allowed.
1110
~~~~~~~
1211
!!! error TS6142: Module './jsx' was resolved to '/jsx.jsx', but '--jsx' is not set.
13-
import js from "./js";
14-
~~~~~~
15-
!!! error TS6143: Module './js' was resolved to '/js.js', but '--allowJs' is not set.
12+
import js from "./js"; // OK because it's an untyped module.
1613

1714
==== /tsx.tsx (0 errors) ====
1815

tests/baselines/reference/moduleResolutionWithExtensions_notSupported.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//// [js.js]
88

99
//// [a.ts]
10-
import tsx from "./tsx";
11-
import jsx from "./jsx";
12-
import js from "./js";
10+
import tsx from "./tsx"; // Not allowed.
11+
import jsx from "./jsx"; // Not allowed.
12+
import js from "./js"; // OK because it's an untyped module.
1313

1414

1515
//// [a.js]

tests/baselines/reference/moduleResolutionWithExtensions_notSupported3.errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/baselines/reference/moduleResolutionWithExtensions_notSupported3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//// [tests/cases/compiler/moduleResolutionWithExtensions_notSupported3.ts] ////
22

33
//// [jsx.jsx]
4-
// Test the error message if we have `--jsx` but not `--allowJw`.
4+
// If we have "--jsx" set and not "--allowJs", it's an implicit-any module.
55

66

77
//// [a.ts]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import jsx from "./jsx";
3+
>jsx : Symbol(jsx, Decl(a.ts, 0, 6))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import jsx from "./jsx";
3+
>jsx : any
4+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/moduleResolution_relativeImportJsFile.ts] ////
2+
3+
//// [b.js]
4+
export const x = 0;
5+
6+
//// [a.ts]
7+
import * as b from "./b";
8+
9+
10+
//// [a.js]
11+
"use strict";
12+
exports.__esModule = true;

0 commit comments

Comments
 (0)