eslinttypescript-eslint

eslint TypeScript using random .js files as config and failing them


I have newly installed eslint v9 into my TypeScript project. I have this eslint.config.mjs at the root of my files.

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config({
  files: ['**/*.ts'],
  extends: [
    eslint.configs.recommended,
    ...tseslint.configs.strict,
  ]
});

It mostly works as expected, but I'm also getting errors in generated javascript files; caused by eslint directives in npm packages; for example I see for the file generated by ChartJs:

/ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js
   3346:5  error    Definition for rule '@typescript-eslint/no-empty-function' was not found       @typescript-eslint/no-empty-function
  12515:9  error    Definition for rule '@typescript-eslint/no-use-before-define' was not found    @typescript-eslint/no-use-before-define
  12545:5  error    Definition for rule '@typescript-eslint/no-use-before-define' was not found    @typescript-eslint/no-use-before-define
  13490:5  warning  Unused eslint-disable directive (no problems were reported from 'complexity')
  13559:5  error    Definition for rule '@typescript-eslint/no-use-before-define' was not found    @typescript-eslint/no-use-before-define
  13562:1  warning  Unused eslint-disable directive (no problems were reported from 'complexity')
  14621:1  warning  Unused eslint-disable directive (no problems were reported from 'complexity')

If I run npx eslint --debug I see this for the example above:

eslint:eslint Lint /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:linter Linting code for /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:languages:js Parsing: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +2ms
eslint:languages:js Parsing successful: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +231ms
eslint:languages:js Scope analysis: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +0ms
eslint:languages:js Scope analysis successful: /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js +285ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +1ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +0ms
eslint:rules Loading rule 'complexity' (remaining=228) +4s
eslint:config-comment-parser Parsing list config +1ms
eslint:config-comment-parser Parsing list config +0ms
eslint:config-comment-parser Parsing list config +0ms
eslint:linter Generating fixed text for /ProjectRoot/wwwroot/dist/js/vendors-node_modules_chart_js_auto_auto_js.js (pass 1) +680ms
eslint:source-code-fixer Applying fixes +680ms
eslint:source-code-fixer shouldFix parameter was false, not attempting fixes +0ms

So it appears to be using the javascript file as the config. This is true for every JavaScript file it fails on; it is using that file as the config.

The top of the debug looks like this:

eslint:cli CLI args: [ '--debug' ] +0ms
eslint:cli Using flat config? true +2ms
eslint:cli Running on files +3ms
eslint:eslint Using file patterns: . +0ms
eslint:eslint Searching for eslint.config.js +0ms
eslint:eslint Loading config from /ProjectRoot/eslint.config.mjs +2ms
eslint:eslint Config file URL is file:///ProjectRoot/eslint.config.mjs +0ms
eslint:rules Loading rule 'consistent-return' (remaining=289) +0ms
## loads more rules...
eslint:eslint Deleting cache file at /ProjectRoot/.eslintcache +998ms
## loads even more rules...
eslint:eslint 191 files found in: 244ms +244ms
eslint:eslint Lint /ProjectRoot/eslint.config.mjs +19ms
eslint:linter Linting code for /ProjectRoot/eslint.config.mjs (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Parsing: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Parsing successful: /ProjectRoot/eslint.config.mjs +7ms
eslint:languages:js Scope analysis: /ProjectRoot/eslint.config.mjs +0ms
eslint:languages:js Scope analysis successful: /ProjectRoot/eslint.config.mjs +2ms
eslint:linter Generating fixed text for /ProjectRoot/eslint.config.mjs (pass 1) +15ms
eslint:source-code-fixer Applying fixes +0ms
eslint:source-code-fixer shouldFix parameter was false, not attempting fixes +0ms
eslint:eslint Lint /ProjectRoot/.template-scripts/publish/index.ts +16ms
eslint:linter Linting code for /ProjectRoot/webpack.config.js (pass 1) +0ms
eslint:linter Verify +0ms
eslint:linter With flat config: /ProjectRoot/webpack.config.js +0ms
### etc...

So it does it right from the top, including the config file and then my webpack.config.js file with itself as the config

Is this an eslint bug; or am I missing something?


Solution

  • The debug log is a bit misleading. It means that ESLint tries to lint the file with the given name using a flat config, see the source code. It does not mean that ESLint tries to use those files as a flat config.

    By default, ESLint includes all JavaScript files in the repository, unless they are explicitly ignored. You are trying to lint built & minified code which is expected to have lint issues. To exclude files or folders, use the ignores option. Note that when using TypeScript ESLint, for some reason it needs to be placed in a separate object:

    import eslint from '@eslint/js';
    import tseslint from 'typescript-eslint';
    
    export default tseslint.config(
      {
        ignores: [
          'dist/', // exclude specific folder
          '**/*.js', // exclude all JavaScript files
        ],
      },
      {
        files: ['**/*.ts'],
        extends: [
          eslint.configs.recommended,
          ...tseslint.configs.strict,
        ],
      },
    );