Lint-staged: Exclude paths

Created on 18 Jan 2017  路  14Comments  路  Source: okonet/lint-staged

Hi,

Thanks for a great library. 馃尮

I'm using lint-staged to run tslint on typescript files (*.ts) but I'd like to exclude certain directories from being linted.

How can I exclude certain paths from being linted?

Cheers

question

Most helpful comment

@okonet how would I use the exclude pattern in minimatch to ignore certain files? I need to ignore compiled files. how should my lint-staged config look? what I want is something like this:

"lint-staged": {
  "linters": {
    "*.js,!public/dist/*.js": [
      "prettier --write"",
      "git add"
    ]
  }
}

However, I don't think *.js,!public/dist/*.js is valid minimatch pattern. Do you know how I could use that?

All 14 comments

The best way to do that is to use .eslintignore and TS alternative (not using it, sorry). In this case it won't matter what runner you're using and your files will always be excluded. If you still want to make the exclusion lint-staged specific then check out the minimatch docs. I assume something like !your_path should work

@okonet Have tried using .eslintignore to ignore some .js files, but it didn't work. eslint still tries to fix the ignored config file (webpack.config.dev.js). Any help would be appreciated.

.eslintignore

# Webpack configs
webpack.config.dev.js

.lintstagedrc

{
    'gitDir': '../../../../',
    'linters': {
        '*.js': ['eslint --fix', 'git add']
    }
}

You should probably refer to https://github.com/eslint/eslint documentation. I _assume_ this is because you have your .eslintignore in an unexpected place due to 'gitDir': '../../../../'. Try running eslint on its own and see if it is working without lint-staged.

@okonet Thepackage.json file is in subfolder and .git located at root level that's why I have configured the 'gitDir': '../../../../' this way.I have kept the .eslintignore and package.json under the same folder so the path shouldn't be the problem I believe, I have verified that eslint without lint-staged it does ignores the file as expected in addition to the below message.

 File ignored because of a matching ignore pattern. Use "--no-ignore" to override
"precommit": "eslint --fix  webpack.config.dev.js"

Hmm that's weird. Can you create a repo where I could reproduce it please?

@okonet I'm unable to reproduce this issue now with my current repo as well as in the below new repo I've created to simulate the behavior. I'm not sure what was wrong earlier 馃槙

https://github.com/karthilxg/lint-staged-precommit

So, can I close this issues then?

Yes!! Thanks for your support.

@okonet how would I use the exclude pattern in minimatch to ignore certain files? I need to ignore compiled files. how should my lint-staged config look? what I want is something like this:

"lint-staged": {
  "linters": {
    "*.js,!public/dist/*.js": [
      "prettier --write"",
      "git add"
    ]
  }
}

However, I don't think *.js,!public/dist/*.js is valid minimatch pattern. Do you know how I could use that?

@tnguyen14 it seems it is not. And the whole discussion about negation patterns doesn't see to make it easier: https://github.com/isaacs/minimatch/issues/65

I think https://github.com/okonet/lint-staged/pull/188#issuecomment-309735299 will be the solution to this. Thoughts?

Yes, I think the solution mentioned in that comment and #201 would be ideal, i.e. providing an array of of patterns for matching.

The proposed solution at https://github.com/okonet/lint-staged/issues/123#issuecomment-315179381 is working to me

{
  "lint-staged": {
    "linters": {
      "*.js,!*.min.js": [
        "prettier-standard",
        "git add"
      ]
    }
}

I'm not sure if something changes in the internal match library 馃

If anyone like me stumbles upon same issue when trying to exclude some folder from lint-staged scope, here's the solution:

{
  "linters": {
    "!(client)/**/*.{ts,tsx}": ["npm run lint:server --", "git add"]
  }
}

Here lint-staged will run for every .ts or .tsx file in the project except these in root client folder (I've got separate tslint.json for my client files).

Some additional exclude patterns can be found here: https://github.com/mysticatea/cpx/issues/24#issuecomment-285389024.

in case you would like just to specify some path src for example:

"lint-staged": {
    "src/**/*.js": [
      "yarn run pretty",
      "yarn run lint",
      "yarn run test",
      "git add"
    ]
  },
Was this page helpful?
0 / 5 - 0 ratings