reddit / emacs

Catch you JS console.log you forgot to remove with Emacs and Magit

AndreaSomePostfix · 2022-11-15 20:12
7890yuiop · 2022-11-15 22:42
Fun. That's really what `pre-commit` git hooks are for though -- and they work even when you're not using Emacs / Magit. Lots of people write *bad* pre-commit hooks, mind you (if a hook will give staged bugs a pass in the presence of unstaged fixes, then it's a bad hook). A good hook tests what will be *committed*, rather than merely what is in the working copy at that time, most likely using either `git checkout-index` to obtain copies of the staged files in their staged state, or using `git stash` (with all appropriate care) to temporarily remove un-staged changes. Your approach goes a step further in not caring about pre-existing debug calls elsewhere in the file you're modifying (with git hooks I use marker comments to flag things which are actually ok and should be ignored), so I can absolutely see the appeal to that (and of course if it's just for you, then an Emacs-centric solution is totally fine :) If you're working with others, though, I would recommend focusing the effort on git hooks. Either way it's neat, though (the button usage is definitely a nice touch).
redguardtoo · 2022-11-15 23:48
You could run eslint cli to check&fix the errors (see https://eslint.org/docs/latest/rules/no-console)
lvng · 2022-11-16 07:40
regarding the "false positive" you could use git diff --cached "**.js" or to filter whole repository independent of location git diff --cached "$(git rev-parse --show-toplevel)/**.js" ** is part of zsh or bash >=4
AndreaSomePostfix · 2022-11-16 10:00
Thanks, what you say makes a lot of sense. This is for me because it is funny any JS project I collaborate with ends up having in the review: "Can you remove that console log". So at least I stop bothering people. I definitely under used git hooks: I will study them more thanks for mentioning it!
AndreaSomePostfix · 2022-11-16 10:01
thanks, that is cool! I was curious to see if I could check only for my staged changed, so I played a bit around with git in this post :)
AndreaSomePostfix · 2022-11-16 10:03
Oh amazing! I didn't realize I could filter files. I will look into that because I need a regexp now that I think about it (I work with jsx and tsx as well :P) Thank you!