un-ts/eslint-plugin-import-x

Support eslint flat config

Opened this issue ยท 7 comments

Likely depends on import-js#2556 but it's good to track nonetheless.

Ref: eslint/eslint#18093

eslint-plugin-i is now eslint-plugin-import-x.

I'm focusing on TypeScript migration first, see also #41.

After that, I'll start working on new features like supporting flat config.

Just to mention, some rules will break when using flat config. See this comment

In brief, eslint will fail to use eslint.config.js as config file after enabling import/no-unused-modules with unusedExports: true

Related issues:

Interesting, we are using eslint-plugin-import-x now with the Flat Config:

This seems to be working for us so far:

Simplified version of the code:

import eslintImportX from 'eslint-plugin-import-x';

/** @type {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigArray} */
const configArray = [
  {
    plugins: {
     'import-x': eslintImportX,
    },
    settings: {
      'import-x/parsers': {
        '@typescript-eslint/parser': ['.ts', '.tsx'],
      },
      'import-x/resolver': {
        // Load <rootdir>/tsconfig.json
        typescript: true,
        node: true,
      },
    },
    rules: {
      // Error on imports that don't match the underlying file system
      // https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/no-unresolved.md
      'import-x/no-unresolved': 'error',
  },
];

@karlhorky I am trying out this plugin with the flat config. I copy pasted the config from your post, but I get these errors:

image

(I enabled more rules than simply import-x/no-unresolved.)

Any idea on how to fix?

n0099 commented
module.exports = {
    extends: [
        'plugin:import-x/recommended',
        'plugin:import-x/typescript',
    ],
}

can only be translate into

export default [
    ...compat.config(pluginImportX.configs.recommended), // https://github.com/un-ts/eslint-plugin-import-x/issues/29#issuecomment-2148843214
    pluginImportX.configs.typescript, // no need of wrapping in compat.config() since there's no pluginImportX.configs.typescript.plugins
];

since


is still passing string as plugin.

onx2 commented

@n0099 where is compat coming from?

Is there an example of a simple flat config for eslint v9 that uses this plugin?

My super simple set up works for everything but the import sorting.

import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginImportX from "eslint-plugin-import-x";

export default [
  {
    languageOptions: { globals: globals.browser },
    ignores: ["./dist/"],
  },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  pluginImportX.configs.typescript,
];
n0099 commented

@n0099 where is compat coming from?

@eslint/eslintrc ota-meshi/typescript-eslint-parser-for-extra-files#95 (comment)

import { FlatCompat } from '@eslint/eslintrc';

My super simple set up works for everything but the import sorting.

If you only want to extends from typescript rules, the plugin is not defined in the rule:

export = {
settings: {
'import-x/extensions': allExtensions,
'import-x/external-module-folders': ['node_modules', 'node_modules/@types'],
'import-x/parsers': {
'@typescript-eslint/parser': [...typeScriptExtensions, '.cts', '.mts'],
},
'import-x/resolver': {
node: {
extensions: allExtensions,
},
},
},
rules: {
// analysis/correctness
// TypeScript compilation already ensures that named imports exist in the referenced module
'import-x/named': 'off',
},
} satisfies PluginConfig

so manually add plugin define to let eslint know where to find defines for rule with prefix import-x/

+  { plugins: { 'import-x': pluginImportX } },
  pluginImportX.configs.typescript,

or just also extending recommended rules:

+  ...compat.config(pluginImportX.configs.recommended),
  pluginImportX.configs.typescript,