-
-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import/no-restricted-paths
doesn't support path aliases
#317
Comments
|
How? As I mentioned here there is no documentation for how to implement in and ESLint flat config (which seems to be what the OP is using). |
tdlr: The plugin respects the path aliases in path option of tsconfig. This shows how to reference the resolver so we can pass options to it, I tried both, using import to import the entire resolver module and also by just referencing it via by using its name without importing it as shown in the docs: Import method: import * as tsResolver from 'eslint-import-resolver-typescript'
import eslintPluginImportX from 'eslint-plugin-import-x'
const importXTypeScript = eslintPluginImportX.flatConfigs.typescript
importXTypeScript.settings['import-x/resolver'] = {
name: 'tsResolver',
options: {
alias: [
{ name: '/', path: path.resolve(import.meta.dirname, 'public') },
{ name: '@components', path: path.resolve(import.meta.dirname, 'src/ui') },
],
},
resolver: tsResolver,
} Name method: import eslintPluginImportX from 'eslint-plugin-import-x'
const importXTypeScript = eslintPluginImportX.flatConfigs.typescript
importXTypeScript.settings['import-x/resolver'].typescript = {
alias: [
{ name: '/', path: path.resolve(import.meta.dirname, 'public') },
{ name: '@components', path: path.resolve(import.meta.dirname, 'src/ui') },
],
} Since enhanced-resolve options are supported I tried passing the alias options, no errors but it did not work. Then I saw that it respects the path option in tsconfig.json {
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
"paths": {
"/*": ["./public/*"],
"@component/*": ["./src/ui/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
} Now all the imports and stuff work as expected. |
@xpz24 thank you this was very helpful. I have realised that to get One of the key issues you helped me solve was baseUrl in the tsconfig. I was getting an error from ESLint referencing the baseUrl being empty but it didn't mention that it was coming from tsconfig... I thought it was a missing setting in the ESLint config. Here is basically what I ended up with:
|
This is what I have thus far:
No idea how to add |
That is super weird indeed, since I need to enable it explicitly in eslint.config.mjs for it work. I have installed it as a dev dependency. baseUrl
But if I do not use baseUrl, VSCode path suggestions do not work with aliases. These aliases are working perfectly now: "baseUrl": ".",
"paths": {
"/*": ["public/*"],
"@utils/*": ["src/scripts/utils/*"],
"@root/*": ["./*"]
},
"rootDir": ".", @melroy89 /* eslint-disable import-x/no-named-as-default-member */
import eslint from '@eslint/js'
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginImportX from 'eslint-plugin-import-x'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'
import globals from 'globals'
// import nodePlugin from 'eslint-plugin-n' // for node applications
import tseslint from 'typescript-eslint'
// typeScript (Ensure eslint applies only to typescript files)
const strictTypeChecked = tseslint.configs.strictTypeChecked.map((ruleSet) =>
ruleSet.files === undefined
? { ...ruleSet, files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'] }
: ruleSet,
)
const stylisticTypeChecked = tseslint.configs.stylisticTypeChecked.map((ruleSet) =>
ruleSet.files === undefined
? { ...ruleSet, files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'] }
: ruleSet,
)
// importX plugin
const importXRecommended = eslintPluginImportX.flatConfigs.recommended
const importXTypeScript = eslintPluginImportX.flatConfigs.typescript
importXRecommended.languageOptions.ecmaVersion = 'latest' // Probably not necessary
const baseConfig = tseslint.config(
eslint.configs.recommended,
...strictTypeChecked,
...stylisticTypeChecked,
importXRecommended,
importXTypeScript,
{
languageOptions: {
globals: {
...globals.builtin,
// ...globals.nodeBuiltin,
...globals.browser,
// ...globals.node,
},
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
// nodePlugin.configs['flat/recommended'],
eslintPluginUnicorn.configs['flat/recommended'],
eslintConfigPrettier,
)
// const eslintMjsRules = {
// files: ['eslint.config.mjs'],
// rules: {
// 'n/no-unpublished-import': [
// 'error',
// {
// allowModules: [
// '@eslint/js',
// 'eslint-config-prettier',
// 'typescript-eslint',
// 'eslint-plugin-n',
// 'tailwindcss',
// ],
// },
// ],
// },
// }
export default [...baseConfig]
|
I have three path aliases setup in my
vite.config.ts
andtsconfig.json
:~
=>src/
ui
=>src/components/*
utils
=>src/lib/index.ts
Files in
src/components/*
andsrc/lib/*
must not import stuff from~
and~/**
. I've tried usingeslint-plugin-import
andeslint-import-resolver-typescript
but the former expects the real path, not the aliases and that's not the behaviour I want.Here's my config (v9):
The text was updated successfully, but these errors were encountered: