Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"@dword-design/functions": "^6.0.0",
"babel-plugin-module-resolver": "^5.0.0",
"deepmerge": "^4.3.1",
"jiti": "^1.18.2"
"jiti": "^1.18.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"@dword-design/base": "^11.0.7",
Expand Down
39 changes: 28 additions & 11 deletions src/rules/prefer-alias.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { OptionManager } from '@babel/core';
import { find, keys, replace, some, startsWith } from '@dword-design/functions';
import {
compact,
find,
keys,
map,
mapValues,
replace,
some,
startsWith,
} from '@dword-design/functions';
import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver';
import deepmerge from 'deepmerge';
import maxBy from 'lodash/fp/maxBy.js';
import P from 'path';

const isParentImport = path => /^(\.\/)?\.\.\//.test(path);
Expand All @@ -10,18 +20,25 @@ const findMatchingAlias = (sourcePath, currentFile, options) => {
const resolvePath = options.resolvePath || defaultResolvePath;
const absoluteSourcePath = P.resolve(P.dirname(currentFile), sourcePath);

for (const aliasName of options.alias |> keys) {
const path = P.resolve(
P.dirname(currentFile),
resolvePath(`${aliasName}/`, currentFile, options),
);
const alias =
options.alias
|> keys
|> map(aliasName => {
const path = P.resolve(
P.dirname(currentFile),
resolvePath(`${aliasName}/`, currentFile, options),
);

if (absoluteSourcePath |> startsWith(path)) {
return { name: aliasName, path };
}
}
if (absoluteSourcePath |> startsWith(path)) {
return { name: aliasName, path };
}

return null;
})
|> compact
|> maxBy('path');

return undefined;
return alias;
};

export default {
Expand Down