Skip to content

Commit

Permalink
fix: shortest path case where relative should be preferred
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Nov 27, 2023
1 parent b773c35 commit 7b1ed40
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Empty file.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/__tests__/shortestImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 7 additions & 5 deletions packages/eslint-plugin/src/shortestImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down Expand Up @@ -319,7 +321,7 @@ class RuleChecker {
if (part === '..') dotsOverPaths += 1;
else dotsOverPaths -= 1;
});
return dotsOverPaths > 0;
return dotsOverPaths >= 0;
}
return relativePathLength < shortestAliasPathLength;
}
Expand Down

0 comments on commit 7b1ed40

Please sign in to comment.