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

Update docs on supported file extensions #917

Merged
merged 5 commits into from Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 27 additions & 22 deletions README.md
Expand Up @@ -97,8 +97,14 @@ Options:
Starting with v3.1 you can now use different ways of configuring it:

- `lint-staged` object in your `package.json`
- `.lintstagedrc` file in JSON or YML format
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the extension:
- `.lintstagedrc.json`
- `.lintstagedrc.yaml`
- `.lintstagedrc.yml`
- `.lintstagedrc.js`
- `.lintstagedrc.cjs`
iiroj marked this conversation as resolved.
Show resolved Hide resolved
- `lint-staged.config.js` file in JS format
- `lint-staged.config.cjs` may also be used in case you have `"type": "module"` set in your `package.json`
- Pass a configuration file using the `--config` or `-c` flag

See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
Expand Down Expand Up @@ -211,24 +217,23 @@ The function can also be async:
// lint-staged.config.js
const micromatch = require('micromatch')

module.exports = (allStagedFiles) => {
const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh']);
if (shFiles.length) {
return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
}
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts']);
const docFiles = micromatch(allStagedFiles, ['**/*.md']);
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
module.exports = allStagedFiles => {
iiroj marked this conversation as resolved.
Show resolved Hide resolved
const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh'])
if (shFiles.length) {
return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
}
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts'])
const docFiles = micromatch(allStagedFiles, ['**/*.md'])
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
}
```


### Example: Wrap filenames in single quotes and run once per file

```js
// .lintstagedrc.js
module.exports = {
'**/*.js?(x)': (filenames) => filenames.map((filename) => `prettier --write '${filename}'`)
'**/*.js?(x)': filenames => filenames.map(filename => `prettier --write '${filename}'`)
}
```

Expand All @@ -246,23 +251,23 @@ module.exports = {
```js
// .lintstagedrc.js
module.exports = {
'**/*.js?(x)': (filenames) =>
filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`
'**/*.js?(x)': filenames => (filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`)
}
```

### Example: Use your own globs

It's better to use the [function-based configuration (seen above)](https://github.com/okonet/lint-staged/README.md#example-export-a-function-to-build-your-own-matchers), if your use case is this.

```js
// lint-staged.config.js
const micromatch = require('micromatch')

module.exports = {
'*': (allFiles) => {
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts']);
const docFiles = micromatch(allFiles, ['**/*.md']);
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
'*': allFiles => {
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts'])
const docFiles = micromatch(allFiles, ['**/*.md'])
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
}
}
```
Expand All @@ -276,7 +281,7 @@ If for some reason you want to ignore files from the glob match, you can use `mi
const micromatch = require('micromatch')

module.exports = {
'*.js': (files) => {
'*.js': files => {
// from `files` filter those _NOT_ matching `*test.js`
const match = micromatch.not(files, '*test.js')
return `eslint ${match.join(' ')}`
Expand All @@ -292,9 +297,9 @@ Please note that for most cases, globs can achieve the same effect. For the abov
const path = require('path')

module.exports = {
'*.ts': (absolutePaths) => {
'*.ts': absolutePaths => {
const cwd = process.cwd()
const relativePaths = absolutePaths.map((file) => path.relative(cwd, file))
const relativePaths = absolutePaths.map(file => path.relative(cwd, file))
return `ng lint myProjectName --files ${relativePaths.join(' ')}`
}
}
Expand Down Expand Up @@ -558,7 +563,7 @@ const { CLIEngine } = require('eslint')
const cli = new CLIEngine({})

module.exports = {
'*.js': (files) =>
'eslint --max-warnings=0 ' + files.filter((file) => !cli.isPathIgnored(file)).join(' ')
'*.js': files =>
'eslint --max-warnings=0 ' + files.filter(file => !cli.isPathIgnored(file)).join(' ')
}
```
2 changes: 2 additions & 0 deletions lib/index.js
Expand Up @@ -31,7 +31,9 @@ function loadConfig(configPath) {
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
],
})

Expand Down