skip to Main Content
"lint": "eslint -c .eslintrc.json --fix src/**/*.ts test/**/*.ts *.ts",
"lint:prod": "eslint -c .eslintrc.prod.json --fix src/**/*.ts test/**/*.ts *.ts",

Note the duplicative paths in these NPM scripts. And there is one more that look pretty much the same in the lint-staged config.

It seems I cannot omit the paths, and have eslint use its config file (.eslintrc.json) to find the files to lint:

  "overrides": [
    {
      "parser": "@typescript-eslint/parser",
      "plugins": [
        "@typescript-eslint"
      ],
      "files": [
        "src/**/*.ts",
        "test/**/*.ts",
        "*.ts"
      ]
    }
  ]

At least, with that config in place, and running npx eslint -c .eslintrc.json, no files are checked. Adding the paths to the command line does check the files.

I guess overrides[].files only tells the related parser which files to look at, but not eslint itself.

Is it not supported to just use the config file to figure out which files to check?

2

Answers


  1. Take advantage of npm scripts themselves and have a script that does "the shared bits", and invoke it with "the bits that are different":

    {
      "scripts":  {
        "lint": "eslint "src/**/*.ts test/**/*.ts *.ts" --fix",
        "lint:dev": "npm run lint -- -c .eslintrc.json",
        "lint:prod": "npm run lint -- -c .eslintrc.prod.json"
      }
    }
    

    (Also note the quotes around the glob, which eslint says is necessary, and that they’re double quotes rather than single quotes, to make sure things work on every OS. Single quotes will break on Windows)

    Login or Signup to reply.
  2. If you specify a folder to lint on the command line, eslint uses the files property from the overrides in the configuration file to find files to lint in that folder. See eslint documentation about specifying target files to lint.

    So instead of npx eslint -c .eslintrc.json you need to run npx eslint -c .eslintrc.json . to try this out. The only difference is the . at the end: the folder to lint.

    And in your package.json scripts you probably want something like:

    "lint": "eslint -c .eslintrc.json --fix .",
    "lint:prod": "eslint -c .eslintrc.prod.json --fix .",
    

    While it is annoying to put the . there, it does not create a maintenance burden because it will not change in the future. The actual configuration about which files to lint is maintained only once in the configuration file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search