Skip to content

Commit

Permalink
feat(config): Add ignore config option (#385)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kent C. Dodds authored and sudo-suhas committed Jan 26, 2018
1 parent e4f0f44 commit 5b7bc67
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> | 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> | 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

Expand Down
7 changes: 7 additions & 0 deletions src/generateTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))

Expand Down
1 change: 1 addition & 0 deletions src/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const defaultConfig = {
dot: true
},
linters: {},
ignore: [],
subTaskConcurrency: 1,
renderer: 'update'
}
Expand Down
3 changes: 3 additions & 0 deletions test/__snapshots__/getConfig.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Object {
"dot": true,
"matchBase": true,
},
"ignore": Array [],
"linters": Object {},
"renderer": "update",
"subTaskConcurrency": 1,
Expand All @@ -22,6 +23,7 @@ Object {
"dot": true,
"matchBase": true,
},
"ignore": Array [],
"linters": Object {},
"renderer": "update",
"subTaskConcurrency": 1,
Expand All @@ -36,6 +38,7 @@ Object {
"dot": true,
"matchBase": true,
},
"ignore": Array [],
"linters": Object {
"*.js": Array [
"eslint --fix",
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LOG {
matchBase: true,
dot: true
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
}"
Expand All @@ -33,6 +34,7 @@ LOG {
matchBase: true,
dot: true
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
}"
Expand Down
14 changes: 14 additions & 0 deletions test/generateTasks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
1 change: 1 addition & 0 deletions test/getConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('getConfig', () => {
linters: {
'*.js': 'eslint'
},
ignore: ['docs/**/*.js'],
subTaskConcurrency: 10,
renderer: 'custom'
}
Expand Down

0 comments on commit 5b7bc67

Please sign in to comment.