Skip to content

Commit

Permalink
Merge pull request #280 from microsoft/fixDosPathComparison
Browse files Browse the repository at this point in the history
Fix issue with path comparison for typesVersions on Windows file systems
  • Loading branch information
rbuckton authored and sandersn committed Nov 29, 2021
1 parent 4acd49c commit 3a8fe19
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/dtslint/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,23 @@ export function isExternalDependency(file: TsType.SourceFile, dirPath: string, p
return !startsWithDirectory(file.fileName, dirPath) || program.isSourceFileFromExternalLibrary(file);
}

function normalizePath(file: string) {
// replaces '\' with '/' and forces all DOS drive letters to be upper-case
return normalize(file)
.replace(/\\/g, "/")
.replace(/^[a-z](?=:)/, c => c.toUpperCase());
}

function isTypesVersionPath(fileName: string, dirPath: string) {
const subdirPath = withoutPrefix(fileName, dirPath);
const normalFileName = normalizePath(fileName);
const normalDirPath = normalizePath(dirPath);
const subdirPath = withoutPrefix(normalFileName, normalDirPath);
return subdirPath && /^\/ts\d+\.\d/.test(subdirPath);
}

function startsWithDirectory(filePath: string, dirPath: string): boolean {
const normalFilePath = normalize(filePath);
const normalDirPath = normalize(dirPath);
assert(!normalDirPath.endsWith("/") && !normalDirPath.endsWith("\\"));
const normalFilePath = normalizePath(filePath);
const normalDirPath = normalizePath(dirPath).replace(/\/$/, "");
return normalFilePath.startsWith(normalDirPath + "/") || normalFilePath.startsWith(normalDirPath + "\\");
}

Expand Down

0 comments on commit 3a8fe19

Please sign in to comment.