![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
lint-staged
Advanced tools
The lint-staged npm package is used to run linters on staged git files. It allows you to run specific commands before committing, ensuring that only clean, linted code gets committed to your repository. This helps in maintaining code quality and reducing the chances of committing code with errors or that doesn't adhere to the project's coding standards.
Running linters on staged files
This configuration in package.json will run ESLint on staged JavaScript files and Stylelint on staged CSS files, automatically fixing any fixable issues.
"lint-staged": {
"*.js": "eslint --fix",
"*.css": "stylelint --fix"
}
Running custom scripts
This configuration will run markdownlint-cli2 on staged Markdown files to ensure they meet the project's markdown style requirements.
"lint-staged": {
"*.md": "npx markdownlint-cli2"
}
Using with pre-commit hooks
This configuration sets up Husky to run lint-staged as a pre-commit hook, ensuring that the linters are run automatically before each commit.
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
pretty-quick is an npm package that runs Prettier on your changed files. It is similar to lint-staged but is specifically focused on formatting with Prettier rather than running arbitrary linters or tasks.
Husky can be used to manage Git hooks and can run tasks on commit, push, and more. While it doesn't run linters on staged files by itself, it is often used in conjunction with lint-staged to trigger linters before a commit.
Lefthook is a fast and powerful Git hooks manager for Node.js, Ruby, or any other type of projects. It can run linters and custom scripts similar to lint-staged, but it also provides additional features like parallel task execution and support for multiple programming languages.
Run linters against staged git files and don't let :poop: slip into your code base!
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.
This project contains a script that will run arbitrary npm and shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
If you've written one, please submit a PR with the link to it!
npm install --save-dev lint-staged
.eslintrc
and .stylelintrc
, etc., configs (see ESLint and Stylelint docs if you need help here).{ "lint-staged": "lint-staged" }
to scripts
section of package.json
."lint-staged": { "*.js": "eslint" }
to package.json
(see configuration).npm install --save-dev pre-commit
¹."pre-commit": "lint-staged"
to package.json
(top level, not the scripts
section).¹ I recommend using pre-commit or husky to manage git hooks but you can use whatever you want.
Now change a few files, git add
some of them to your commit and try to git commit
them.
See examples below.
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 formatlint-staged.config.js
file in JS formatSee cosmiconfig for more details on what formats are supported.
Lint-staged supports simple and advanced config formats.
Should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses minimatch for glob patterns.
package.json
example:{
"scripts": {
"my-task": "your-command",
},
"lint-staged": {
"*": "my-task"
}
}
.lintstagedrc
example{
"*": "my-task"
}
This config will execute npm run my-task
with the list of currently staged files passed as arguments.
So, considering you did git add file1.ext file2.ext
, lint-staged will run the following command:
npm run my-task -- file1.ext file2.ext
To set options and keep lint-staged extensible, advanced format can be used. This should hold linters object in linters
property.
linters
— Object
— keys (String
) are glob patterns, values (Array<String> | String
) are commands to execute.gitDir
— Sets the relative path to the .git
root. Useful when your package.json
is located in a sub-directory. See working from a sub-directoryconcurrent
— true — runs linters for each glob pattern simultaneously. If you don’t want this, you can set concurrent: false
verbose
— false — runs lint-staged in verbose mode. When true
it will use https://github.com/SamVerschueren/listr-verbose-renderer.It is possible to run linters for certain paths only by using minimatch patterns. The paths used for filtering via minimatch are relative to the directory that contains the .git
directory. The paths passed to the linters are absolute to avoid confusion in case they're executed with a different working directory, as would be the case when using the gitDir
option.
{
// .js files anywhere in the project
"*.js": "eslint",
// .js files anywhere in the project
"**/*.js": "eslint",
// .js file in the src directory
"src/*.js": "eslint",
// .js file anywhere within and below the src directory
"src/**/*.js": "eslint",
}
Supported are both local npm scripts (npm run-script
), or any executables installed locally or globally via npm
as well as any executable from your $PATH.
Using globally installed scripts is discouraged, since lint-staged may not work for someone who doesn’t have it installed.
lint-staged
is using npm-which to locate locally installed scripts, so you don't need to add { "eslint": "eslint" }
to the scripts
section of your package.json
. So in your .lintstagedrc
you can write:
{
"*.js": "eslint --fix"
}
Pass arguments to your commands separated by space as you would do in the shell. See examples below.
Starting from v2.0.0 sequences of commands are supported. Pass an array of commands instead of a single one and they will run sequentially. This is useful for running auto-formatting tools like eslint --fix
or stylefmt
but can be used for any arbitrary sequences.
Tools like ESLint or stylefmt can re-format your code according to an appropriate config by running eslint --fix
. After the code is re-formatted, we want it to be added to the same commit. This can be done using following config:
{
"*.js": ["eslint --fix", "git add"]
}
Starting from v3.1, lint-staged will stash you remaining changes (not added to the index) and restore them from stash afterwards. This allows you to create partial commits with hunks using This is still not resolvedgit add --patch
.
If your package.json
is located in a sub-directory of the git root directory, you can use gitDir
relative path to point there in order to make lint-staged work.
{
"gitDir": "../",
"linters":{
"*": "my-task"
}
}
All examples assuming you’ve already set up lint-staged and pre-commit in the package.json
{
"name": "My project",
"version": "0.1.0",
"scripts": {
"lint-staged": "lint-staged"
},
"pre-commit": "lint-staged"
}
Note we don’t pass a path as an argument for the runners. This is important since lint-staged will do this for you. Please don’t reuse your tasks with paths from package.json.
*.js
and *.jsx
running as a pre-commit hook{
"*.{js,jsx}": "eslint"
}
--fix
and add to commit{
"*.js": ["eslint --fix", "git add"]
}
This will run eslint --fix
and automatically add changes to the commit. Please note, that it doesn’t work well with committing hunks (git add -p
).
{
"*.css": "stylelint",
"*.scss": "stylelint --syntax=scss"
}
stylefmt
and add to commit{
"*.scss": ["stylefmt", "stylelint --syntax scss", "git add"]
}
{
"*.scss": [
"postcss --config path/to/your/config --replace",
"stylelint",
"git add"
]
}
FAQs
Lint files staged by git
The npm package lint-staged receives a total of 4,050,549 weekly downloads. As such, lint-staged popularity was classified as popular.
We found that lint-staged demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.