Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix issue with case sensitivity of noUnusedVariable rule on Windows #2819

Merged
merged 2 commits into from May 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/rules/noUnusedVariableRule.ts
Expand Up @@ -368,22 +368,26 @@ function getUnusedCheckedProgram(program: ts.Program, checkParameters: boolean):

function makeUnusedCheckedProgram(program: ts.Program, checkParameters: boolean): ts.Program {
const options = { ...program.getCompilerOptions(), noUnusedLocals: true, ...(checkParameters ? { noUnusedParameters: true } : null) };
const sourceFilesByName = new Map<string, ts.SourceFile>(program.getSourceFiles().map<[string, ts.SourceFile]>((s) => [s.fileName, s]));
const sourceFilesByName = new Map<string, ts.SourceFile>(
program.getSourceFiles().map<[string, ts.SourceFile]>((s) => [getCanonicalFileName(s.fileName), s]));

// tslint:disable object-literal-sort-keys
return ts.createProgram(Array.from(sourceFilesByName.keys()), options, {
fileExists: (f) => sourceFilesByName.has(f),
readFile(f) {
const s = sourceFilesByName.get(f)!;
return s.text;
},
getSourceFile: (f) => sourceFilesByName.get(f)!,
fileExists: (f) => sourceFilesByName.has(getCanonicalFileName(f)),
readFile: (f) => sourceFilesByName.get(getCanonicalFileName(f))!.text,
getSourceFile: (f) => sourceFilesByName.get(getCanonicalFileName(f))!,
getDefaultLibFileName: () => ts.getDefaultLibFileName(options),
writeFile: () => {}, // tslint:disable-line no-empty
getCurrentDirectory: () => "",
getDirectories: () => [],
getCanonicalFileName: (f) => f,
useCaseSensitiveFileNames: () => true,
getCanonicalFileName,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getNewLine: () => "\n",
});
// tslint:enable object-literal-sort-keys

// We need to be careful with file system case sensitivity
function getCanonicalFileName(fileName: string): string {
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
}
}