From 5b7bc67c8cdf5160bc78fb4012b2f40d85f88075 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 25 Jan 2018 23:42:42 -0700 Subject: [PATCH] feat(config): Add `ignore` config option (#385) 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. --- README.md | 6 ++++-- src/generateTasks.js | 7 +++++++ src/getConfig.js | 1 + test/__snapshots__/getConfig.spec.js.snap | 3 +++ test/__snapshots__/index.spec.js.snap | 2 ++ test/generateTasks.spec.js | 14 ++++++++++++++ test/getConfig.spec.js | 1 + 7 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec4120be9..8e25de049 100644 --- a/README.md +++ b/README.md @@ -124,11 +124,13 @@ To set options and keep lint-staged extensible, advanced format can be used. Thi ## Options -* `linters` — `Object` — keys (`String`) are glob patterns, values (`Array | String`) are commands to execute. * `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. +* `linters` — `Object` — keys (`String`) are glob patterns, values (`Array | String`) are commands to execute. * `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. ## Filtering files diff --git a/src/generateTasks.js b/src/generateTasks.js index 5d3fe73f1..04469229e 100644 --- a/src/generateTasks.js +++ b/src/generateTasks.js @@ -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)) + // if there are filters, then return false if the input matches any + // if there are not, then return true for all input + const ignoreFilter = ignoreFilters.length + ? input => !ignoreFilters.some(filter => filter(input)) + : () => true const gitDir = resolveGitDir() const cwd = process.cwd() @@ -30,6 +36,7 @@ module.exports = function generateTasks(config, relFiles) { .map(file => path.relative(cwd, file)) // We want to filter before resolving paths .filter(filter) + .filter(ignoreFilter) // Return absolute path after the filter is run .map(file => path.resolve(cwd, file)) diff --git a/src/getConfig.js b/src/getConfig.js index f9536ec7b..a46626b0c 100644 --- a/src/getConfig.js +++ b/src/getConfig.js @@ -27,6 +27,7 @@ const defaultConfig = { dot: true }, linters: {}, + ignore: [], subTaskConcurrency: 1, renderer: 'update' } diff --git a/test/__snapshots__/getConfig.spec.js.snap b/test/__snapshots__/getConfig.spec.js.snap index 511c5c6fb..9e6d96016 100644 --- a/test/__snapshots__/getConfig.spec.js.snap +++ b/test/__snapshots__/getConfig.spec.js.snap @@ -8,6 +8,7 @@ Object { "dot": true, "matchBase": true, }, + "ignore": Array [], "linters": Object {}, "renderer": "update", "subTaskConcurrency": 1, @@ -22,6 +23,7 @@ Object { "dot": true, "matchBase": true, }, + "ignore": Array [], "linters": Object {}, "renderer": "update", "subTaskConcurrency": 1, @@ -36,6 +38,7 @@ Object { "dot": true, "matchBase": true, }, + "ignore": Array [], "linters": Object { "*.js": Array [ "eslint --fix", diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 39f2d214d..8f1cdd4e2 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -13,6 +13,7 @@ LOG { matchBase: true, dot: true }, + ignore: [], subTaskConcurrency: 1, renderer: 'verbose' }" @@ -33,6 +34,7 @@ LOG { matchBase: true, dot: true }, + ignore: [], subTaskConcurrency: 1, renderer: 'verbose' }" diff --git a/test/generateTasks.spec.js b/test/generateTasks.spec.js index b3313bd97..013c514ae 100644 --- a/test/generateTasks.spec.js +++ b/test/generateTasks.spec.js @@ -205,4 +205,18 @@ describe('generateTasks', () => { ) }) }) + + it('should ignore patterns in the ignore array of configuration', () => { + const pattern = '**/*.js' + const commands = 'lint' + const result = generateTasks( + { ignore: ['**/ignore/**', '**/ignore.*'], linters: { [pattern]: commands } }, + ['ignore/me.js', 'ignore.me.js', 'cool/js.js'] + ) + expect(result[0]).toEqual({ + pattern, + commands, + fileList: [`${workDir}/cool/js.js`].map(path.normalize) + }) + }) }) diff --git a/test/getConfig.spec.js b/test/getConfig.spec.js index 8f14de8c7..3ffd0eb18 100644 --- a/test/getConfig.spec.js +++ b/test/getConfig.spec.js @@ -139,6 +139,7 @@ describe('getConfig', () => { linters: { '*.js': 'eslint' }, + ignore: ['docs/**/*.js'], subTaskConcurrency: 10, renderer: 'custom' }