Skip to content

fix: nested aliases #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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,

Check failure on line 7 in src/rules/prefer-alias.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest)

'mapValues' is defined but never used

Check failure on line 7 in src/rules/prefer-alias.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'mapValues' is defined but never used

Check failure on line 7 in src/rules/prefer-alias.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'mapValues' is defined but never used
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 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
Loading