diff --git a/packages/eslint-plugin/__tests__/fixtures/test_src/feature1/slice1/inner2/sub/index.ts b/packages/eslint-plugin/__tests__/fixtures/test_src/feature1/slice1/inner2/sub/index.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/eslint-plugin/__tests__/shortestImport.test.ts b/packages/eslint-plugin/__tests__/shortestImport.test.ts index c8908845..9d01554a 100644 --- a/packages/eslint-plugin/__tests__/shortestImport.test.ts +++ b/packages/eslint-plugin/__tests__/shortestImport.test.ts @@ -164,6 +164,10 @@ typescriptSetups.forEach((config) => { path: '../inner2', filename: './test_src/feature1/slice1/inner1/index.ts', }, + { + path: '../../inner1/index', + filename: './test_src/feature1/slice1/inner2/sub/index.ts', + }, { path: '~/feature1/slice2/second', filename: './test_src/feature1/slice1/inner1/index.ts', diff --git a/packages/eslint-plugin/src/shortestImport.ts b/packages/eslint-plugin/src/shortestImport.ts index 68fd0c38..7f3ccb1e 100644 --- a/packages/eslint-plugin/src/shortestImport.ts +++ b/packages/eslint-plugin/src/shortestImport.ts @@ -273,12 +273,14 @@ class RuleChecker { preferredAliasPaths: string[]; }) { if (!aliasPaths.length && !baseUrlPaths.length) return relativePath; + const arePreferredAliasPathsInvalid = + preferredAliasPaths.length && + [...aliasPaths, ...baseUrlPaths].some((aliasPath) => + preferredAliasPaths.some((alias) => aliasPath.startsWith(alias)), + ); const shouldAvoidRelative = this.relativeGoesThroughBaseUrl(relativePath, resolvedFilePath) || - [...aliasPaths, ...baseUrlPaths].some((aliasPath) => { - if (!preferredAliasPaths.length) return false; - return preferredAliasPaths.some((alias) => aliasPath.startsWith(alias)); - }); + arePreferredAliasPathsInvalid; const allPathsWithLength = (aliasPaths.length ? aliasPaths : baseUrlPaths) .map((aliasPath) => { const parts = aliasPath.split('/'); @@ -319,7 +321,7 @@ class RuleChecker { if (part === '..') dotsOverPaths += 1; else dotsOverPaths -= 1; }); - return dotsOverPaths > 0; + return dotsOverPaths >= 0; } return relativePathLength < shortestAliasPathLength; }