Skip to content

Commit 006bdc9

Browse files
committed
fix: regression when an alias is directly imported from a sibling or a parent
1 parent f0ba5fa commit 006bdc9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/rules/prefer-alias.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ export default {
118118
})
119119
}
120120

121+
const isDirectAlias =
122+
options.alias |> keys |> some(alias => sourcePath === alias)
123+
121124
const shouldUnalias =
122125
hasAlias &&
126+
!isDirectAlias &&
123127
(((importWithoutAlias |> isSiblingImport) && !options.forSiblings) ||
124128
((importWithoutAlias |> isSubpathImport) && !options.forSubpaths))
125129
if (shouldUnalias) {

src/rules/prefer-alias.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,32 @@ export default tester(
245245
output: "import foo from '@/foo'",
246246
})
247247
},
248+
'direct import of an alias from a parent': async () => {
249+
await outputFiles({
250+
'.babelrc.json': JSON.stringify({
251+
plugins: [
252+
[
253+
packageName`babel-plugin-module-resolver`,
254+
{ alias: { '@components': './sub/components' } },
255+
],
256+
],
257+
}),
258+
})
259+
expect(lint("import { foo } from '@components'").messages).toEqual([])
260+
},
261+
'direct import of an alias from a sibling': async () => {
262+
await outputFiles({
263+
'.babelrc.json': JSON.stringify({
264+
plugins: [
265+
[
266+
packageName`babel-plugin-module-resolver`,
267+
{ alias: { '@components': './components' } },
268+
],
269+
],
270+
}),
271+
})
272+
expect(lint("import { foo } from '@components'").messages).toEqual([])
273+
},
248274
'direct import of an alias from another one': async () => {
249275
await outputFiles({
250276
'.babelrc.json': JSON.stringify({

0 commit comments

Comments
 (0)