fix: CompilerHost.getSourceFile is being called for odd filenames besides the one being compiled

Ignore falsy file names from `getDefaultLibraryFileName`

Closes: #13629
This commit is contained in:
Alan Agius 2018-04-02 20:58:37 +02:00
parent 7714a2bb5b
commit 7e482b27fb
4 changed files with 43 additions and 3 deletions

View File

@ -154,6 +154,7 @@ var harnessSources = harnessCoreSources.concat([
"transform.ts",
"customTransforms.ts",
"programMissingFiles.ts",
"programNoParseFalsyFileNames.ts",
"symbolWalker.ts",
"languageService.ts",
"publicApi.ts",

View File

@ -611,8 +611,9 @@ namespace ts {
if (!skipDefaultLib) {
// If '--lib' is not specified, include default library file according to '--target'
// otherwise, using options specified in '--lib' instead of '--target' default library file
if (!options.lib) {
processRootFile(getDefaultLibraryFileName(), /*isDefaultLib*/ true);
const defaultLibraryFileName = getDefaultLibraryFileName();
if (!options.lib && defaultLibraryFileName) {
processRootFile(defaultLibraryFileName, /*isDefaultLib*/ true);
}
else {
forEach(options.lib, libFileName => {
@ -1117,7 +1118,7 @@ namespace ts {
// otherwise, using options specified in '--lib' instead of '--target' default library file
const equalityComparer = host.useCaseSensitiveFileNames() ? equateStringsCaseSensitive : equateStringsCaseInsensitive;
if (!options.lib) {
return equalityComparer(file.fileName, getDefaultLibraryFileName());
return equalityComparer(file.fileName, getDefaultLibraryFileName());
}
else {
return forEach(options.lib, libFileName => equalityComparer(file.fileName, combinePaths(defaultLibraryPath, libFileName)));

View File

@ -135,6 +135,7 @@
"./unittests/telemetry.ts",
"./unittests/languageService.ts",
"./unittests/programMissingFiles.ts",
"./unittests/programNoParseFalsyFileNames.ts",
"./unittests/publicApi.ts",
"./unittests/hostNewLineSupport.ts"
]

View File

@ -0,0 +1,37 @@
/// <reference path="..\harness.ts" />
namespace ts {
describe("programNoParseFalsyFileNames", () => {
let program: Program;
beforeEach(() => {
const testSource = `
class Foo extends HTMLElement {
bar: string = 'baz';
}`;
const host: CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ScriptTarget, _onError?: (message: string) => void) => {
return fileName === "test.ts" ? createSourceFile(fileName, testSource, languageVersion) : undefined;
},
getDefaultLibFileName: () => "",
writeFile: (_fileName, _content) => { throw new Error("unsupported"); },
getCurrentDirectory: () => sys.getCurrentDirectory(),
getCanonicalFileName: fileName => sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
getNewLine: () => sys.newLine,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
fileExists: fileName => fileName === "test.ts",
readFile: fileName => fileName === "test.ts" ? testSource : undefined,
resolveModuleNames: (_moduleNames: string[], _containingFile: string) => { throw new Error("unsupported"); },
getDirectories: _path => { throw new Error("unsupported"); },
};
program = createProgram(["test.ts"], { module: ModuleKind.ES2015 }, host);
});
it("should not have missing file paths", () => {
assert(program.getSourceFiles().length === 1, "expected 'getSourceFiles' length to be 1");
assert(program.getMissingFilePaths().length === 0, "expected 'getMissingFilePaths' length to be 0");
assert(program.getFileProcessingDiagnostics().getDiagnostics().length === 0, "expected 'getFileProcessingDiagnostics' length to be 0");
});
});
}