antfu / eslint-config

Anthony's ESLint config preset
https://eslint-config.antfu.me/
MIT License
4.02k stars 443 forks source link

"No cycle" rule isn't working #373

Open arpowers opened 10 months ago

arpowers commented 10 months ago

Describe the bug

One of the handiest eslint rules is the no-cycle rule which detects circular imports (because these can be such a nightmare)

However, the rule doesn't appear to be working inside this library.

Config:

// @ts-check
import antfu from "@antfu/eslint-config";

export default antfu(
  {
    typescript: {
      tsconfigPath: "./tsconfig.json",
      parserOptions: {
        project: "./tsconfig.json",
        extraFileExtensions: [".vue", ".json"],
      },
    },
    rules: {
      eqeqeq: "warn",

      "import/no-cycle": [
        "error",
        {
          maxDepth: 5,
          ignoreExternal: true,
        },
      ],
      "ts/no-floating-promises": "error",
    },
  },
  {
    ignores: [
      "**/.ref/**/*",
      "**/__*",
      "node_modules",
      "dist",
      "**/.factor/**",
      ".pnpmfile.cjs",
    ],
  }
);

Reproduction

https://github.com/arpowers/eslint-testing

System Info

MacOS

Used Package Manager

npm

Validations

arpowers commented 9 months ago

any update on this? @antfu as a work around, if there is a better way to make sure there are no circular imports I'm interested.

antfu commented 9 months ago

I don't know. I am not using that rule and not sure if it's a problem with this config or the plugin - Investigating yourself might get your problem solved faster.

InfiniteXyy commented 3 months ago

Hi, @arpowers

I found that this issue might caused by eslint-plugin-import doesn't support flatConfig, see https://github.com/un-ts/eslint-plugin-import-x/issues/29

But we could use @eslint/eslintrc compact as a workaround 👇

I'm not sure if it's the best workaround, at least it works for me. Also, I don't know how to override the import settings in antfu() composer, looks like import doesn't provide override options like other rule sets (for example react)

import { combine, javascript, renamePluginInConfigs, typescript } from '@antfu/eslint-config';
import { FlatCompat } from '@eslint/eslintrc';
import importPlugin from 'eslint-plugin-import-x';

const compat = new FlatCompat({});

export default combine(
  javascript(),
  typescript(),
  // rename the `import-x` to `import` to keep sync with antfu default naming
  renamePluginInConfigs([...compat.config(importPlugin.configs.recommended), importPlugin.configs.typescript], { 'import-x': 'import' }),
  [{
    rules: {
      'import/no-cycle': 'error',
    },
  }],
);
SukkaW commented 3 months ago

eslint-plugin-import-x doesn't have built-in flat config exports, but almost all rules work with flat config (except no-unused-modules). You can always compose your own flat config just like how @antfu/eslint-config already did. No compat util is needed.

InfiniteXyy commented 3 months ago

Hi @SukkaW

You are right.

Adding these settings into antfu config also makes the import/no-cycle rule work.

{
 settings: {
    'import-x/parsers': {
      '@typescript-eslint/parser': ['.ts'],
    },
    'import-x/resolver': {
      node: {
        extensions: ['.ts'],
      },
    },
  },
}