noaignite / create-ignite-app

Boilerplate for React with Next.js and MUI
MIT License
17 stars 2 forks source link

[Husky] Run `yarn lint` on only staged files #46

Open adamsoderstrom opened 2 years ago

adamsoderstrom commented 2 years ago

We currently use a tool called husky to check if the project contains any ESLint errors. The tool runs on git commit, but performs before the actual commit has occurred.

Current results

If i have an unstaged file with ESlint error, the commit will not occur.

Expected results

Husky should ignore the unstaged files and only execute yarn lint on the staged files.

maeertin commented 2 years ago

Hi @adamsoderstrom and thanks for the ticket!

Not sure how to solve this correctly. Me and @alexanderflink looked into only running the lint-staged commands on currently git staged files. This however created problems on it's own where one was now able to commit files that could cause application errors. Imagine the following scenario:

// utils.js

export function someFunction() {
  ...
}
// App.js

import { someFunction } from 'utils'

someFunction()

The code above is currently commited to git. Now you decide to refactor the util called someFunction and while you're at it you also rename it to someOtherFunction because the functionality has changed. This would then leave you with the code below:

// utils.js

export function someOtherFunction() {
  ...
}
// App.js

import { someFunction } from 'utils'

someFunction()

Now you do a git status and the only modified file is the utils file that you just refactored so you decide to commit it. Our setup would then only run the lint-staged commands on currently staged files which would then validate as there are no errors in utils.js and the commit would go through. However, this is an issue because we are still importing the old someFunction in App.js and this file was not linted upon committing since that files was not staged.

alexanderflink commented 2 years ago

Maybe the solution to this is to fix lint-staged like we tried before so that only your staged files are linted, but to also have a hook so that everything is linted when you try to push your changes. That way you can have linting errors locally in unstaged files, but to be able to push, everything must be linted correctly.

maeertin commented 1 year ago

Actually that sounds like a super neat solution, let's look into this! @alexanderflink