Aborting the commit with husky and lint-staged if linting produces warnings or errors?

  Kiến thức lập trình

is there a way to abort a commit with lint-staged if linting produces warnings or errors?

I’m trying to set up the Lit Element Typescript starter project so that it will abort a commit if the linting produces warning or errors:

git clone [email protected]:lit/lit-element-starter-ts.git 

cd lit-element-starter-ts
npm i -D husky && npx husky init

This command is from the prettier documentation and it initializes the lint staged configuration.

npx mrm@2 lint-staged

The .husky/pre-commit file can now run lint staged and it should look like this.

npx lint-staged

So we can update the package.json lint staged configuration so it runs the lint script that comes with the lit element typescript starter like this:

  "lint-staged": {
    "*.ts": "npm run lint"
  }

And to test it we can introduce an unused variable into my-element.ts like this.

  override render() {
    let unused;
    ...

That produces a linting warning, but if we run:

git add . && git commit -m "chore: test that it won't commit" 

The lint warning does not abort the commit.

I’d like to husky to abort the commit, the same way it does if tests fail when we have npm test in the pre-commit script.

Thoughts?

8

LEAVE A COMMENT