lint-staged 7.1.3 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@ The latest versions of `lint-staged` require Node.js v6 or newer. (Versions of `
6
6
 
7
7
  ## Why
8
8
 
9
- Linting makes more sense when running before committing your code. By doing that you can ensure no errors are going into repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
9
+ Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
10
10
 
11
11
  This project contains a script that will run arbitrary shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
12
12
 
@@ -39,7 +39,7 @@ This project contains a script that will run arbitrary shell tasks with a list o
39
39
 
40
40
  Now change a few files, `git add` some of them to your commit and try to `git commit` them.
41
41
 
42
- And this is how it looks like in action:
42
+ This is how it looks in action:
43
43
 
44
44
  <p align="center">
45
45
  <img src="./screenshots/lint-staged-prettier.gif" alt="lint-staged with prettier example"
@@ -71,14 +71,14 @@ $ ./node_modules/.bin/lint-staged --help
71
71
  Options:
72
72
 
73
73
  -V, --version output the version number
74
- -c, --config [path] Path to configuration file
74
+ -c, --config [path] Configuration file path or package
75
75
  -d, --debug Enable debug mode
76
76
  -h, --help output usage information
77
77
  ```
78
78
 
79
- * **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search.
79
+ * **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search. You may pass a npm package name for configuration also.
80
80
  * **`--debug`**: Enabling the debug mode does the following:
81
- * `lint-staged` uses the [debug](https://github.com/visionmedia/debug) module internally to log information about staged files, commands being executed, location of binaries etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
81
+ * `lint-staged` uses the [debug](https://github.com/visionmedia/debug) module internally to log information about staged files, commands being executed, location of binaries, etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
82
82
  * Use the [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`.
83
83
  * Do not pass `--silent` to npm scripts.
84
84
 
@@ -125,9 +125,24 @@ So, considering you did `git add file1.ext file2.ext`, lint-staged will run the
125
125
 
126
126
  ### Advanced config format
127
127
 
128
- To set options and keep lint-staged extensible, advanced format can be used. This should hold linters object in `linters` property.
128
+ To extend and customise lint-staged, advanced options are available. To use these options the format should be as the following:
129
129
 
130
- ## Options
130
+ #### `package.json` example with `ignore` option:
131
+
132
+ ```json
133
+ {
134
+ "lint-staged": {
135
+ "linters": {
136
+ "*.{js,scss}": ["some command", "git add"]
137
+ },
138
+ "ignore": ["**/dist/*.min.js"]
139
+ }
140
+ }
141
+ ```
142
+
143
+ Notice that the linting commands now are nested into the `linters` object. The following options are available for advance configuration:
144
+
145
+ #### Options
131
146
 
132
147
  * `concurrent` — _true_ — runs linters for each glob pattern simultaneously. If you don’t want this, you can set `concurrent: false`
133
148
  * `chunkSize` — Max allowed chunk size based on number of files for glob pattern. This option is only applicable on Windows based systems to avoid command length limitations. See [#147](https://github.com/okonet/lint-staged/issues/147)
@@ -326,3 +341,16 @@ If you wish to use `lint-staged` in a multi package monorepo, it is recommended
326
341
  [`lerna`](https://github.com/lerna/lerna) can be used to execute the `precommit` script in all sub-packages.
327
342
 
328
343
  Example repo: [sudo-suhas/lint-staged-multi-pkg](https://github.com/sudo-suhas/lint-staged-multi-pkg).
344
+
345
+ ### Can I lint files outside of the current project folder?
346
+
347
+ tl;dr: Yes, but the pattern should start with `../`.
348
+
349
+ By default, `lint-staged` executes linters only on the files present inside the project folder(where `lint-staged` is installed and run from).
350
+ So this question is relevant _only_ when the project folder is a child folder inside the git repo.
351
+ In certain project setups, it might be desirable to bypass this restriction. See [#425](https://github.com/okonet/lint-staged/issues/425), [#487](https://github.com/okonet/lint-staged/issues/487) for more context.
352
+
353
+ `lint-staged` provides an escape hatch for the same(`>= v7.3.0`). For patterns that start with `../`, all the staged files are allowed to match against the pattern.
354
+ Note that patterns like `*.js`, `**/*.js` will still only match the project files and not any of the files in parent or sibling directories.
355
+
356
+ Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo-suhas/lint-staged-django-react-demo).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "7.1.3",
3
+ "version": "7.3.0",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/okonet/lint-staged",
@@ -28,7 +28,6 @@
28
28
  "test:watch": "jest --watch"
29
29
  },
30
30
  "dependencies": {
31
- "app-root-path": "^2.0.1",
32
31
  "chalk": "^2.3.1",
33
32
  "commander": "^2.14.1",
34
33
  "cosmiconfig": "^5.0.2",
@@ -38,7 +37,7 @@
38
37
  "find-parent-dir": "^0.3.0",
39
38
  "is-glob": "^4.0.0",
40
39
  "is-windows": "^1.0.2",
41
- "jest-validate": "^23.0.0",
40
+ "jest-validate": "^23.5.0",
42
41
  "listr": "^0.14.1",
43
42
  "lodash": "^4.17.5",
44
43
  "log-symbols": "^2.2.0",
package/src/findBin.js CHANGED
@@ -1,14 +1,22 @@
1
1
  'use strict'
2
2
 
3
3
  const parse = require('string-argv')
4
- const appRoot = require('app-root-path')
5
4
  const npmWhich = require('npm-which')(process.cwd())
6
5
  const checkPkgScripts = require('./checkPkgScripts')
7
6
 
8
- // Find and load the package.json at the root of the project.
9
- const pkg = require(appRoot.resolve('package.json')) // eslint-disable-line import/no-dynamic-require
10
7
  const debug = require('debug')('lint-staged:find-bin')
11
8
 
9
+ // Find and load the package.json at the root of the project.
10
+ let pkg
11
+ try {
12
+ // eslint-disable-next-line import/no-dynamic-require, global-require
13
+ pkg = require(`${process.cwd()}/package.json`)
14
+ debug('Loaded package.json using `process.cwd()`')
15
+ } catch (ignore) {
16
+ debug('Could not load package.json using `process.cwd()`')
17
+ pkg = {}
18
+ }
19
+
12
20
  const cache = new Map()
13
21
 
14
22
  module.exports = function findBin(cmd) {
@@ -8,7 +8,7 @@ const resolveGitDir = require('./resolveGitDir')
8
8
 
9
9
  const debug = require('debug')('lint-staged:gen-tasks')
10
10
 
11
- module.exports = function generateTasks(config, relFiles) {
11
+ module.exports = function generateTasks(config, stagedRelFiles) {
12
12
  debug('Generating linter tasks')
13
13
 
14
14
  const normalizedConfig = getConfig(config) // Ensure we have a normalized config
@@ -17,16 +17,18 @@ module.exports = function generateTasks(config, relFiles) {
17
17
 
18
18
  const gitDir = resolveGitDir()
19
19
  const cwd = process.cwd()
20
- const files = relFiles.map(file => path.resolve(gitDir, file))
20
+ const stagedFiles = stagedRelFiles.map(file => path.resolve(gitDir, file))
21
21
 
22
22
  return Object.keys(linters).map(pattern => {
23
+ const isParentDirPattern = pattern.startsWith('../')
23
24
  const patterns = [pattern].concat(ignorePatterns)
24
25
  const commands = linters[pattern]
25
26
 
26
27
  const fileList = micromatch(
27
- files
28
- // Only worry about children of the CWD
29
- .filter(file => pathIsInside(file, cwd))
28
+ stagedFiles
29
+ // Only worry about children of the CWD unless the pattern explicitly
30
+ // specifies that it concerns a parent directory.
31
+ .filter(file => isParentDirPattern || pathIsInside(file, cwd))
30
32
  // Make the paths relative to CWD for filtering
31
33
  .map(file => path.relative(cwd, file)),
32
34
  patterns,
package/src/getConfig.js CHANGED
@@ -138,6 +138,7 @@ function validateConfig(config) {
138
138
  exampleConfig,
139
139
  deprecatedConfig,
140
140
  unknown: unknownValidationReporter,
141
+ recursive: false,
141
142
  comment:
142
143
  'Please refer to https://github.com/okonet/lint-staged#configuration for more information...'
143
144
  })
package/src/index.js CHANGED
@@ -18,20 +18,28 @@ if (process.stdout.isTTY) {
18
18
 
19
19
  const errConfigNotFound = new Error('Config could not be found')
20
20
 
21
+ function resolveConfig(configPath) {
22
+ try {
23
+ return require.resolve(configPath)
24
+ } catch (ignore) {
25
+ return configPath
26
+ }
27
+ }
28
+
21
29
  function loadConfig(configPath) {
22
30
  const explorer = cosmiconfig('lint-staged', {
23
31
  searchPlaces: [
24
32
  'package.json',
25
- `.lintstagedrc`,
26
- `.lintstagedrc.json`,
27
- `.lintstagedrc.yaml`,
28
- `.lintstagedrc.yml`,
29
- `.lintstagedrc.js`,
30
- `lint-staged.config.js`
33
+ '.lintstagedrc',
34
+ '.lintstagedrc.json',
35
+ '.lintstagedrc.yaml',
36
+ '.lintstagedrc.yml',
37
+ '.lintstagedrc.js',
38
+ 'lint-staged.config.js'
31
39
  ]
32
40
  })
33
41
 
34
- return configPath ? explorer.load(configPath) : explorer.search()
42
+ return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
35
43
  }
36
44
 
37
45
  /**