eslint-plugin-import-alias
eslint-plugin-import-alias copied to clipboard
Doesn't work for imports in the same directory
When importing from the same directory, this rule doesn't get applied. It considers this valid:
import { Foo } from './foo'
Expectation:
import { Foo } from '@/path/to/foo'
All my valid imports have the error: Unexpected subpath import via alias
Real world example:
Unexpected subpath import via alias '@lib/constants/a2.constants'. Use './constants/a2.constants' instead
Configuration in .eslintrc.js:
'@dword-design/import-alias/prefer-alias': [
'error',
{
'alias': {
'@lib': './src/'
}
}
],
Is there a way to get this to work with . and not just ..?
I also would prefer to use this and standardize on always using aliases, maybe would be a useful config option
👋🏽 Hello,
I'm working with a TypeScript project that doesn't use Babel and I've run into a problem with the @dword-design/import-alias/prefer-alias rule. Alongside converting aliased imports like @/xyz to ./xyz in the same directory, it's also not correctly recognising imports from the same directory.
Is there a configuration option or workaround available to address these issues, particularly for maintaining aliased paths and ensuring correct import recognition in projects without Babel?
Thanks in advance for any assistance or insights!
Update 1: I did a little bit of digging and found out that adding a condition alongside hasAlias (link to code) property can solve this issue of converting aliased imports like @/xyz to ./xyz.
if (!(importWithoutAlias |> isParentImport) && hasAlias && !sourcePath.startsWith('@/')) {
// existing logic
}
Is it possible to add some flag or configuration option like @roryabraham asked to accommodate this change? This is a rough solution I developed after about 10 minutes of investigation. Please feel free to correct or improve it.
Shipped.