Do not include json file unless --resolveJsonModule is specified

Fixes #26402
This commit is contained in:
Sheetal Nandi 2018-08-14 16:10:32 -07:00
parent aa8f1e5b6a
commit af71d55a49
6 changed files with 34 additions and 15 deletions

View File

@ -3941,6 +3941,10 @@
"category": "Error",
"code": 7041
},
"Module '{0}' was resolved to '{1}', but '--resolveJsonModule' is not used.": {
"category": "Error",
"code": 7042
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000
@ -4576,4 +4580,4 @@
"category": "Message",
"code": 95066
}
}
}

View File

@ -2849,7 +2849,6 @@ namespace ts {
switch (extension) {
case Extension.Ts:
case Extension.Dts:
case Extension.Json: // Since module is resolved to json file only when --resolveJsonModule, we dont need further check
// These are always allowed.
return undefined;
case Extension.Tsx:
@ -2858,6 +2857,8 @@ namespace ts {
return needJsx() || needAllowJs();
case Extension.Js:
return needAllowJs();
case Extension.Json:
return needResolveJsonModule();
}
function needJsx() {
@ -2866,6 +2867,9 @@ namespace ts {
function needAllowJs() {
return options.allowJs || !getStrictOptionValue(options, "noImplicitAny") ? undefined : Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
}
function needResolveJsonModule() {
return options.resolveJsonModule ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_resolveJsonModule_is_not_used;
}
}
function getModuleNames({ imports, moduleAugmentations }: SourceFile): string[] {

View File

@ -0,0 +1,23 @@
/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.
==== /tsconfig.json (0 errors) ====
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types"]
},
"allowJs": true,
"outDir": "bin"
}
}
==== /a.ts (1 errors) ====
import foobar from "foo/bar/foobar.json";
~~~~~~~~~~~~~~~~~~~~~
!!! error TS7042: Module 'foo/bar/foobar.json' was resolved to '/node_modules/foo/bar/foobar.json', but '--resolveJsonModule' is not used.
==== /node_modules/foo/bar/foobar.json (0 errors) ====
{ "a": 10 }

View File

@ -7,8 +7,6 @@
import foobar from "foo/bar/foobar.json";
//// [/bin/node_modules/foo/bar/foobar.json]
{ "a": 10 }
//// [/bin/a.js]
"use strict";
exports.__esModule = true;

View File

@ -2,7 +2,3 @@
import foobar from "foo/bar/foobar.json";
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))
=== /node_modules/foo/bar/foobar.json ===
{ "a": 10 }
>"a" : Symbol("a", Decl(foobar.json, 0, 1))

View File

@ -1,10 +1,4 @@
=== /a.ts ===
import foobar from "foo/bar/foobar.json";
>foobar : { "a": number; }
=== /node_modules/foo/bar/foobar.json ===
{ "a": 10 }
>{ "a": 10 } : { "a": number; }
>"a" : number
>10 : 10
>foobar : any