typescript-eslint / typescript-eslint

:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript
https://typescript-eslint.io
Other
15.2k stars 2.72k forks source link

Bug: This method cannot be used with flat config. Add your entries directly into the config array #10063

Closed SolankiYogesh3500 closed 2 weeks ago

SolankiYogesh3500 commented 2 weeks ago

Before You File a Bug Report Please Confirm You Have Done The Following...

Relevant Package

eslint-plugin

Playground Link

No response

Repro Code

import path from 'path'
import {fileURLToPath} from 'url'
import {fixupPluginRules} from '@eslint/compat'
import pluginJs from '@eslint/js'
import airbnb from 'eslint-config-airbnb-base'
import love from 'eslint-config-love'
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
import pluginReact from 'eslint-plugin-react'
import hooks from 'eslint-plugin-react-hooks'
import reactNative from 'eslint-plugin-react-native'
import sortImports from 'eslint-plugin-simple-import-sort'
import globals from 'globals'
import tseslint from 'typescript-eslint'

const rulesDirPlugin = await import('eslint-plugin-rulesdir')
const __filename = fileURLToPath(import.meta.url) // get the resolved path to the file
const __dirname = path.dirname(__filename) // get the name of the directory
rulesDirPlugin.default.RULES_DIR = path.resolve(__dirname, './')
export default tseslint.config(
  {languageOptions: {globals: globals.browser}},
  {
    plugins: {
      'react-native': fixupPluginRules(reactNative),
      'react-hooks': fixupPluginRules(hooks),
      'simple-import-sort': sortImports,
      airbnb: fixupPluginRules(airbnb),

      rulesdir: fixupPluginRules(rulesDirPlugin)
    }
  },
  eslintConfigPrettier,
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  pluginReact.configs.flat.recommended,
  eslintPluginPrettierRecommended,
  ...tseslint.configs.recommendedTypeChecked,
  ...tseslint.configs.stylisticTypeChecked,
  love,

  {
    rules: {
      '@typescript-eslint/no-explicit-any': 'off',
      'react/display-name': 'off',
      '@typescript-eslint/no-magic-numbers': 'off',
      '@typescript-eslint/explicit-function-return': 'off',
      '@typescript-eslint/strict-boolean-expressions': 'off',
      '@typescript-eslint/no-require-imports': 'off',
      '@typescript-eslint/explicit-function-return-type': 'off',
      'no-async-promise-executor': 'off',
      '@typescript-eslint/no-misused-promises': 'off',
      '@typescript-eslint/prefer-nullish-coalescing': 'off',
      'import/no-duplicates': 'off',
      '@typescript-eslint/consistent-type-assertions': 'off',
      '@typescript-eslint/no-floating-promises': 'off',
      '@typescript-eslint/no-unsafe-argument': 'off',
      '@typescript-eslint/no-empty-function': 'off',
      '@typescript-eslint/return-await': 'off',
      '@typescript-eslint/prefer-promise-reject-errors': 'off',
      '@typescript-eslint/max-params': 'off',
      '@typescript-eslint/naming-convention': 'off',
      'no-case-declarations': 'off',
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          args: 'all',
          argsIgnorePattern: '^_',
          caughtErrors: 'all',
          caughtErrorsIgnorePattern: '^_',
          destructuredArrayIgnorePattern: '^_',
          varsIgnorePattern: '^_',
          ignoreRestSiblings: true
        }
      ],
      '@typescript-eslint/no-extraneous-class': 'off',
      'simple-import-sort/imports': 'error',
      'simple-import-sort/exports': 'error',
      'react-hooks/rules-of-hooks': 'error',
      'react-hooks/exhaustive-deps': 'error',
      '@typescript-eslint/prefer-enum-initializers': 'error',
      '@typescript-eslint/no-var-requires': 'off',
      '@typescript-eslint/no-non-null-assertion': 'error',
      '@typescript-eslint/switch-exhaustiveness-check': 'error',
      '@typescript-eslint/consistent-type-definitions': ['error', 'type'],
      '@typescript-eslint/no-floating-promises': 'off',
      '@typescript-eslint/no-import-type-side-effects': 'error',
      '@typescript-eslint/array-type': ['error', {default: 'array-simple'}],
      '@typescript-eslint/naming-convention': [
        'error',
        {
          selector: ['variable', 'property'],
          format: ['camelCase', 'UPPER_CASE', 'PascalCase']
        },
        {
          selector: 'function',
          format: ['camelCase', 'PascalCase']
        },
        {
          selector: ['typeLike', 'enumMember'],
          format: ['PascalCase']
        },
        {
          selector: ['parameter', 'method'],
          format: ['camelCase', 'PascalCase'],
          leadingUnderscore: 'allow'
        }
      ],
      '@typescript-eslint/ban-types': [
        'error',
        {
          types: {
            object: "Use 'Record<string, T>' instead."
          },
          extendDefaults: true
        }
      ],
      '@typescript-eslint/consistent-type-imports': [
        'error',
        {
          prefer: 'type-imports',
          fixStyle: 'separate-type-imports'
        }
      ],
      '@typescript-eslint/consistent-type-exports': [
        'error',
        {
          fixMixedExportsWithInlineTypeSpecifier: false
        }
      ],
      '@typescript-eslint/no-use-before-define': ['error', {functions: false}],

      // ESLint core rules
      'es/no-nullish-coalescing-operators': 'off',
      'es/no-optional-chaining': 'off',

      // Import specific rules
      'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
      'import/no-extraneous-dependencies': 'off',

      // Rulesdir specific rules
      'rulesdir/no-default-props': 'error',
      'rulesdir/prefer-type-fest': 'error',
      'rulesdir/no-multiple-onyx-in-file': 'off',
      'rulesdir/prefer-underscore-method': 'off',
      'rulesdir/prefer-import-module-contents': 'off',

      // React and React Native specific rules
      'react-native-a11y/has-accessibility-hint': ['off'],
      'react/require-default-props': 'off',
      'react/prop-types': 'off',
      'react/jsx-no-constructed-context-values': 'error',
      'react-native-a11y/has-valid-accessibility-descriptors': [
        'error',
        {
          touchables: ['PressableWithoutFeedback', 'PressableWithFeedback']
        }
      ],
      'react-compiler/react-compiler': 'error',

      // Disallow usage of certain functions and imports
      'no-restricted-syntax': [
        'error',
        {
          selector: 'TSEnumDeclaration',
          message: "Please don't declare enums, use union types instead."
        }
      ],
      'no-restricted-properties': [
        'error',
        {
          object: 'Image',
          property: 'getSize',
          message:
            'Usage of Image.getImage is restricted. Please use the `react-native-image-size`.'
        }
      ],

      // Other rules
      curly: 'error',
      'lodash/import-scope': ['error', 'method'],
      'prefer-regex-literals': 'off'
    },
    settings: {
      'import/resolver': {
        typescript: {}
      },
      react: {
        version: 'detect'
      }
    }
  }
)

ESLint Config

import path from 'path'
import {fileURLToPath} from 'url'
import {fixupPluginRules} from '@eslint/compat'
import pluginJs from '@eslint/js'
import airbnb from 'eslint-config-airbnb-base'
import love from 'eslint-config-love'
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
import pluginReact from 'eslint-plugin-react'
import hooks from 'eslint-plugin-react-hooks'
import reactNative from 'eslint-plugin-react-native'
import sortImports from 'eslint-plugin-simple-import-sort'
import globals from 'globals'
import tseslint from 'typescript-eslint'

const rulesDirPlugin = await import('eslint-plugin-rulesdir')
const __filename = fileURLToPath(import.meta.url) // get the resolved path to the file
const __dirname = path.dirname(__filename) // get the name of the directory
rulesDirPlugin.default.RULES_DIR = path.resolve(__dirname, './')
export default tseslint.config(
  {languageOptions: {globals: globals.browser}},
  {
    plugins: {
      'react-native': fixupPluginRules(reactNative),
      'react-hooks': fixupPluginRules(hooks),
      'simple-import-sort': sortImports,
      airbnb: fixupPluginRules(airbnb),

      rulesdir: fixupPluginRules(rulesDirPlugin)
    }
  },
  eslintConfigPrettier,
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  pluginReact.configs.flat.recommended,
  eslintPluginPrettierRecommended,
  ...tseslint.configs.recommendedTypeChecked,
  ...tseslint.configs.stylisticTypeChecked,
  love,

  {
    rules: {
      '@typescript-eslint/no-explicit-any': 'off',
      'react/display-name': 'off',
      '@typescript-eslint/no-magic-numbers': 'off',
      '@typescript-eslint/explicit-function-return': 'off',
      '@typescript-eslint/strict-boolean-expressions': 'off',
      '@typescript-eslint/no-require-imports': 'off',
      '@typescript-eslint/explicit-function-return-type': 'off',
      'no-async-promise-executor': 'off',
      '@typescript-eslint/no-misused-promises': 'off',
      '@typescript-eslint/prefer-nullish-coalescing': 'off',
      'import/no-duplicates': 'off',
      '@typescript-eslint/consistent-type-assertions': 'off',
      '@typescript-eslint/no-floating-promises': 'off',
      '@typescript-eslint/no-unsafe-argument': 'off',
      '@typescript-eslint/no-empty-function': 'off',
      '@typescript-eslint/return-await': 'off',
      '@typescript-eslint/prefer-promise-reject-errors': 'off',
      '@typescript-eslint/max-params': 'off',
      '@typescript-eslint/naming-convention': 'off',
      'no-case-declarations': 'off',
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          args: 'all',
          argsIgnorePattern: '^_',
          caughtErrors: 'all',
          caughtErrorsIgnorePattern: '^_',
          destructuredArrayIgnorePattern: '^_',
          varsIgnorePattern: '^_',
          ignoreRestSiblings: true
        }
      ],
      '@typescript-eslint/no-extraneous-class': 'off',
      'simple-import-sort/imports': 'error',
      'simple-import-sort/exports': 'error',
      'react-hooks/rules-of-hooks': 'error',
      'react-hooks/exhaustive-deps': 'error',
      '@typescript-eslint/prefer-enum-initializers': 'error',
      '@typescript-eslint/no-var-requires': 'off',
      '@typescript-eslint/no-non-null-assertion': 'error',
      '@typescript-eslint/switch-exhaustiveness-check': 'error',
      '@typescript-eslint/consistent-type-definitions': ['error', 'type'],
      '@typescript-eslint/no-floating-promises': 'off',
      '@typescript-eslint/no-import-type-side-effects': 'error',
      '@typescript-eslint/array-type': ['error', {default: 'array-simple'}],
      '@typescript-eslint/naming-convention': [
        'error',
        {
          selector: ['variable', 'property'],
          format: ['camelCase', 'UPPER_CASE', 'PascalCase']
        },
        {
          selector: 'function',
          format: ['camelCase', 'PascalCase']
        },
        {
          selector: ['typeLike', 'enumMember'],
          format: ['PascalCase']
        },
        {
          selector: ['parameter', 'method'],
          format: ['camelCase', 'PascalCase'],
          leadingUnderscore: 'allow'
        }
      ],
      '@typescript-eslint/ban-types': [
        'error',
        {
          types: {
            object: "Use 'Record<string, T>' instead."
          },
          extendDefaults: true
        }
      ],
      '@typescript-eslint/consistent-type-imports': [
        'error',
        {
          prefer: 'type-imports',
          fixStyle: 'separate-type-imports'
        }
      ],
      '@typescript-eslint/consistent-type-exports': [
        'error',
        {
          fixMixedExportsWithInlineTypeSpecifier: false
        }
      ],
      '@typescript-eslint/no-use-before-define': ['error', {functions: false}],

      // ESLint core rules
      'es/no-nullish-coalescing-operators': 'off',
      'es/no-optional-chaining': 'off',

      // Import specific rules
      'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
      'import/no-extraneous-dependencies': 'off',

      // Rulesdir specific rules
      'rulesdir/no-default-props': 'error',
      'rulesdir/prefer-type-fest': 'error',
      'rulesdir/no-multiple-onyx-in-file': 'off',
      'rulesdir/prefer-underscore-method': 'off',
      'rulesdir/prefer-import-module-contents': 'off',

      // React and React Native specific rules
      'react-native-a11y/has-accessibility-hint': ['off'],
      'react/require-default-props': 'off',
      'react/prop-types': 'off',
      'react/jsx-no-constructed-context-values': 'error',
      'react-native-a11y/has-valid-accessibility-descriptors': [
        'error',
        {
          touchables: ['PressableWithoutFeedback', 'PressableWithFeedback']
        }
      ],
      'react-compiler/react-compiler': 'error',

      // Disallow usage of certain functions and imports
      'no-restricted-syntax': [
        'error',
        {
          selector: 'TSEnumDeclaration',
          message: "Please don't declare enums, use union types instead."
        }
      ],
      'no-restricted-properties': [
        'error',
        {
          object: 'Image',
          property: 'getSize',
          message:
            'Usage of Image.getImage is restricted. Please use the `react-native-image-size`.'
        }
      ],

      // Other rules
      curly: 'error',
      'lodash/import-scope': ['error', 'method'],
      'prefer-regex-literals': 'off'
    },
    settings: {
      'import/resolver': {
        typescript: {}
      },
      react: {
        version: 'detect'
      }
    }
  }
)

tsconfig

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["es2017"],
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "strict": true,
    // Solves https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16825
    "skipLibCheck": true,
    "target": "esnext"
  }
}

Expected Result

should show code errors and also work fine with eslint": "^9.11.1

Actual Result

ESLint: 9.11.1

Error: This method cannot be used with flat config. Add your entries directly into the config array. at assertEslintrcConfig (/Users/mac-3/Desktop/SolankiYogesh/reeva-consumer/node_modules/eslint/lib/linter/linter.js:1230:15) at Linter.getRules (/Users/mac-3/Desktop/SolankiYogesh/reeva-consumer/node_modules/eslint/lib/linter/linter.js:2254:9) at Object. (/Users/mac-3/Desktop/SolankiYogesh/reeva-consumer/node_modules/eslint-config-love/lib/index.js:31:59) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at cjsLoader (node:internal/modules/esm/translators:348:17) at ModuleWrap. (node:internal/modules/esm/translators:297:7) at ModuleJob.run (node:internal/modules/esm/module_job:222:25)

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin ^8.7.0
@typescript-eslint/parser ^8.7.0
@typescript-eslint/rule-tester ^8.7.0
@typescript-eslint/scope-manager N/A
@typescript-eslint/typescript-estree N/A
@typescript-eslint/type-utils N/A
@typescript-eslint/utils ^8.7.0
TypeScript 5.6.2
ESLint ^9.11.1
node v20.16.0
bradzacher commented 2 weeks ago

There's a WHOLE lot of stuff here. You've got dozens of dependencies and you haven't really listed anything to enable someone to reproduce this. You should ensure you're providing all of the information for someone to be able to quickly reproduce the bug. For example -- creating an isolated reproduction repository with just the package.json, eslint config, and any other required files so that someone can just checkout, install, and reproduce.


That being said -- if you checkout the error stack trace you'll notice that it mentions:

at Object. (/Users/mac-3/Desktop/SolankiYogesh/reeva-consumer/node_modules/eslint-config-love/lib/index.js:31:59)

i.e. eslint-config-love is the cause.

And sure enough if we check the config -- it doesn't support ESLint v9. https://github.com/mightyiam/eslint-config-love/blob/33002904cbfa9868138b45f31d59c729abcc9ed9/package.json#L65

SolankiYogesh3500 commented 1 week ago

yeah thanks for reply i removed eslint-config-love and its started working again