From d36df0dda5589d8bb4c8c16a946660000d2f6dfc Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 8 Jan 2021 12:03:29 -0800 Subject: [PATCH] Try file if matched pattern specifies the extension instead of all the time (#42246) * Test case for path mapping with extension * Try file if matched pattern specifies the extension instead of all the time Fixes #39743 --- src/compiler/moduleNameResolver.ts | 2 +- ...olutionWithExtensions_withPaths.errors.txt | 55 +++++++++++++++++++ ...oduleResolutionWithExtensions_withPaths.js | 30 ++++++++++ ...ResolutionWithExtensions_withPaths.symbols | 26 +++++++++ ...olutionWithExtensions_withPaths.trace.json | 48 ++++++++++++++++ ...leResolutionWithExtensions_withPaths.types | 26 +++++++++ ...eResolution_withExtensionInName.trace.json | 2 - ...ithExtension_MapedToNodeModules.trace.json | 31 +++++++++++ ...ResolveJsonModuleAndPathMapping.errors.txt | 4 +- ...ResolveJsonModuleAndPathMapping.trace.json | 30 +++++++++- .../requireOfJsonFile_PathMapping.js | 12 ++++ .../requireOfJsonFile_PathMapping.symbols | 8 +++ .../requireOfJsonFile_PathMapping.trace.json | 43 +++++++++++++++ .../requireOfJsonFile_PathMapping.types | 10 ++++ ...oduleResolutionWithExtensions_withPaths.ts | 40 ++++++++++++++ .../compiler/requireOfJsonFile_PathMapping.ts | 24 ++++++++ 16 files changed, 384 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/moduleResolutionWithExtensions_withPaths.errors.txt create mode 100644 tests/baselines/reference/moduleResolutionWithExtensions_withPaths.js create mode 100644 tests/baselines/reference/moduleResolutionWithExtensions_withPaths.symbols create mode 100644 tests/baselines/reference/moduleResolutionWithExtensions_withPaths.trace.json create mode 100644 tests/baselines/reference/moduleResolutionWithExtensions_withPaths.types create mode 100644 tests/baselines/reference/requireOfJsonFile_PathMapping.js create mode 100644 tests/baselines/reference/requireOfJsonFile_PathMapping.symbols create mode 100644 tests/baselines/reference/requireOfJsonFile_PathMapping.trace.json create mode 100644 tests/baselines/reference/requireOfJsonFile_PathMapping.types create mode 100644 tests/cases/compiler/moduleResolutionWithExtensions_withPaths.ts create mode 100644 tests/cases/compiler/requireOfJsonFile_PathMapping.ts diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 6329387b281..5348db0973d 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -1377,7 +1377,7 @@ namespace ts { trace(state.host, Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); } // A path mapping may have an extension, in contrast to an import, which should omit it. - const extension = tryGetExtensionFromPath(candidate); + const extension = tryGetExtensionFromPath(subst); if (extension !== undefined) { const path = tryFile(candidate, onlyRecordFailures, state); if (path !== undefined) { diff --git a/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.errors.txt b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.errors.txt new file mode 100644 index 00000000000..32fdbe484ff --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.errors.txt @@ -0,0 +1,55 @@ +error TS6504: File '/node_modules/foo/lib/test.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/relative.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Matched by include pattern '**/*' in '/tsconfig.json' + + +!!! error TS6504: File '/node_modules/foo/lib/test.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/relative.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Matched by include pattern '**/*' in '/tsconfig.json' +==== /tsconfig.json (0 errors) ==== + { + "compilerOptions": { + "outDir": "lib", + "target": "ES6", + "module": "ES6", + "baseUrl": "/", + "moduleResolution": "Node", + "noImplicitAny": true, + "traceResolution": true, + "paths": { + "foo/*": ["node_modules/foo/lib/*"] + } + } + } + +==== /node_modules/foo/lib/test.js (0 errors) ==== + export function test() { + console.log("test"); + } + +==== /node_modules/foo/lib/test.d.ts (0 errors) ==== + export declare function test(): void; + +==== /relative.js (0 errors) ==== + export function relative() { + console.log("test"); + } + +==== /relative.d.ts (0 errors) ==== + export declare function relative(): void; + + +==== /test.ts (0 errors) ==== + import { test } from "foo/test.js"; + import { test as test2 } from "foo/test"; + import { relative } from "./relative.js"; + import { relative as relative2 } from "./relative"; + + + \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.js b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.js new file mode 100644 index 00000000000..58d7c51bba5 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.js @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/moduleResolutionWithExtensions_withPaths.ts] //// + +//// [test.js] +export function test() { + console.log("test"); +} + +//// [test.d.ts] +export declare function test(): void; + +//// [relative.js] +export function relative() { + console.log("test"); +} + +//// [relative.d.ts] +export declare function relative(): void; + + +//// [test.ts] +import { test } from "foo/test.js"; +import { test as test2 } from "foo/test"; +import { relative } from "./relative.js"; +import { relative as relative2 } from "./relative"; + + + + +//// [test.js] +export {}; diff --git a/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.symbols b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.symbols new file mode 100644 index 00000000000..501d4ac2874 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.symbols @@ -0,0 +1,26 @@ +=== /node_modules/foo/lib/test.d.ts === +export declare function test(): void; +>test : Symbol(test, Decl(test.d.ts, 0, 0)) + +=== /relative.d.ts === +export declare function relative(): void; +>relative : Symbol(relative, Decl(relative.d.ts, 0, 0)) + + +=== /test.ts === +import { test } from "foo/test.js"; +>test : Symbol(test, Decl(test.ts, 0, 8)) + +import { test as test2 } from "foo/test"; +>test : Symbol(test, Decl(test.d.ts, 0, 0)) +>test2 : Symbol(test2, Decl(test.ts, 1, 8)) + +import { relative } from "./relative.js"; +>relative : Symbol(relative, Decl(test.ts, 2, 8)) + +import { relative as relative2 } from "./relative"; +>relative : Symbol(relative, Decl(relative.d.ts, 0, 0)) +>relative2 : Symbol(relative2, Decl(test.ts, 3, 8)) + + + diff --git a/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.trace.json b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.trace.json new file mode 100644 index 00000000000..f99d99800f4 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.trace.json @@ -0,0 +1,48 @@ +[ + "======== Resolving module 'foo/test.js' from '/test.ts'. ========", + "Explicitly specified module resolution kind: 'NodeJs'.", + "'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/test.js'.", + "'paths' option is specified, looking for a pattern to match module name 'foo/test.js'.", + "Module name 'foo/test.js', matched pattern 'foo/*'.", + "Trying substitution 'node_modules/foo/lib/*', candidate module location: 'node_modules/foo/lib/test.js'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/lib/test.js', target file type 'TypeScript'.", + "File '/node_modules/foo/lib/test.js.ts' does not exist.", + "File '/node_modules/foo/lib/test.js.tsx' does not exist.", + "File '/node_modules/foo/lib/test.js.d.ts' does not exist.", + "File name '/node_modules/foo/lib/test.js' has a '.js' extension - stripping it.", + "File '/node_modules/foo/lib/test.ts' does not exist.", + "File '/node_modules/foo/lib/test.tsx' does not exist.", + "File '/node_modules/foo/lib/test.d.ts' exist - use it as a name resolution result.", + "File '/node_modules/foo/package.json' does not exist.", + "======== Module name 'foo/test.js' was successfully resolved to '/node_modules/foo/lib/test.d.ts'. ========", + "======== Resolving module 'foo/test' from '/test.ts'. ========", + "Explicitly specified module resolution kind: 'NodeJs'.", + "'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/test'.", + "'paths' option is specified, looking for a pattern to match module name 'foo/test'.", + "Module name 'foo/test', matched pattern 'foo/*'.", + "Trying substitution 'node_modules/foo/lib/*', candidate module location: 'node_modules/foo/lib/test'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/lib/test', target file type 'TypeScript'.", + "File '/node_modules/foo/lib/test.ts' does not exist.", + "File '/node_modules/foo/lib/test.tsx' does not exist.", + "File '/node_modules/foo/lib/test.d.ts' exist - use it as a name resolution result.", + "File '/node_modules/foo/package.json' does not exist.", + "======== Module name 'foo/test' was successfully resolved to '/node_modules/foo/lib/test.d.ts'. ========", + "======== Resolving module './relative.js' from '/test.ts'. ========", + "Explicitly specified module resolution kind: 'NodeJs'.", + "Loading module as file / folder, candidate module location '/relative.js', target file type 'TypeScript'.", + "File '/relative.js.ts' does not exist.", + "File '/relative.js.tsx' does not exist.", + "File '/relative.js.d.ts' does not exist.", + "File name '/relative.js' has a '.js' extension - stripping it.", + "File '/relative.ts' does not exist.", + "File '/relative.tsx' does not exist.", + "File '/relative.d.ts' exist - use it as a name resolution result.", + "======== Module name './relative.js' was successfully resolved to '/relative.d.ts'. ========", + "======== Resolving module './relative' from '/test.ts'. ========", + "Explicitly specified module resolution kind: 'NodeJs'.", + "Loading module as file / folder, candidate module location '/relative', target file type 'TypeScript'.", + "File '/relative.ts' does not exist.", + "File '/relative.tsx' does not exist.", + "File '/relative.d.ts' exist - use it as a name resolution result.", + "======== Module name './relative' was successfully resolved to '/relative.d.ts'. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.types b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.types new file mode 100644 index 00000000000..6e720932a2e --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithExtensions_withPaths.types @@ -0,0 +1,26 @@ +=== /node_modules/foo/lib/test.d.ts === +export declare function test(): void; +>test : () => void + +=== /relative.d.ts === +export declare function relative(): void; +>relative : () => void + + +=== /test.ts === +import { test } from "foo/test.js"; +>test : () => void + +import { test as test2 } from "foo/test"; +>test : () => void +>test2 : () => void + +import { relative } from "./relative.js"; +>relative : () => void + +import { relative as relative2 } from "./relative"; +>relative : () => void +>relative2 : () => void + + + diff --git a/tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json b/tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json index 216442fb378..c23f61c1c3e 100644 --- a/tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json +++ b/tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json @@ -5,7 +5,6 @@ "'paths' option is specified, looking for a pattern to match module name 'zone.js'.", "Module name 'zone.js', matched pattern '*'.", "Trying substitution 'foo/*', candidate module location: 'foo/zone.js'.", - "File '/foo/zone.js' does not exist.", "Loading module as file / folder, candidate module location '/foo/zone.js', target file type 'TypeScript'.", "File '/foo/zone.js.ts' does not exist.", "File '/foo/zone.js.tsx' does not exist.", @@ -25,7 +24,6 @@ "'paths' option is specified, looking for a pattern to match module name 'zone.tsx'.", "Module name 'zone.tsx', matched pattern '*'.", "Trying substitution 'foo/*', candidate module location: 'foo/zone.tsx'.", - "File '/foo/zone.tsx' does not exist.", "Loading module as file / folder, candidate module location '/foo/zone.tsx', target file type 'TypeScript'.", "File '/foo/zone.tsx.ts' does not exist.", "File '/foo/zone.tsx.tsx' does not exist.", diff --git a/tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.trace.json b/tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.trace.json index dac9d19bc0f..b4aa0889de7 100644 --- a/tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.trace.json +++ b/tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.trace.json @@ -5,6 +5,37 @@ "'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.", "Module name 'foo/bar/foobar.js', matched pattern '*'.", "Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.js', target file type 'TypeScript'.", + "File '/node_modules/foo/bar/foobar.js.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.js.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.js.d.ts' does not exist.", + "File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.", + "File '/node_modules/foo/bar/foobar.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.d.ts' does not exist.", + "Directory '/node_modules/foo/bar/foobar.js' does not exist, skipping all lookups in it.", + "Trying substitution 'src/types', candidate module location: 'src/types'.", + "Loading module as file / folder, candidate module location '/src/types', target file type 'TypeScript'.", + "Loading module 'foo/bar/foobar.js' from 'node_modules' folder, target file type 'TypeScript'.", + "File '/node_modules/foo/package.json' does not exist.", + "File '/node_modules/foo/bar/foobar.js.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.js.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.js.d.ts' does not exist.", + "File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.", + "File '/node_modules/foo/bar/foobar.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.d.ts' does not exist.", + "Directory '/node_modules/@types' does not exist, skipping all lookups in it.", + "File name '/node_modules/@types/foo/bar/foobar.js' has a '.js' extension - stripping it.", + "'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.js'.", + "'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.", + "Module name 'foo/bar/foobar.js', matched pattern '*'.", + "Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.js', target file type 'JavaScript'.", + "File '/node_modules/foo/bar/foobar.js.js' does not exist.", + "File '/node_modules/foo/bar/foobar.js.jsx' does not exist.", + "File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.", "File '/node_modules/foo/bar/foobar.js' exist - use it as a name resolution result.", + "File '/node_modules/foo/package.json' does not exist.", "======== Module name 'foo/bar/foobar.js' was successfully resolved to '/node_modules/foo/bar/foobar.js'. ========" ] \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.errors.txt b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.errors.txt index fee86f2174c..de95af399d5 100644 --- a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.errors.txt +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.errors.txt @@ -1,4 +1,4 @@ -/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. +/a.ts(1,20): error TS2732: Cannot find module 'foo/bar/foobar.json'. Consider using '--resolveJsonModule' to import module with '.json' extension. ==== /tsconfig.json (0 errors) ==== @@ -16,7 +16,7 @@ ==== /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. +!!! error TS2732: Cannot find module 'foo/bar/foobar.json'. Consider using '--resolveJsonModule' to import module with '.json' extension. ==== /node_modules/foo/bar/foobar.json (0 errors) ==== { "a": 10 } diff --git a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.trace.json b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.trace.json index ed48ec5baac..400f05ead56 100644 --- a/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.trace.json +++ b/tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.trace.json @@ -5,6 +5,32 @@ "'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'. ========" + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'TypeScript'.", + "File '/node_modules/foo/bar/foobar.json.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.json.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.", + "Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.", + "Trying substitution 'src/types', candidate module location: 'src/types'.", + "Loading module as file / folder, candidate module location '/src/types', target file type 'TypeScript'.", + "Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'TypeScript'.", + "File '/node_modules/foo/package.json' does not exist.", + "File '/node_modules/foo/bar/foobar.json.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.json.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.", + "Directory '/node_modules/@types' does not exist, skipping all lookups in it.", + "'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'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'JavaScript'.", + "File '/node_modules/foo/bar/foobar.json.js' does not exist.", + "File '/node_modules/foo/bar/foobar.json.jsx' does not exist.", + "Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.", + "Trying substitution 'src/types', candidate module location: 'src/types'.", + "Loading module as file / folder, candidate module location '/src/types', target file type 'JavaScript'.", + "Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'JavaScript'.", + "File '/node_modules/foo/package.json' does not exist.", + "File '/node_modules/foo/bar/foobar.json.js' does not exist.", + "File '/node_modules/foo/bar/foobar.json.jsx' does not exist.", + "======== Module name 'foo/bar/foobar.json' was not resolved. ========" ] \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFile_PathMapping.js b/tests/baselines/reference/requireOfJsonFile_PathMapping.js new file mode 100644 index 00000000000..4c6f409383f --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile_PathMapping.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/requireOfJsonFile_PathMapping.ts] //// + +//// [foobar.json] +{ "a": 10 } + +//// [a.ts] +import foobar from "foo/bar/foobar.json"; + + +//// [/bin/a.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/requireOfJsonFile_PathMapping.symbols b/tests/baselines/reference/requireOfJsonFile_PathMapping.symbols new file mode 100644 index 00000000000..b934b22d66a --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile_PathMapping.symbols @@ -0,0 +1,8 @@ +=== /a.ts === +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)) + diff --git a/tests/baselines/reference/requireOfJsonFile_PathMapping.trace.json b/tests/baselines/reference/requireOfJsonFile_PathMapping.trace.json new file mode 100644 index 00000000000..53987c27142 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile_PathMapping.trace.json @@ -0,0 +1,43 @@ +[ + "======== 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'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'TypeScript'.", + "File '/node_modules/foo/bar/foobar.json.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.json.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.", + "Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.", + "Trying substitution 'src/types', candidate module location: 'src/types'.", + "Loading module as file / folder, candidate module location '/src/types', target file type 'TypeScript'.", + "Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'TypeScript'.", + "File '/node_modules/foo/package.json' does not exist.", + "File '/node_modules/foo/bar/foobar.json.ts' does not exist.", + "File '/node_modules/foo/bar/foobar.json.tsx' does not exist.", + "File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.", + "Directory '/node_modules/@types' does not exist, skipping all lookups in it.", + "'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'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'JavaScript'.", + "File '/node_modules/foo/bar/foobar.json.js' does not exist.", + "File '/node_modules/foo/bar/foobar.json.jsx' does not exist.", + "Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.", + "Trying substitution 'src/types', candidate module location: 'src/types'.", + "Loading module as file / folder, candidate module location '/src/types', target file type 'JavaScript'.", + "Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'JavaScript'.", + "File '/node_modules/foo/package.json' does not exist.", + "File '/node_modules/foo/bar/foobar.json.js' does not exist.", + "File '/node_modules/foo/bar/foobar.json.jsx' does not exist.", + "'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'.", + "Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'Json'.", + "File '/node_modules/foo/bar/foobar.json' exist - use it as a name resolution result.", + "File '/node_modules/foo/package.json' does not exist.", + "======== Module name 'foo/bar/foobar.json' was successfully resolved to '/node_modules/foo/bar/foobar.json'. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/requireOfJsonFile_PathMapping.types b/tests/baselines/reference/requireOfJsonFile_PathMapping.types new file mode 100644 index 00000000000..a1e1094ac9b --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFile_PathMapping.types @@ -0,0 +1,10 @@ +=== /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 + diff --git a/tests/cases/compiler/moduleResolutionWithExtensions_withPaths.ts b/tests/cases/compiler/moduleResolutionWithExtensions_withPaths.ts new file mode 100644 index 00000000000..3049c876aff --- /dev/null +++ b/tests/cases/compiler/moduleResolutionWithExtensions_withPaths.ts @@ -0,0 +1,40 @@ +// @filename: /tsconfig.json +{ + "compilerOptions": { + "outDir": "lib", + "target": "ES6", + "module": "ES6", + "baseUrl": "/", + "moduleResolution": "Node", + "noImplicitAny": true, + "traceResolution": true, + "paths": { + "foo/*": ["node_modules/foo/lib/*"] + } + } +} + +// @filename: /node_modules/foo/lib/test.js +export function test() { + console.log("test"); +} + +// @filename: /node_modules/foo/lib/test.d.ts +export declare function test(): void; + +// @filename: /relative.js +export function relative() { + console.log("test"); +} + +// @filename: /relative.d.ts +export declare function relative(): void; + + +// @filename: /test.ts +import { test } from "foo/test.js"; +import { test as test2 } from "foo/test"; +import { relative } from "./relative.js"; +import { relative as relative2 } from "./relative"; + + diff --git a/tests/cases/compiler/requireOfJsonFile_PathMapping.ts b/tests/cases/compiler/requireOfJsonFile_PathMapping.ts new file mode 100644 index 00000000000..f61692ae83d --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFile_PathMapping.ts @@ -0,0 +1,24 @@ +// @noImplicitReferences: true +// @traceResolution: true +// @allowJs: true +// @esModuleInterop: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @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" + } +}