I'm looking for a way to have ESLint detect when there's a semicolon following an if statement. For example, code like `if (mytest); { myFunction(); }` should raise a flag. I'd also like it to handle one-line blocks where the braces are optional, such as: `if (myTest); myFunction();`. If there's a rule that extends this to WHILE/ FOR statements, that'd be a bonus! I know I could use grep to catch this, but I prefer having ESLint do it to avoid such errors before commits.
5 Answers
Check out this GitHub issue that discusses your problem: https://github.com/eslint/eslint/issues/13785. You can use the rule `'nonblock-statement-body-position': ['error', 'below']` to make it clearer when there's a mistake, like this: `if (mytest) ; myFunction();`. It's a good start!
New troll post format just dropped! Why not just ask some AI to whip up a custom linter for this mess?
What even is this JavaScript? Seems a bit questionable, right?
You might think grep could handle this, but it really can't in every situation. Regular expressions can't cover it perfectly. It's why compilers rely on parsers after the regex step!
Seriously, just write JavaScript like a normal person! Nobody wants to deal with code that looks that messy.
Thanks for the tip! I saw in the issue some alternative solutions too that might work well for me.