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

Commit

Permalink
Fix issue with case sensitivity of noUnusedVariable rule on Windows (#…
Browse files Browse the repository at this point in the history
…2819)

When TSLint is being used to provide as-you-type linting as in Angular
IDE, incorrect configuration of program created in
makeUnusedCheckedProgram function is causing whole functionality to blow
up. The core of the issue is that SourceFiles are being assigned case
sensitive path when program is recreated for TSLint validation, which
later causes that wrong path to stick and file not found problems appear
in whole IDE.
  • Loading branch information
piotrtomiak authored and adidahiya committed May 24, 2017
1 parent aebb1d6 commit 4889953
Showing 1 changed file with 13 additions and 9 deletions.
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();
}
}

0 comments on commit 4889953

Please sign in to comment.