import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.5k stars 1.57k forks source link

Support eslint v9 #2948

Closed DamienCassou closed 4 days ago

DamienCassou commented 9 months ago

eslint 9 is being prepared and comes with a lot of changes impacting users and plugin developers: see https://eslint.org/blog/2023/12/eslint-v9.0.0-alpha.0-released/ for example.

What is the status of eslint v9 support for eslint-plugin-import?

ljharb commented 9 months ago

Since we don’t even support flat config yet (#2873), and eslint doesn’t provide any replacement for FileEnumerator, I’d say it’ll probably be awhile before we support eslint 9.

JounQin commented 9 months ago

At least we can support using eslintrc on eslint v9 currently.

TODO: compatible with some internal API migration like context vs SourceCode.

ljharb commented 9 months ago

I'd definitely love to add some tests on eslint 9, using the env var that restores eslintrc, so we can get ahead of any needed changes.

marmitar commented 6 months ago

I made a patch for eslint-plugin-import@2.29.1 (and eslint-plugin-i@2.29.1) that works with pnpm and ESLint v9.0.0-beta.2: https://gist.github.com/TiagodePAlves/8c9b1ed4062a484022f5ebf7ba53a5d5

ljharb commented 6 months ago

@TiagodePAlves thanks, that's helpful! once we have tests running on eslint 9, it shouldn't be too difficult to adapt that patch into something that works on both v9 and older versions.

G-Rath commented 6 months ago

@ljharb fwiw I did a very naive find-and-replace of the context utils we're using in eslint-plugin-jest and it looks like that's all thats needed? specifically also seems that only getSourceCode and getScope are the only two removed context methods being used (or at least out of the rules eslint-plugin-jest is using).

Would you like me to attempt a draft PR? I don't know if I'll have the bandwidth to setup a whole test suite but I figure I can contribute enough to kick start the work 🙂

ljharb commented 6 months ago

@G-Rath that might be the case! The blocker really is setting up tests, unfortunately - if we can get tests working on eslint 9, the rest is something I can probably find time for sooner than later :-)

ryanblock commented 5 months ago

@ljharb it may be something that's classified under "it'll take more time to explain than it would take for me [@ljharb] to do it" but if you can scope the test suite work here I or others may be able to help pitch in! 💕

ljharb commented 5 months ago

@ryanblock basically:

  1. add eslint 9 to the test matrix
  2. get tests passing on eslint 9

2996 should cover this eventually. If anyone has contributions, please do NOT open a new PR, and instead, comment a link to the branch or commit on the PR, and the OP or myself can pull them in.

karlhorky commented 4 months ago

Copying my comment in the PR here for visibility:

Workaround (switch to eslint-plugin-import-x)

In case anyone wants to upgrade to ESLint v9 with Flat Config now, an alternative is to switch to the eslint-plugin-import-x fork:

  1. Install eslint-plugin-import-x
  2. Install @typescript-eslint/parser for TypeScript support
-import eslintImport from 'eslint-plugin-import';
+import eslintImportX from 'eslint-plugin-import-x';

/** @type {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigArray} */
const configArray = [
  {
    plugins: {
-     import: eslintImport,
+     '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/import-js/eslint-plugin-import/blob/master/docs/rules/no-unresolved.md
-     'import/no-unresolved': 'error',
+     // https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/no-unresolved.md
+     'import-x/no-unresolved': 'error',
  },
];
wojtekmaj commented 4 months ago

Copying my comment in the PR here for visibility:

Workaround (switch to eslint-plugin-import-x)

In case anyone wants to upgrade to ESLint v9 with Flat Config now, an alternative is to switch to the eslint-plugin-import-x fork:

  1. Install eslint-plugin-import-x
  2. Install @typescript-eslint/parser for TypeScript support
-import eslintImport from 'eslint-plugin-import';
+import eslintImportX from 'eslint-plugin-import-x';

/** @type {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigArray} */
const configArray = [
  {
    plugins: {
-     import: eslintImport,
+     '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/import-js/eslint-plugin-import/blob/master/docs/rules/no-unresolved.md
-     'import/no-unresolved': 'error',
+     // https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/no-unresolved.md
+     'import-x/no-unresolved': 'error',
  },
];

Why would a genuinely helpful post, outlining alternatives, thus also reducing the amount of people blocked by this, and reducing the pressure on the maintainers of the original project, be marked as spam?

ryanblock commented 4 months ago

@wojtekmaj well, I'm not a contributor here, but I'd imagine because promoting a fork in an active PR in an actively maintained repo seems entirely unnecessary, and perhaps a bit ridiculous. Everyone appears aligned with making this happen.

ljharb commented 4 months ago

Indeed, posting about an alternative project is spam, in a general sense.

rotu commented 4 months ago

Indeed, posting about an alternative project is spam, in a general sense.

Posting about an irrelevant project is spam. But this is well-intentioned community effort to improve the project, not commercial self-promotion. I’m very familiar with the feeling of frustration “I have put in hard work to help, but it doesn’t help anyone unless/until project maintainers merge the PR”.

I wish I knew the solution.

JoseLion commented 4 months ago

In case anyone finds it helpful, I got this plugin working on v9 with the help of a couple of extra dependencies from ESLint designed to provide backward compatibility: @eslint/eslintrc and @eslint/compat.

Fair warning: A lot more code needs to be added! But it works, so.... 🤷🏼‍♂️

import { fixupPluginRules } from "@eslint/compat";
import { FlatCompat } from "@eslint/eslintrc";
import eslintJs from "@eslint/js";
import eslintTs from "typescript-eslint";

const project = "./tsconfig.json";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: eslintJs.configs.recommended,
});

/**
 * @param {string} name the pugin name
 * @param {string} alias the plugin alias
 * @returns {import("eslint").ESLint.Plugin}
 */
function legacyPlugin(name, alias = name) {
  const plugin = compat.plugins(name)[0]?.plugins?.[alias];

  if (!plugin) {
    throw new Error(`Unable to resolve plugin ${name} and/or alias ${alias}`);
  }

  return fixupPluginRules(plugin);
}

export default eslintTs.config(
  eslintJs.configs.recommended,
  ...eslintTs.configs.recommendedTypeChecked,
  ...compat.extends("plugin:import/typescript"),
  {
    languageOptions: {
      parserOptions: {
        project,
        tsconfigRootDir: import.meta.dirname,
      },
    },
    settings: {
      "import/resolver": {
        typescript: {
          alwaysTryTypes: true,
          project,
        }
      },
    },
    plugins: [
      import: legacyPlugin("eslint-plugin-import", "import"),
      // ...rest of plugins
    ],
    rules: {
      // your rules here....
    },
  },
);

You can avoid the legacyPlugin(..) abstraction if you like, but it works with other plugins as well, so it's a good idea to keep it around while the ecosystem moves to v9.

IMHO, the ESLint plugin ecosystem is getting harder and harder to maintain, and things get worse during major version upgrades. For instance, I usually find myself adding yet another plugin just for a single rule (e.g. eslint-plugin-deprecation). I think trying to consolidate more rules into ESLint and typescript-eslint can help with this, but I might be missing something, so those are just my two cents 🙂

cha0s commented 3 months ago

Forks in open source are not spam. If you don't like forks, consider spending more fixing your project than hiding comments that actually help your users.

P.S. I swear, seeing a comment from that guy virtually guarantees the issue or project itself is cooked. Cannot wait until this supply chain troll is gone.

ljharb commented 3 months ago

@cha0s the forks aren't. the comments about them are.

Bessonov commented 3 months ago

@ljharb, even though you are the author, committer, and maintainer of this and many other repositories, your absolute comments and actions aren't helpful at all and they poison these projects. I have observed your destructive behavior in many projects already. Maybe you are also a great developer. However, you aren't a god. It looks like your fame has overwhelmed you. You behave like a teenager who rebels just for the sake of it, not for any real reasons. And you are alone:

image

Please think about that and how you interact with the world, in a general sense.

Regarding the post about the fork: This issue was created half a year ago and is still unresolved. And this is quite OK. No offense - it's open source, everyone can contribute or create forks.

Does the fork violate the rights of the project? Or your personal rights? Or do you feel any damage from it? The whole purpose of the fork is to resolve this issue and it is good to know about it for everyone. And, actually, there is a demand:

image

Maybe it would be better to make a PR in this repo. Maybe instead of defending the broken state, you could collaborate to make a PR happen. However, looking at your behavior, I am not sure I would ever contribute to a project where you are a maintainer.

Of course, you can mark or delete comments about forks, contributions, discussions, and also my comment. However, it wouldn't change who you are and how the world will remember you. It's your life, it's your choice. Some people are pretty fine being AHs their whole life.

kachkaev commented 4 days ago

Amazing news on merging #2996 🎉

It’d be great to see a new release by the end of the week, because v8 reaches EOL on Saturday. At the time of writing, the latest release is v2.30.0 (2024-09-03).

ljharb commented 3 days ago

I'm traveling right now, but I should be able to get a release out before the end of the week.