ESLint import/extensions rule doesn't catch TypeScript path aliases
## Issue Description
The `import/extensions` ESLint rule successfully catches file extensions in relative imports but fails to detect them in TypeScript path aliases.
## Current Behavior
✅ **Works**: Relative imports
```typescript
import { foo } from './bar.js'; // ❌ ESLint error: "Unexpected use of file extension"
```
❌ **Doesn't work**: Path aliases
```typescript
import { Agent } from '~/agents/agent.js'; // ⚠️ No ESLint error (should error)
```
## Expected Behavior
Both relative imports and path aliases should be caught by the `import/extensions` rule when configured with `'never'`.
## Configuration
ESLint configuration in `eslint.config.js`:
```javascript
{
plugins: {
'import': importPlugin,
},
settings: {
'import/resolver': {
typescript: {
project: './tsconfig.json',
alwaysTryTypes: true
}
}
},
rules: {
'import/extensions': ['error', 'ignorePackages', {
'js': 'never',
'mjs': 'never',
'jsx': 'never',
'ts': 'never',
'tsx': 'never'
}],
}
}
```
TypeScript path mapping in `tsconfig.json`:
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
}
}
```
## Attempted Solutions
1. **eslint-plugin-import-x**: Same limitation - doesn't catch path aliases
2. **eslint-plugin-n**: `n/file-extension-in-import` rule has same limitation
3. **TypeScript resolver configuration**: Tried various `eslint-import-resolver-typescript` options
## Root Cause
This appears to be a fundamental limitation where ESLint import rules check literal import path strings rather than resolved paths, so TypeScript path mappings are not processed by the extension rules.
## Impact
- Manual oversight required to catch `.js` extensions in path aliases
- Inconsistent enforcement of import style across the codebase
- Risk of accidentally including extensions in path aliases
## Workaround
Currently relying on manual review and considering a custom ESLint rule or pre-commit script to catch this specific case.
## Environment
- ESLint: 9.29.0
- eslint-plugin-import: 2.31.0
- eslint-import-resolver-typescript: 4.4.4
- TypeScript: 5.8.3
- Node.js: 24.1.0
## Related
This is a known limitation documented in various GitHub issues for eslint-plugin-import, but worth tracking for our codebase to ensure we don't accidentally remove the partial protection we do have for relative imports.
0 条评论