-4

We have implemented a coding standard for the company for nodejs backend projects and React js front end projects where a maximum number of lines for a file should be less than 250. We want to prevent developers commit files where the lines count is greater than 250.

How do we automate this other than manually review commits?

margherita pizza
  • 6,623
  • 23
  • 84
  • 152
  • If I understand correctly your projects are written in js, so you could use eslint's pre-commit: https://levelup.gitconnected.com/how-to-run-eslint-using-pre-commit-hook-25984fbce17e – KiprasT Jul 27 '21 at 07:05
  • So, a class implementation can't have more than 250 lines of code to it without artificially putting some code in another file and then importing it - done for no other reason than the 250 line limit? What is the real motivation for this limit? What about lookup tables of Javascript data? – jfriend00 Jul 27 '21 at 07:20

2 Answers2

2

Eslint has a rule that enforces a maximum number of lines per file. If you include this in your Eslint coding standards, it will fail if your files exceed this limit. You can combine this with a pre-commit Git hook to run the coding standard on each commit.

Unfortunately Git hooks aren't considered part of the repository and therefore aren't recreated when the repository is cloned. It's a good idea to also have ESLint run in a continuous integration environment so any breaches of the standard are caught when someone pushes too.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
1

On the "client/developer" side you use husky to setup pre-commit hooks to run the check.

Eslint has a max-lines rule that you can use.

For performance reasons you may want to use lint-staged to only run eslint on changed files.

Of course this is all done "on the client side" and you have to trust developers to have the tools installed and configured correctly and to not disable them. If you want to be more sure, you can run eslint as part of the CI and make it fail if it detects any error.

npretto
  • 1,098
  • 7
  • 18