record resolution for relative file name if file was found via absolute name

This commit is contained in:
Vladimir Matveev
2015-09-26 21:29:07 -07:00
parent d9559d58ca
commit dae5a626da
2 changed files with 113 additions and 49 deletions

View File

@@ -163,4 +163,66 @@ module ts {
]);
});
});
describe("Module resolution - relative imports", () => {
it("should find all modules", () => {
const options: CompilerOptions = { module: ModuleKind.CommonJS };
const files: Map<string> = {
"/a/b/c/first/shared.ts": `
class A {}
export = A`,
"/a/b/c/first/second/class_a.ts": `
import Shared = require('../shared');
import C = require('../../third/class_c');
class B {}
export = B;`,
"/a/b/c/third/class_c.ts":`
import Shared = require('../first/shared');
class C {}
export = C;
`
};
const currentDirectory = "/a/b/c/first/second";
const host: CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ScriptTarget) => {
let path = normalizePath(combinePaths(currentDirectory, fileName));
return hasProperty(files, path) ? createSourceFile(fileName, files[path], languageVersion) : undefined;
},
getDefaultLibFileName: () => "lib.d.ts",
writeFile: (fileName, content): void => { throw new Error("NotImplemented"); },
getCurrentDirectory: () => currentDirectory,
getCanonicalFileName: fileName => fileName.toLowerCase(),
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => false,
fileExists: fileName => {
let path = normalizePath(combinePaths(currentDirectory, fileName));
return hasProperty(files, path);
},
readFile: (fileName): string => { throw new Error("NotImplemented"); }
};
const program = createProgram(["class_a.ts"], options, host);
assert.equal(program.getSourceFiles().length, 3);
const syntacticDiagnostics = program.getSyntacticDiagnostics();
assert.equal(syntacticDiagnostics.length, 0, `expect no syntactic diagnostics, got: ${JSON.stringify(syntacticDiagnostics.map(diagnosticToString))}`);
const semanticDiagnostics = program.getSemanticDiagnostics();
assert.equal(semanticDiagnostics.length, 0, `expect no semantic diagnostics, got: ${JSON.stringify(semanticDiagnostics.map(diagnosticToString))}`);
});
function diagnosticToString(diagnostic: Diagnostic) {
let output = "";
if (diagnostic.file) {
let loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
output += `${ diagnostic.file.fileName }(${ loc.line + 1 },${ loc.character + 1 }): `;
}
let category = DiagnosticCategory[diagnostic.category].toLowerCase();
output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }${ sys.newLine }`;
return output;
}
});
}