classic resolution: don't perform folder walk if module name is relative

This commit is contained in:
Vladimir Matveev 2016-02-12 16:30:19 -08:00
parent 02df49994b
commit 9960064bc3
4 changed files with 44 additions and 24 deletions

View file

@ -533,18 +533,25 @@ namespace ts {
}
let referencedSourceFile: string;
while (true) {
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
if (referencedSourceFile) {
break;
if (moduleHasNonRelativeName(moduleName)) {
while (true) {
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
if (referencedSourceFile) {
break;
}
const parentPath = getDirectoryPath(containingDirectory);
if (parentPath === containingDirectory) {
break;
}
containingDirectory = parentPath;
}
const parentPath = getDirectoryPath(containingDirectory);
if (parentPath === containingDirectory) {
break;
}
containingDirectory = parentPath;
}
else {
const candidate = normalizePath(combinePaths(containingDirectory, moduleName));
referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
}
return referencedSourceFile
? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations }

View file

@ -0,0 +1,11 @@
tests/cases/compiler/somefolder/a.ts(2,17): error TS2307: Cannot find module './b'.
==== tests/cases/compiler/somefolder/a.ts (1 errors) ====
import {x} from "./b"
~~~~~
!!! error TS2307: Cannot find module './b'.
==== tests/cases/compiler/b.ts (0 errors) ====
export let x = 1;

View file

@ -0,0 +1,7 @@
// @module:amd
// @filename: somefolder/a.ts
import {x} from "./b"
// @filename: b.ts
export let x = 1;

View file

@ -48,7 +48,7 @@ module ts {
return hasProperty(directories, path);
},
fileExists: path => {
assert.isTrue(hasProperty(directories, getDirectoryPath(path)), "'fileExists' request in non-existing directory");
assert.isTrue(hasProperty(directories, getDirectoryPath(path)), `'fileExists' '${path}' request in non-existing directory`);
return hasProperty(map, path);
}
}
@ -814,7 +814,6 @@ import b = require("./moduleB.ts");
it ("classic + rootDirs", () => {
test(/*hasDirectoryExists*/ false);
test(/*hasDirectoryExists*/ true);
function test(hasDirectoryExists: boolean) {
let file1: File = { name: "/root/folder1/file1.ts" };
@ -844,24 +843,20 @@ import b = require("./moduleB.ts");
"/root/generated/folder1/file1.d.ts",
// then try alternative rootDir entry
]);
check("../folder1/file1_1", file3, file4, [
// load from initial location
check("folder1/file1_1", file3, file4, [
// current location
"/root/generated/folder2/folder1/file1_1.ts",
"/root/generated/folder2/folder1/file1_1.tsx",
"/root/generated/folder2/folder1/file1_1.d.ts",
// other entry in rootDirs
"/root/generated/folder1/file1_1.ts",
"/root/generated/folder1/file1_1.tsx",
"/root/generated/folder1/file1_1.d.ts",
// load from alternative rootDir entry
"/root/folder1/file1_1.ts",
"/root/folder1/file1_1.tsx",
"/root/folder1/file1_1.d.ts",
// fallback to classic
// step1: initial location
"/root/generated/folder1/file1_1.ts",
"/root/generated/folder1/file1_1.tsx",
"/root/generated/folder1/file1_1.d.ts",
// step2: walk 1 level up
// fallback
"/root/folder1/file1_1.ts",
"/root/folder1/file1_1.tsx",
"/root/folder1/file1_1.d.ts",
// found one
]);
function check(name: string, container: File, expected: File, expectedFailedLookups: string[]) {