Skip to content

Commit a178072

Browse files
authored
Update lint-staged example to use node.js path (#30510)
The current example attempts to remove resolve the relative pathname of the absolute pathnames given by `lint-staged` using string manipulation and `process.cwd()`. This is a risky solution because pathnames have many platform dependent edge cases. The example also returns pathnames that are technically wrong in linux. `/home/project/file => /file`, I propose to use the utilities provided by node.js to resolve the relative pathnames, and to format the sample `.lintstagedrc.js` file in a manner easier for novice programmers to understand and modify. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint`
1 parent 9d1fbbc commit a178072

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

docs/basic-features/eslint.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,15 @@ Then, add `prettier` to your existing ESLint config:
203203
If you would like to use `next lint` with [lint-staged](https://github.com/okonet/lint-staged) to run the linter on staged git files, you'll have to add the following to the `.lintstagedrc.js` file in the root of your project in order to specify usage of the `--file` flag.
204204

205205
```js
206+
const path = require('path')
207+
208+
const buildEslintCommand = (filenames) =>
209+
`next lint --fix --file ${filenames
210+
.map((f) => path.relative(process.cwd(), f))
211+
.join(' --file ')}`
212+
206213
module.exports = {
207-
'**/*.js?(x)': (filenames) =>
208-
`next lint --fix --file ${filenames
209-
.map((file) => file.split(process.cwd())[1])
210-
.join(' --file ')}`,
214+
'*.{js,jsx,ts,tsx}': [buildEslintCommand],
211215
}
212216
```
213217

0 commit comments

Comments
 (0)