Skip to content

Commit

Permalink
fix: shortest import should prefer alias with ~ at the start
Browse files Browse the repository at this point in the history
Resolves the following scenario;
* `../Common/ShopSelect`
* `~/Common/ShopSelect`

These are considered “same length” but we want to prefer the `~` alias path as it’s easier to read than the relative path.
Found and fixed another preferred alias’ bug.
  • Loading branch information
SimeonC committed Nov 2, 2023
1 parent 912df07 commit 5d771a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
9 changes: 9 additions & 0 deletions packages/eslint-plugin/__tests__/shortestImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,20 @@ typescriptSetups.forEach((config) => {
path: '~/feature2/index',
filename: './test_src/feature1/slice1/index.ts',
},
{
path: '~/feature2/index',
filename: './test_src/feature1/index.ts',
},
{
path: '~/feature1/index',
filename: './test_src/feature1/slice1/index.ts',
options: [['~/feature1', 'feature1']],
},
{
path: '~/feature1/slice1',
filename: './test_src/feature1/index.ts',
options: [['~/feature1', 'feature1']],
},
{
path: '@node/module',
filename: './test_src/feature1/slice1/inner1/index.ts',
Expand Down
33 changes: 18 additions & 15 deletions packages/eslint-plugin/src/shortestImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class RuleChecker {
relativePath,
aliasPaths,
baseUrlPaths,
avoidRelativeParents: context.options[0] || [],
preferredAliasPaths: context.options[0] || [],
});

if (preferredPath === importPath) return;
Expand Down Expand Up @@ -270,31 +270,34 @@ class RuleChecker {
relativePath,
aliasPaths,
baseUrlPaths,
avoidRelativeParents,
preferredAliasPaths,
}: {
resolvedFilePath: string;
relativePath: string;
aliasPaths: string[];
baseUrlPaths: string[];
avoidRelativeParents: string[];
preferredAliasPaths: string[];
}) {
if (!aliasPaths.length && !baseUrlPaths.length) return relativePath;
const parentSlugs = relativePath.split('/').filter((s) => s === '..');
const shouldAvoidRelative =
this.relativeGoesThroughBaseUrl(relativePath, resolvedFilePath) ||
[...aliasPaths, ...baseUrlPaths].some((aliasPath) => {
if (!avoidRelativeParents.length) return false;
const relativeRoot = aliasPath
.split('/')
.slice(0, -1 * parentSlugs.length)
.join('/');
return avoidRelativeParents.includes(relativeRoot);
if (!preferredAliasPaths.length) return false;
return preferredAliasPaths.some((alias) => aliasPath.startsWith(alias));
});
const allPathsWithLength = (aliasPaths.length ? aliasPaths : baseUrlPaths)
.map((aliasPath) => ({
aliasPath,
length: aliasPath.split('/').length,
}))
.map((aliasPath) => {
const parts = aliasPath.split('/');
if (parts[0].match(/^[^a-z0-9]$/i))
return {
aliasPath,
length: parts.length - 1,
};
return {
aliasPath,
length: parts.length,
};
})
.sort((a, b) => a.length - b.length);

const shortestAliasPath = allPathsWithLength[0];
Expand Down Expand Up @@ -413,7 +416,7 @@ export const shortestImport: TSESLint.RuleModule<
type: 'problem',
docs: {
description:
'Enforce the consistent use of preferred import paths. A list of alias paths to prefer over relative `../` paths can also be provided',
'Enforce the consistent use of preferred import paths. A list of alias paths to prefer over relative paths can also be provided',
recommended: 'stylistic',
},
fixable: 'code',
Expand Down

0 comments on commit 5d771a3

Please sign in to comment.