eslint / eslint

Find and fix problems in your JavaScript code.
https://eslint.org
MIT License
24.39k stars 4.4k forks source link

Bug: the `files` and `ignore` configuration properties are being ignored #18417

Closed dancrumb closed 2 weeks ago

dancrumb commented 2 weeks ago

Environment

Node version: v18.20.2 yarn version: 3.6.1 Local ESLint version: v9.2.0 Operating System: macOS

What parser are you using?

@typescript-eslint/parser

What did you do?

Configuration Here's a slimmed down version of my config; I only removed a bunch of rules, since they aren't relevant to the issue. ``` import tsEslint from '@typescript-eslint/eslint-plugin' import tsParse from '@typescript-eslint/parser' export default [ { files: ["**/*.ts"], ignores: [ '**/dist/**', '**/coverage/**', '**/node_modules/**', '**/docs/**' ] }, { languageOptions: { ecmaVersion: 2019, sourceType: 'module', parser: tsParse }, plugins: { '@typescript-eslint': tsEslint } }, { rules: { "no-unused-vars": "off", "@typescript-eslint/no-unused-vars": [ "error", { "argsIgnorePattern": "^_" } ], }, }]; ```

When I run eslint, it's reporting issues with a bunch of files that are in ignored directories. In addition, it's reporting on .js files.

What did you expect to happen?

I would expect ESLint to only report on .ts files and to ignore any files per my configuration

What actually happened?

I'm getting errors reported on files that have been ignored.

In addition, when I run yarn run eslint --print-config eslint.config.js, I get the following output, which is missing the files and ignores fields.

Printed Configuration ```json { "linterOptions": { "reportUnusedDisableDirectives": 1 }, "languageOptions": { "ecmaVersion": 2019, "sourceType": "module", "parser": "typescript-eslint/parser@7.8.0", "parserOptions": {}, "globals": {} }, "plugins": [ "@", "@typescript-eslint:@typescript-eslint/eslint-plugin@7.8.0" ], "rules": { "@typescript-eslint/adjacent-overload-signatures": [ 2 ], "@typescript-eslint/no-explicit-any": [ 2 ], "@typescript-eslint/no-misused-new": [ 2 ], "@typescript-eslint/prefer-for-of": [ 2 ], "@typescript-eslint/unified-signatures": [ 2 ], "@typescript-eslint/no-shadow": [ 2 ], "no-unused-vars": [ 0 ], "@typescript-eslint/no-unused-vars": [ 2, { "argsIgnorePattern": "^_" } ], "@typescript-eslint/no-redeclare": [ 2 ], "no-dupe-class-members": [ 0 ], "@typescript-eslint/no-dupe-class-members": [ 2 ], "eqeqeq": [ 2, "smart" ], "no-caller": [ 2 ], "no-invalid-this": [ 2 ], "no-new-wrappers": [ 2 ], "no-shadow": [ 0 ], "no-throw-literal": [ 2 ], "no-unused-labels": [ 2 ], "no-var": [ 2 ], "arrow-body-style": [ 0 ], "prefer-arrow-callback": [ 0 ], "no-redeclare": [ 0 ] } } ```

Link to Minimal Reproducible Example

In this example, you can see that errors are reported on the .js file as well as the .ts file

https://eslint-online-playground.netlify.app/#eNqFUk1v1DAQ/SvGl0DUzba9AKmQOIAEJ3pAXPBSmWQSuTgey3ZWRVX+OxOPd1m0BS75eDNv3rxnP8oYui086MlbaO6jbGWHLiahxRtRfa9ulJMXpz1XTTo0ITEsjs+V/ADW4jMlX1A70wdEGnB9Q2SI1rjUED6YkSXM5DEkkeL7XBNDwElUb9NPD7ELxqcNk7b82ng7j8ZVyh2JtzpE+DvPr+VABOXgIVN6GPRsk/iqnBCP60OIwViILUGyrrc1GVNyd8ElMzoMXMz/QlTU05uYtnVdlSYGO9xD0COcFRz2cDdhP5PKWbHHLoOM7dbXkhvKbla7caapn3wylGh7wIWAbtJfIESCW3F9efX6ODfiHDr4TGm0omLh36IcSXvIjmGWpGIOeFV5Ks5qZZWjWnjTwzphNXeynJION7ObI/SbPeko2RKGw6DkcRMlnzixc9ox+ZUBIWA4mUE5EarDGD/mk7rVKUFwLPftTkleNGdbSOx12fGV9rr7QfHSfURHNzIbUHLdi2dwfEVRyR7278CD68F1BvJ+xTPtlh0wzeoEkb6LJlkt/u5LFOcN/7n1THvZvGou/83iA/6jPRtWbpHLL4wMO8I=

Participation

Additional comments

No response

mdjermanovic commented 2 weeks ago

Hi @dancrumb, thanks for the issue!

This works as intended. If you want to specify patterns to ignore files and directories, ignores key should be used without any other keys in the configuration object:

https://eslint.org/docs/latest/use/configure/ignore

Also, .js files are linted by default. If you want to ignore .js files, you can specify '**/*.js' in ignore patterns.

Here's your config modified to ignore all .js files and dist, coverage, node_modules and docs directories:

import tsEslint from '@typescript-eslint/eslint-plugin'
import tsParse from '@typescript-eslint/parser'

export default [

  // ignored files and directories
  {    
    ignores: [
      '**/*.js', // ignore all .js files
      '**/dist/**',
      '**/coverage/**',
      '**/node_modules/**',
      '**/docs/**'
    ]
  },

  {
    files: ["**/*.ts"], // needed to enable linting of .ts files
    languageOptions: {
      ecmaVersion: 2019,
      sourceType: 'module',
      parser: tsParse
    },
    plugins: { '@typescript-eslint': tsEslint }
  }, {
    rules: {
      "no-unused-vars": "off",
      "@typescript-eslint/no-unused-vars": [
        "error",
        { "argsIgnorePattern": "^_" }
      ],
    },
  }];
dancrumb commented 2 weeks ago

Great!

Thanks.

FWIW, I was migrating an old configuration, so was reading this: https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files and the requirement for it to be in its own configuration object isn't so clear there.

I'd be happy to create a documentation PR

mdjermanovic commented 2 weeks ago

https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files and the requirement for it to be in its own configuration object isn't so clear there.

I agree that it isn't so clear there. PR to clarify this in the migration guide is welcome!