From 6351112a922fa8ecd8ef262964916e7f523507ac Mon Sep 17 00:00:00 2001 From: Sebastian Landwehr Date: Sat, 17 Aug 2024 16:21:00 +0000 Subject: [PATCH] fix: nested aliases --- package.json | 3 ++- src/rules/prefer-alias.js | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 32da86f..3c44a64 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/rules/prefer-alias.js b/src/rules/prefer-alias.js index 991031e..cb1435f 100644 --- a/src/rules/prefer-alias.js +++ b/src/rules/prefer-alias.js @@ -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); @@ -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 {