Skip to content

Commit f0ba5fa

Browse files
committed
fix: handle when an alias is directly imported from another one
1 parent 6f05f08 commit f0ba5fa

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/rules/prefer-alias.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export default {
7575
const hasAlias =
7676
options.alias
7777
|> keys
78-
|> some(alias => sourcePath |> startsWith(`${alias}/`))
78+
|> some(
79+
alias =>
80+
(sourcePath |> startsWith(`${alias}/`)) || sourcePath === alias,
81+
)
7982

8083
const importWithoutAlias = resolvePath(sourcePath, currentFile, options)
8184

@@ -96,10 +99,11 @@ export default {
9699

97100
const absoluteImportPath = P.resolve(folder, sourcePath)
98101

99-
const rewrittenImport = `${matchingAlias.name}/${
100-
P.relative(matchingAlias.path, absoluteImportPath)
101-
|> replace(/\\/g, '/')
102-
}`
102+
const rewrittenImport =
103+
`${matchingAlias.name}/${
104+
P.relative(matchingAlias.path, absoluteImportPath)
105+
|> replace(/\\/g, '/')
106+
}` |> replace(/\/$/, '')
103107

104108
const importType = getImportType(importWithoutAlias)
105109

src/rules/prefer-alias.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ export default tester(
245245
output: "import foo from '@/foo'",
246246
})
247247
},
248+
'direct import of an alias from another one': async () => {
249+
await outputFiles({
250+
'.babelrc.json': JSON.stringify({
251+
plugins: [
252+
[
253+
packageName`babel-plugin-module-resolver`,
254+
{ alias: { '@components': './components', '@hooks': './hooks' } },
255+
],
256+
],
257+
}),
258+
})
259+
expect(
260+
lint("import { foo } from '../hooks'", {
261+
filename: 'components/bar.js',
262+
}),
263+
).toEqual({
264+
messages: ["Unexpected parent import '../hooks'. Use '@hooks' instead"],
265+
output: "import { foo } from '@hooks'",
266+
})
267+
},
248268
external: async () => {
249269
await fs.outputFile(
250270
'.babelrc.json',

0 commit comments

Comments
 (0)