Skip to content

Commit b983da5

Browse files
authored
Merge pull request microsoft#26457 from Microsoft/baseUrlPathMappingResolveJsonModule
Do not include json file unless --resolveJsonModule is specified
2 parents fd948ac + af71d55 commit b983da5

8 files changed

+87
-2
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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[] {
Lines changed: 23 additions & 0 deletions
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+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.ts] ////
2+
3+
//// [foobar.json]
4+
{ "a": 10 }
5+
6+
//// [a.ts]
7+
import foobar from "foo/bar/foobar.json";
8+
9+
10+
//// [/bin/a.js]
11+
"use strict";
12+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import foobar from "foo/bar/foobar.json";
3+
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
"======== Resolving module 'foo/bar/foobar.json' from '/a.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.json'.",
5+
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.json'.",
6+
"Module name 'foo/bar/foobar.json', matched pattern '*'.",
7+
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.json'.",
8+
"File '/node_modules/foo/bar/foobar.json' exist - use it as a name resolution result.",
9+
"======== Module name 'foo/bar/foobar.json' was successfully resolved to '/node_modules/foo/bar/foobar.json'. ========"
10+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import foobar from "foo/bar/foobar.json";
3+
>foobar : any
4+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @noImplicitReferences: true
2+
// @traceResolution: true
3+
// @allowJs: true
4+
// @esModuleInterop: true
5+
// @fullEmitPaths: true
6+
// @resolveJsonModule: false
7+
8+
// @Filename: /node_modules/foo/bar/foobar.json
9+
{ "a": 10 }
10+
11+
// @Filename: /a.ts
12+
import foobar from "foo/bar/foobar.json";
13+
14+
// @Filename: /tsconfig.json
15+
{
16+
"compilerOptions": {
17+
"baseUrl": ".",
18+
"paths": {
19+
"*": ["node_modules/*", "src/types"]
20+
},
21+
"allowJs": true,
22+
"outDir": "bin"
23+
}
24+
}

0 commit comments

Comments
 (0)