Skip to content

Instantly share code, notes, and snippets.

@natterstefan
Last active April 2, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natterstefan/efa4cf22841ab14f09fa72bbbaf02f18 to your computer and use it in GitHub Desktop.
Save natterstefan/efa4cf22841ab14f09fa72bbbaf02f18 to your computer and use it in GitHub Desktop.
Lint-Staged - lint only staged files with eslint and stylelint

Lint-Staged - lint only staged files with eslint and stylelint

Based on the solution created by luuuis, I added two npm scripts lint-css-staged and lint-js-staged. They both lint (with ESLint and stylelint) only staged files.

You could also use pre-commit.sh from luuuis, if you do not want to put this into your package.json.

Prettier provides an example as well.

Licence

MIT

{
"name": "something-js",
"description": "Some package",
"version": "0.0.1",
"author": "Stefan Natter",
"license": "Apache-2.0",
"private": true,
"engines": {
"node": "^10.14",
"yarn": ">=1.13.0 <2.0.0"
},
"scripts": {
"lint": "yarn lint-css && yarn lint-js",
"lint-staged": "yarn lint-css-staged && yarn lint-js-staged",
"lint-css": "stylelint 'packages/**/*.js'",
"lint-css-staged": "git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' | xargs -0 -I % sh -c 'git show \":%\" | stylelint \"%\";'",
"lint-js": "eslint ./packages",
"lint-js-staged": "git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' | xargs -0 -I % sh -c 'git show \":%\" | eslint --stdin --stdin-filename \"%\";'",
},
"scripts/comments": {
"lint-staged": "lint only staged files"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"prettier --write",
"yarn lint-staged",
"git update-index --again",
"jest --findRelatedTests"
]
},
"devDependencies": {
"babel-eslint": "10.0.1",
"eslint": "5.16.0",
"eslint-config-ns": "0.3.0",
"eslint-import-resolver-lerna": "1.1.0",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-jest": "22.4.1",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-prettier": "3.0.1",
"eslint-plugin-react": "7.12.4",
"prettier": "1.16.4",
"stylelint": "9.10.1",
"stylelint-config-recommended": "2.1.0",
"stylelint-config-styled-components": "0.1.1",
"stylelint-custom-processor-loader": "0.6.0",
"stylelint-processor-styled-components": "1.6.0",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment