@dword-design/eslint-plugin-import-alias 8.1.7 → 8.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,7 +3,7 @@ declare module 'babel-plugin-module-resolver' {
|
|
|
3
3
|
sourcePath: string,
|
|
4
4
|
currentFile: string,
|
|
5
5
|
options: { alias?: Record<string, string>; cwd?: string },
|
|
6
|
-
): string;
|
|
6
|
+
): string | null;
|
|
7
7
|
}
|
|
8
8
|
declare module '@babel/core' {
|
|
9
9
|
export type BabelPlugin = { key: string; options?: Record<string, unknown> };
|
|
@@ -3,7 +3,7 @@ import { ESLintUtils } from '@typescript-eslint/utils';
|
|
|
3
3
|
export interface BabelPluginModuleResolverOptions {
|
|
4
4
|
alias?: Record<string, string>;
|
|
5
5
|
cwd?: string;
|
|
6
|
-
resolvePath?: (sourcePath: string, currentFile: string, options: Pick<BabelPluginModuleResolverOptions, 'alias' | 'cwd'>) => string;
|
|
6
|
+
resolvePath?: (sourcePath: string, currentFile: string, options: Pick<BabelPluginModuleResolverOptions, 'alias' | 'cwd'>) => string | null;
|
|
7
7
|
}
|
|
8
8
|
interface AliasInfo {
|
|
9
9
|
path: string;
|
|
@@ -15,7 +15,7 @@ export interface Options {
|
|
|
15
15
|
aliasForSubpaths: boolean;
|
|
16
16
|
shouldReadTsConfig: boolean;
|
|
17
17
|
shouldReadBabelConfig: boolean;
|
|
18
|
-
resolvePath: (sourcePath: string, currentFile: string, options: Pick<BabelPluginModuleResolverOptions, 'alias' | 'cwd'>) => string;
|
|
18
|
+
resolvePath: (sourcePath: string, currentFile: string, options: Pick<BabelPluginModuleResolverOptions, 'alias' | 'cwd'>) => string | null;
|
|
19
19
|
}
|
|
20
20
|
type BabelOptions = Exclude<Parameters<typeof loadOptions>[0], undefined>;
|
|
21
21
|
export type OptionsInput = Omit<Partial<Options>, 'alias'> & {
|
|
@@ -5,6 +5,7 @@ import { ESLintUtils } from "@typescript-eslint/utils";
|
|
|
5
5
|
import { resolvePath as defaultResolvePath } from "babel-plugin-module-resolver";
|
|
6
6
|
import { mapValues, omit, orderBy, pick } from "lodash-es";
|
|
7
7
|
import micromatch from "micromatch";
|
|
8
|
+
import isParentImport from "./is-parent-import/index.js";
|
|
8
9
|
const ts = await import("typescript").then(module => module.default).catch(() => null);
|
|
9
10
|
const loadTsConfigPathsFromFile = (configPath, cwd, visitedConfigs = /* @__PURE__ */new Set()) => {
|
|
10
11
|
if (!ts || visitedConfigs.has(configPath)) {
|
|
@@ -72,7 +73,6 @@ const loadTsConfigPaths = (currentFile, cwd) => {
|
|
|
72
73
|
return loadTsConfigPathsFromFile(configPath, cwd);
|
|
73
74
|
};
|
|
74
75
|
const createRule = ESLintUtils.RuleCreator(() => "");
|
|
75
|
-
const isParentImport = path => /^(\.\/)?\.\.\//.test(path);
|
|
76
76
|
const findMatchingAlias = (sourcePath, currentFilename, options, {
|
|
77
77
|
cwd = "."
|
|
78
78
|
} = {}) => {
|
|
@@ -80,12 +80,16 @@ const findMatchingAlias = (sourcePath, currentFilename, options, {
|
|
|
80
80
|
const matches = Object.entries(options.alias).flatMap(([aliasName, aliasInfos]) => aliasInfos.map(info => [aliasName, info])).filter(([, info]) => info.includePatterns.length > 0 ? micromatch.isMatch(pathLib.relative(info.configDir, currentFilename), info.includePatterns, {
|
|
81
81
|
cwd: info.configDir
|
|
82
82
|
}) : true).map(([aliasName, info]) => {
|
|
83
|
-
const
|
|
83
|
+
const resolvedRelativePath = options.resolvePath(`${aliasName}/`, currentFilename, {
|
|
84
84
|
alias: {
|
|
85
85
|
[aliasName]: info.path
|
|
86
86
|
},
|
|
87
87
|
cwd
|
|
88
|
-
})
|
|
88
|
+
});
|
|
89
|
+
if (!resolvedRelativePath) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const path = pathLib.resolve(pathLib.dirname(currentFilename), resolvedRelativePath);
|
|
89
93
|
if (absoluteSourcePath.startsWith(path)) {
|
|
90
94
|
return {
|
|
91
95
|
name: aliasName,
|
|
@@ -158,7 +162,8 @@ export default createRule({
|
|
|
158
162
|
return;
|
|
159
163
|
}
|
|
160
164
|
const absoluteImportPath = pathLib.resolve(folder, sourcePath);
|
|
161
|
-
const
|
|
165
|
+
const relativePath = pathLib.relative(matchingAlias.path, absoluteImportPath).replaceAll("\\", "/");
|
|
166
|
+
const rewrittenImport = [matchingAlias.name, ...(relativePath ? [relativePath] : [])].join("/");
|
|
162
167
|
return context.report({
|
|
163
168
|
data: {
|
|
164
169
|
rewrittenImport,
|
|
@@ -173,7 +178,7 @@ export default createRule({
|
|
|
173
178
|
alias: filteredAliases,
|
|
174
179
|
cwd: options.cwd
|
|
175
180
|
});
|
|
176
|
-
if (!isParentImport(importWithoutAlias) && hasAlias && !options.aliasForSubpaths) {
|
|
181
|
+
if (importWithoutAlias && !isParentImport(importWithoutAlias) && hasAlias && !options.aliasForSubpaths) {
|
|
177
182
|
return context.report({
|
|
178
183
|
data: {
|
|
179
184
|
rewrittenImport: importWithoutAlias,
|