Skip to content

Commit

Permalink
fix: support alias paths that are start with "./"
Browse files Browse the repository at this point in the history
  • Loading branch information
samtsai committed Oct 24, 2024
1 parent e49f5bb commit b0bbb56
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/rules/alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 1 addition & 2 deletions src/rules/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit b0bbb56

Please sign in to comment.