@dword-design/eslint-plugin-import-alias 8.1.6 → 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> };
@@ -0,0 +1,2 @@
1
+ declare const _default: (path: string) => boolean;
2
+ export default _default;
@@ -0,0 +1,7 @@
1
+ export default path => {
2
+ const segments = path.split("/");
3
+ if (segments[0] === ".") {
4
+ segments.shift();
5
+ }
6
+ return segments[0] === "..";
7
+ };
@@ -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 path = pathLib.resolve(pathLib.dirname(currentFilename), options.resolvePath(`${aliasName}/`, currentFilename, {
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 rewrittenImport = `${matchingAlias.name}/${pathLib.relative(matchingAlias.path, absoluteImportPath).replaceAll("\\", "/")}`;
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dword-design/eslint-plugin-import-alias",
3
- "version": "8.1.6",
3
+ "version": "8.1.8",
4
4
  "description": "An ESLint plugin that enforces the use of import aliases. Also supports autofixing.",
5
5
  "keywords": [
6
6
  "alias",
@@ -80,7 +80,7 @@
80
80
  "optional": true
81
81
  }
82
82
  },
83
- "packageManager": "pnpm@10.11.1+sha512.e519b9f7639869dc8d5c3c5dfef73b3f091094b0a006d7317353c72b124e80e1afd429732e28705ad6bfa1ee879c1fce46c128ccebd3192101f43dd67c667912",
83
+ "packageManager": "pnpm@10.32.0+sha512.9b2634bb3fed5601c33633f2d92593f506270a3963b8c51d2b2d6a828da615ce4e9deebef9614ccebbc13ac8d3c0f9c9ccceb583c69c8578436fa477dbb20d70",
84
84
  "engines": {
85
85
  "node": ">=22"
86
86
  },