From e49f5bbacbd05ebf183d9a718bb24937bc7b2385 Mon Sep 17 00:00:00 2001 From: Sam Tsai Date: Thu, 24 Oct 2024 13:51:55 -0400 Subject: [PATCH 1/3] fix: add missing schema so eslint would accept options --- src/rules/alias.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/rules/alias.ts b/src/rules/alias.ts index f075279..e610a39 100644 --- a/src/rules/alias.ts +++ b/src/rules/alias.ts @@ -72,6 +72,21 @@ function findAlias( const rule: Rule.RuleModule = { meta: { fixable: 'code', + schema: { + type: 'array', + minItems: 0, + maxItems: 1, + items: [ + { + type: 'object', + properties: { + configFilePath: { type: 'string' }, + ignoredPaths: { type: 'array', items: { type: 'string' } }, + }, + additionalProperties: false, + }, + ], + }, }, create(context) { const baseDir = findDirWithFile('package.json'); From b0bbb566d8643ea5c3b310304f2330886847276a Mon Sep 17 00:00:00 2001 From: Sam Tsai Date: Thu, 24 Oct 2024 15:33:38 -0400 Subject: [PATCH 2/3] fix: support alias paths that are start with "./" --- src/rules/alias.test.ts | 7 +++++++ src/rules/alias.ts | 3 +-- tsconfig.json | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/rules/alias.test.ts b/src/rules/alias.test.ts index 0f1eb26..3393ae4 100644 --- a/src/rules/alias.test.ts +++ b/src/rules/alias.test.ts @@ -57,6 +57,13 @@ tester.run('paths-alias', rule, { errors: ['Update import to @foo/x/y/z'], output: `import z from '@foo/x/y/z';`, }, + { + name: 'relative named import from alias must be fixed', + filename: path.resolve('./src/nested/index.ts'), + code: `import { x } from '../qux/x/y/z';`, + errors: ['Update import to @qux/x/y/z'], + output: `import { x } from '@qux/x/y/z';`, + }, { name: 'absolute import from alias must be fixed', filename: path.resolve('./src/index.ts'), diff --git a/src/rules/alias.ts b/src/rules/alias.ts index e610a39..315b3c6 100644 --- a/src/rules/alias.ts +++ b/src/rules/alias.ts @@ -49,8 +49,7 @@ function findAlias( // Remove basedir and slash in start const slicedImportPath = importPath .slice(baseDir.length + 1) - .slice(path.dirname(matchedPath).length + 1); - + .slice(path.dirname(matchedPath.replace(/^\.\//, '')).length + 1); // Remove asterisk from end of alias const replacedPathSegments = path .join(path.dirname(alias), slicedImportPath) diff --git a/tsconfig.json b/tsconfig.json index 9d45dab..f2078f9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,8 @@ "baseUrl": ".", "paths": { "@foo/*": ["src/foo/*"], - "@bar/*": ["src/bar/*"] + "@bar/*": ["src/bar/*"], + "@qux/*": ["./src/qux/*"] } }, "include": ["src/*.ts", "src/*.tsx", "src/**/*.ts", "src/**/*.tsx"] From acca85fbe0e30fa26846403c285c9e9b6f2ae3c8 Mon Sep 17 00:00:00 2001 From: Sam Tsai Date: Thu, 24 Oct 2024 15:44:41 -0400 Subject: [PATCH 3/3] fix: use normalize instead of replace --- src/rules/alias.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/alias.ts b/src/rules/alias.ts index 315b3c6..2f29a04 100644 --- a/src/rules/alias.ts +++ b/src/rules/alias.ts @@ -49,7 +49,7 @@ function findAlias( // Remove basedir and slash in start const slicedImportPath = importPath .slice(baseDir.length + 1) - .slice(path.dirname(matchedPath.replace(/^\.\//, '')).length + 1); + .slice(path.dirname(path.normalize(matchedPath)).length + 1); // Remove asterisk from end of alias const replacedPathSegments = path .join(path.dirname(alias), slicedImportPath)