Merge pull request #26457 from Microsoft/baseUrlPathMappingResolveJsonModule

Do not include json file unless --resolveJsonModule is specified
This commit is contained in:
Sheetal Nandi 2018-08-15 11:43:01 -07:00 committed by GitHub
commit b983da55fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 2 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

@ -0,0 +1,12 @@
//// [tests/cases/compiler/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.ts] ////
//// [foobar.json]
{ "a": 10 }
//// [a.ts]
import foobar from "foo/bar/foobar.json";
//// [/bin/a.js]
"use strict";
exports.__esModule = true;

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import foobar from "foo/bar/foobar.json";
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))

View file

@ -0,0 +1,10 @@
[
"======== Resolving module 'foo/bar/foobar.json' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.json'.",
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.json'.",
"Module name 'foo/bar/foobar.json', matched pattern '*'.",
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.json'.",
"File '/node_modules/foo/bar/foobar.json' exist - use it as a name resolution result.",
"======== Module name 'foo/bar/foobar.json' was successfully resolved to '/node_modules/foo/bar/foobar.json'. ========"
]

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import foobar from "foo/bar/foobar.json";
>foobar : any

View file

@ -0,0 +1,24 @@
// @noImplicitReferences: true
// @traceResolution: true
// @allowJs: true
// @esModuleInterop: true
// @fullEmitPaths: true
// @resolveJsonModule: false
// @Filename: /node_modules/foo/bar/foobar.json
{ "a": 10 }
// @Filename: /a.ts
import foobar from "foo/bar/foobar.json";
// @Filename: /tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types"]
},
"allowJs": true,
"outDir": "bin"
}
}