Skip to content

Commit af71d55

Browse files
committed
Do not include json file unless --resolveJsonModule is specified
Fixes microsoft#26402
1 parent aa8f1e5 commit af71d55

6 files changed

+34
-15
lines changed

src/compiler/diagnosticMessages.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -3941,6 +3941,10 @@
39413941
"category": "Error",
39423942
"code": 7041
39433943
},
3944+
"Module '{0}' was resolved to '{1}', but '--resolveJsonModule' is not used.": {
3945+
"category": "Error",
3946+
"code": 7042
3947+
},
39443948
"You cannot rename this element.": {
39453949
"category": "Error",
39463950
"code": 8000
@@ -4576,4 +4580,4 @@
45764580
"category": "Message",
45774581
"code": 95066
45784582
}
4579-
}
4583+
}

src/compiler/program.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,6 @@ namespace ts {
28492849
switch (extension) {
28502850
case Extension.Ts:
28512851
case Extension.Dts:
2852-
case Extension.Json: // Since module is resolved to json file only when --resolveJsonModule, we dont need further check
28532852
// These are always allowed.
28542853
return undefined;
28552854
case Extension.Tsx:
@@ -2858,6 +2857,8 @@ namespace ts {
28582857
return needJsx() || needAllowJs();
28592858
case Extension.Js:
28602859
return needAllowJs();
2860+
case Extension.Json:
2861+
return needResolveJsonModule();
28612862
}
28622863

28632864
function needJsx() {
@@ -2866,6 +2867,9 @@ namespace ts {
28662867
function needAllowJs() {
28672868
return options.allowJs || !getStrictOptionValue(options, "noImplicitAny") ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
28682869
}
2870+
function needResolveJsonModule() {
2871+
return options.resolveJsonModule ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_resolveJsonModule_is_not_used;
2872+
}
28692873
}
28702874

28712875
function getModuleNames({ imports, moduleAugmentations }: SourceFile): string[] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/a.ts(1,20): error TS7042: Module 'foo/bar/foobar.json' was resolved to '/node_modules/foo/bar/foobar.json', but '--resolveJsonModule' is not used.
2+
3+
4+
==== /tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"baseUrl": ".",
8+
"paths": {
9+
"*": ["node_modules/*", "src/types"]
10+
},
11+
"allowJs": true,
12+
"outDir": "bin"
13+
}
14+
}
15+
16+
==== /a.ts (1 errors) ====
17+
import foobar from "foo/bar/foobar.json";
18+
~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS7042: Module 'foo/bar/foobar.json' was resolved to '/node_modules/foo/bar/foobar.json', but '--resolveJsonModule' is not used.
20+
21+
==== /node_modules/foo/bar/foobar.json (0 errors) ====
22+
{ "a": 10 }
23+

tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.js

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import foobar from "foo/bar/foobar.json";
88

99

10-
//// [/bin/node_modules/foo/bar/foobar.json]
11-
{ "a": 10 }
1210
//// [/bin/a.js]
1311
"use strict";
1412
exports.__esModule = true;

tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.symbols

-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22
import foobar from "foo/bar/foobar.json";
33
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))
44

5-
=== /node_modules/foo/bar/foobar.json ===
6-
{ "a": 10 }
7-
>"a" : Symbol("a", Decl(foobar.json, 0, 1))
8-
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
=== /a.ts ===
22
import foobar from "foo/bar/foobar.json";
3-
>foobar : { "a": number; }
4-
5-
=== /node_modules/foo/bar/foobar.json ===
6-
{ "a": 10 }
7-
>{ "a": 10 } : { "a": number; }
8-
>"a" : number
9-
>10 : 10
3+
>foobar : any
104

0 commit comments

Comments
 (0)