From da3ca7608c3f496100d59d61c5cffdfb8e2e5945 Mon Sep 17 00:00:00 2001 From: jogelin Date: Thu, 17 Aug 2023 11:28:52 +0200 Subject: [PATCH] fix(linter): Normalize paths when in getSourceFilePath (#18601) Co-authored-by: jgelin (cherry picked from commit 1f9b3c4a5ca31276cf9efd513b2abe7da8706ab4) --- .../src/utils/runtime-lint-utils.spec.ts | 17 +++++++++++++++++ .../src/utils/runtime-lint-utils.ts | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts index a3deed02200c8..273d7887bd34d 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts @@ -9,6 +9,7 @@ import { DepConstraint, findConstraintsFor, findTransitiveExternalDependencies, + getSourceFilePath, hasBannedDependencies, hasBannedImport, hasNoneOfTheseTags, @@ -558,3 +559,19 @@ describe('hasNoneOfTheseTags', () => { } ); }); + +describe('getSourceFilePath', () => { + it.each([ + ['/root/libs/dev-kit/package.json', '/root'], + ['/root/libs/dev-kit/package.json', 'C:\\root'], + ['C:\\root\\libs\\dev-kit\\package.json', '/root'], + ['C:\\root\\libs\\dev-kit\\package.json', 'C:\\root'], + ])( + 'should return "libs/dev-kit/package.json" when sourceFileName is "%s" and projectPath is "%s"', + (sourceFileName, projectPath) => { + expect(getSourceFilePath(sourceFileName, projectPath)).toBe( + 'libs/dev-kit/package.json' + ); + } + ); +}); diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts index e9f5200c0e006..c690dcdb05cec 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts @@ -222,8 +222,9 @@ export function onlyLoadChildren( } export function getSourceFilePath(sourceFileName: string, projectPath: string) { - const relativePath = sourceFileName.slice(projectPath.length + 1); - return normalizePath(relativePath); + const normalizedProjectPath = normalizePath(projectPath); + const normalizedSourceFileName = normalizePath(sourceFileName); + return normalizedSourceFileName.slice(normalizedProjectPath.length + 1); } /**