lint-staged
lint-staged is a widely adopted Git tool that executes commands against staged git files to prevent erroneous code from entering the repository.
Installation
You have to install lint-staged first. Currently lint-staged v15 is supported.
npm install lint-staged -D
yarn add lint-staged -D
pnpm install lint-staged -D
bun install lint-staged -d
Configuration
ESM
// lint-staged.config.mjs
// or lint-staged.config.js with "type": "module" in package.json
import { lintStaged } from '@modyqyw/fabric';
// or
// import { lintStaged } from '@modyqyw/fabric/lint-staged';
export default lintStaged();
CJS
// lint-staged.config.cjs
// or lint-staged.config.js without "type": "module" in package.json
const { lintStaged } = require('@modyqyw/fabric');
// or
// const { lintStaged } = require('@modyqyw/fabric/lint-staged');
module.exports = lintStaged();
Customization
Parameters
Passing parameters to the exported lintStaged
method can customize, and the lintStaged
method takes two parameters.
The first parameter is used for basic customization, you can pass either undefined
or an object. To explicitly enable or disable a plugin, you need to explicitly set the boolean value in the passed object.
The following plugins are currently supported:
- eslint - Lint scripts with ESLint. Enabled by default if you have ESLint installed.
- lintJsonc - Lint JSON files with ESLint when ESLint is enabled. Enabled by default.
- lintYml - Lint YML files with ESLint when ESLint is enabled. Enabled by default.
- oxlint - Lint scripts with oxlint. Enabled by default if you have oxlint installed.
- stylelint - Lint styles with Stylelint. Enabled by default if you have Stylelint installed.
- markdownlint - Lint markdown files with markdownlint. Enabled by default if you have markdownlint-cli installed.
- prettier - Format with Prettier. Enabled by default if you have Prettier installed.
- formatChangelog - Format CHANGELOG.md with Prettier when Prettier is enabled. Disabled by default.
// lint-staged.config.mjs
// or lint-staged.config.js with "type": "module" in package.json
import {
hasESLint,
hasMarkdownlintCli,
hasOxlint,
hasPrettier,
hasStylelint,
lintStaged,
} from '@modyqyw/fabric';
export default lintStaged({
eslint: hasESLint,
jsonc: true,
markdownlint: hasMarkdownlintCli,
oxlint: hasOxlint,
prettier: hasPrettier,
stylelint: hasStylelint,
yml: true,
});
The second parameter is used for further customization, you can pass an object to override the generated configuration.
// lint-staged.config.mjs
// or lint-staged.config.js with "type": "module" in package.json
import { lintStaged } from '@modyqyw/fabric';
export default lintStaged(undefined, {
// operations that require customization
});
Integration
simple-git-hooks
If you are using the simple-git-hooks configuration provided by the package, see the simple-git-hooks chapter.
If you are not, you can refer to the following configuration.
// simple-git-hooks.cjs
module.exports = {
'pre-commit': 'npx lint-staged',
};