Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add ignore config option #385

Merged
merged 3 commits into from
Jan 26, 2018

Conversation

kentcdodds
Copy link
Contributor

This allows users to prevent some files from being processed by lint-staged ever. This is useful if your project contains generated files that you commit to the repository, but don't want to process.

I know it's not great to commit generated files, but in this case it's really the best solution for me.

I didn't open an issue for this because I really need this anyway so if it's not merged then I'll just use my own fork.

This allows users to prevent some files from being processed by
lint-staged ever. This is useful if your project contains generated
files that you commit to the repository, but don't want to process.
@codecov
Copy link

codecov bot commented Jan 19, 2018

Codecov Report

Merging #385 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #385      +/-   ##
==========================================
+ Coverage    98.1%   98.13%   +0.03%     
==========================================
  Files          10       10              
  Lines         211      215       +4     
  Branches       23       26       +3     
==========================================
+ Hits          207      211       +4     
  Misses          4        4
Impacted Files Coverage Δ
src/getConfig.js 100% <ø> (ø) ⬆️
src/generateTasks.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e4f0f44...4b2f730. Read the comment docs.

@sudo-suhas
Copy link
Collaborator

Hey @kentcdodds, thanks for the PR 😄. I think overall, the feature addition makes sense. I have a minor suggestion. Instead of the multiple filter steps, how about we switch over to micromatch? This can filter files with multiple glob patterns and should be faster too.

@kentcdodds
Copy link
Contributor Author

Sounds fine. What would the API look like to add an ignore? The same? Should we maybe do it in a different pull request in that case?

@sudo-suhas
Copy link
Collaborator

I was thinking the API would be the same. We can just concat the ignore patterns(with ! at the beginning) to the selection criteria and filter in a single step. However, if you have any suggestion for how it could be different, we could consider that.

@kentcdodds
Copy link
Contributor Author

I don't mind making the change. Is this the only place minimatch is used in the codebase?

@sudo-suhas
Copy link
Collaborator

Yes, it is only used in generateTasks.js. It's also mentioned in a couple of places in the readme.

However, looks like there are very minor differences in the options for minimatch and micromatch. The following options are not present in micromatch:

So we might need to do this change in a separate PR and make a major release to denote the breaking change. I personally do not think these options would be used by anybody so it should be okay to make this change in a minor version. @okonet Could you take a call on this?

@kentcdodds
Copy link
Contributor Author

Yes, I'd prefer to do that in a separate PR. Seems unrelated to the core feature I want to add here :)

Copy link
Collaborator

@luftywiranda13 luftywiranda13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kentcdodds, thanks for the PR 🎉

I have a small suggestion...

README.md Outdated
@@ -129,6 +129,7 @@ To set options and keep lint-staged extensible, advanced format can be used. Thi
* `chunkSize` — Max allowed chunk size based on number of files for glob pattern. This is important on windows based systems to avoid command length limitations. See [#147](https://github.com/okonet/lint-staged/issues/147)
* `subTaskConcurrency` — `1` — Controls concurrency for processing chunks generated for each linter. Execution is **not** concurrent by default(see [#225](https://github.com/okonet/lint-staged/issues/225))
* `globOptions` — `{ matchBase: true, dot: true }` — [minimatch options](https://github.com/isaacs/minimatch#options) to customize how glob patterns match files.
* `ignore` - `['**/docs/**/*.js']` - array of glob patterns to entirely ignore from any task.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now it makes sense to sort items in options section alphabetically. So users can find what they need easier.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

@kentcdodds thanks for your PR. We have a stalled (my fault!) PR that should be able to make the whole setup more flexible here #273

As @sudo-suhas mentioned we want to transition to micromatch and introduce array syntax to linters key so you'll be able to add multiple globs and ignore patterns in an obvious way. What do you think of the proposed syntax?

@sudo-suhas
Copy link
Collaborator

@okonet While having the option to include multiple file filter patterns would satisfy the requirement, I think the feature addition of global ignore is still justified. I think one of the good things is that it doesn't require any changes for existing configuration format. Also, do you think the minimatch ➜ micromatch switch requires a major version change?

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

Not sure about global ignore tbh if we want to have arrays anyways. Wouldn’t it just add more frustration to have both?

Switching glob engine shouldn’t be a major release unless it’s a breaking change.

@sudo-suhas
Copy link
Collaborator

Wouldn’t it just add more frustration to have both?

Wasn't the use-case of being able to ignore files our strongest motivation for introducing the multiple glob patterns option? Also, cascading rules are quite common(ex: eslint) so it shouldn't cause any issue in my opinion.

Switching glob engine shouldn’t be a major release unless it’s a breaking change.

Please see #385 (comment)

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

@sudo-suhas regarding mircomatch: we should do a major release then. No need to take any risks here.

Regarding the ignore option: if the only use case is to ignore files globally, when we should not over complicate things and introduce this global ignore patterns option. My concern is that next request will be "that's great but how do I ignore files only for this task" :)

But again, I'm leaving towards solving one problem at the time and it seems like @kentcdodds solution is good enough for now so let's go ahead with it.

@sudo-suhas sudo-suhas dismissed luftywiranda13’s stale review January 21, 2018 13:02

Requested changes were done

Copy link
Collaborator

@sudo-suhas sudo-suhas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* `concurrent` — *true* — runs linters for each glob pattern simultaneously. If you don’t want this, you can set `concurrent: false`
* `chunkSize` — Max allowed chunk size based on number of files for glob pattern. This is important on windows based systems to avoid command length limitations. See [#147](https://github.com/okonet/lint-staged/issues/147)
* `globOptions` — `{ matchBase: true, dot: true }` — [minimatch options](https://github.com/isaacs/minimatch#options) to
customize how glob patterns match files.
* `ignore` - `['**/docs/**/*.js']` - array of glob patterns to entirely ignore from any task.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add something like. Useful to skip linting of generated files.

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

Closes #310 (I guess?)

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

The only thing that just popped in my head is that this PR will prevent us from doing #277 which I'd really love to do since I never was a fan of having multiple config formats. So, the question is: should the ignore go into linter's items config, perhaps? Just realized it was the whole point of #273 discussion :(

@sudo-suhas
Copy link
Collaborator

#277 might make this obsolete in which case, we can drop it from the global options. Since it will be a breaking change, we can just ask users to move the ignore to each linter's options.

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

Not sure if this is a more desired approach in this case. I'd prefer to pull off the #273 instead, TBH. I'm wondering what @kentcdodds has to say if proposed changes would solve his use case.

Looks like I'll get some time to work on #62 and #273 soon!

@kentcdodds
Copy link
Contributor Author

Looks like #273 was closed?

@okonet
Copy link
Collaborator

okonet commented Jan 21, 2018

I will need to create a new PR but the discussion is still relevant.

@kentcdodds
Copy link
Contributor Author

That looks like it would work fine for me. I just need a way to specify a glob of files I don't want processed.

@kentcdodds
Copy link
Contributor Author

What's the status on this? Could we merge it as-is and then iterate to the improvements?

@okonet
Copy link
Collaborator

okonet commented Jan 25, 2018

I still didn’t find to work on it yet. Sorry.

@sudo-suhas
Copy link
Collaborator

Could we merge it as-is and then iterate to the improvements?

I second this. Moreover, I have come to have reservations about 'Get rid of advanced config options'. We would be writing ourselves into a corner where every option would have to be at the linters level. This would restrict us in adding options which might make sense to have at the global level. To clarify, I support removing config options like subTaskConcurrency but I'd still like to leave the window open for any future requirements.

@okonet
Copy link
Collaborator

okonet commented Jan 26, 2018

Okay let’s go forward with this PR then. @sudo-suhas please go ahead and merge it.

@sudo-suhas sudo-suhas merged commit 5b7bc67 into lint-staged:master Jan 26, 2018
@sudo-suhas
Copy link
Collaborator

Merged and released in v6.1.0 🎉. Thanks again @kentcdodds!

@kentcdodds
Copy link
Contributor Author

Super! Thank you!

@kentcdodds kentcdodds deleted the pr/ignore branch January 26, 2018 07:08
@okonet
Copy link
Collaborator

okonet commented Jan 26, 2018

Thanks @kentcdodds and @sudo-suhas for taking care of this!

@@ -14,6 +14,12 @@ module.exports = function generateTasks(config, relFiles) {
const normalizedConfig = getConfig(config) // Ensure we have a normalized config
const linters = normalizedConfig.linters
const globOptions = normalizedConfig.globOptions
const ignoreFilters = normalizedConfig.ignore.map(pattern => minimatch.filter(pattern))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This minimatch.filter() call isn't being passed globOptions. Is this intentional?

I'm also unable to ignore '.vscode/*.json' (whereas 'package.json' in the same ignores array works fine).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably pass globOptions I guess 🤔

If we did, then your problem with .vscode would probably be resolved because you could specify the dot option.

Copy link
Collaborator

@sudo-suhas sudo-suhas Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to reproduce this issue locally:

λ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .vscode/settings.json

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .lintstagedrc

λ cat .lintstagedrc
{
    "linters": {
        "*.js": [
            "eslint --fix",
            "git add"
        ],
        ".*rc": "jsonlint",
        "*.json": "jsonlint"
    },
    "ignore": [".vscode/*.json"]
}

λ node . --debug
  lint-staged:bin Running `[email protected]` +0ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `E:\Projects\repos\lint-staged\.lintstagedrc`:
  lint-staged { linters:
  lint-staged    { '*.js': [ 'eslint --fix', 'git add' ],
  lint-staged      '.*rc': 'jsonlint',
  lint-staged      '*.json': 'jsonlint' },
  lint-staged   ignore: [ '.vscode/*.json' ] } +16ms
  lint-staged:cfg Normalizing config +0ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  linters: {
    '*.js': [
      'eslint --fix',
      'git add'
    ],
    '.*rc': 'jsonlint',
    '*.json': 'jsonlint'
  },
  ignore: [
    '.vscode/*.json'
  ],
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  subTaskConcurrency: 1,
  renderer: 'verbose'
}
  lint-staged Loaded scripts from package.json:
  lint-staged { precommit: 'node index.js',
  lint-staged   cz: 'git-cz',
  lint-staged   lint: 'eslint .',
  lint-staged   'lint:fix': 'yarn lint --fix',
  lint-staged   pretest: 'yarn lint',
  lint-staged   test: 'jest --coverage',
  lint-staged   'test:watch': 'jest --watch' } +16ms
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `E:\Projects\repos\lint-staged` +15ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ '.vscode/settings.json' ] +241ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +272ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '*.js',
  lint-staged:gen-tasks   commands: [ 'eslint --fix', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +0ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '.*rc', commands: 'jsonlint', fileList: [] } +0ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '*.json', commands: 'jsonlint', fileList: [] } +0ms
Running tasks for *.js [started]
Running tasks for .*rc [started]
Running tasks for *.json [started]
Running tasks for *.js [skipped]
→ No staged files match *.js
Running tasks for .*rc [skipped]
→ No staged files match .*rc
Running tasks for *.json [skipped]
→ No staged files match *.json
  lint-staged linters were executed successfully! +271ms

Please take a look at your debug logs. It could be a configuration issue. If you continue to face the issue, please do create a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants