Git – checking JavaScript files for debugger statements

We developers, when working on a common codebase, often step on each other’s toes. Recently, while debugging code of a complex application server I have been hit with JavaScript debugger statements placed by another developer all around, that should not get to the common branch. So what is better than complaining to your colleagues to be more careful every time they commit code? Present them with a Git hook that does the checking for them and solves the problem once and for all 🙂 This hook is an expansion of the previous one.

The previous hook only checked the branch name against a pattern to avoid unintended commits to stable branches. This hook gets a list of files in a commit, picks only .js files and searches them for “debugger” string. If such files are found, the script lists them and asks what to do:

  • commit anyway
  • cancel the commit
  • commit and remove “debugger” statements

I avoid breaking someone else’s workflows as far as possible, so there is still an option to just commit the code. I could also implement a server-side pre-push hook that would do the checking and simply reject unwanted stuff, but that would force the developers to reset their branches and do the cleaning themselves. I think that problems are best dealt with as early as possible, so this hook is executed in the pre-commit phase.

The last option is especially useful, because it saves the time needed to walk through all the files and clean code manually. Replacing the “debugger” string with a semicolon is probably the safest option. As a side-effect the script will also do replacements in comments, but that should not be a big problem.

Rate this post