Especially when dealing with an unfamiliar/legacy codebase, or just cleaning up a larger codebase, you may want to inspect all the comments. Comments tend to get stale and linger much longer than needed. It's a good idea to prune them as you code or do period tidying. That's what the RegEx. I wrote more about my love of RegEx, online prototyping and learning tools, and another use practical use case in the VS Code Search panel.
- single-line comments starting with
//
- comments can start at the beginning of the line or they can be added after code
- multi-line comments where every line starts with
//
for some reason 🤷♂️
// This will be detected
//so will this
alert(""); // this will also be detected
// This
// and this will be detected (as separate comments)
- multi-line comments or JSDoc-style comments using
/* */
- URL strings in the code (if they include
://
likehttps://
) inside code or in a multi-line comment. - Items marked
TODO
- Error suppressing comments starting with:
eslint
@
//TODO: this will not be detected
// TODO: neither will this
// @ts-ignore - this will not be detected
//@ts-ignore - this will not be detected
// eslint-disable - this will not be detected
//eslint-disable - this will not be detected
/*
This will not be detected
*/
/*
This sentence will not be detected. Neither will the URL
https://drecali.link/til
*/
Even though it's not a best practice to suppress errors, that feature exists for a reason and is handy when you're happy with your strict linting rules 99% of the time but need to intentionally break them in a unique situation that warrants it.
(?<!:)\/\/(?!(\s*eslint))(?!(\s*@))(?!(\s*TODO))
This RegEx finds patterns only if all the criteria below are met:
- Includes
//
- Does not include a colon (
:
) before the//
- Does not include
eslint
,@
, orTODO
with or without a space before it.
Many senior devs advised me against reinventing the wheel, so I initially tried to use a RegEx suggested by this StackOverflow answer. That one was really good at detecting edge cases in both both single-line and multi-line comments but it was more difficult to exclude the items I wanted to exclude.