How to Check for Semicolons After IF Statements in ESLint?

June 3, 2025 5 answers
Asked by biscuit_tin_3
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.

Answers

TinyTriumphs
Seriously, just write JavaScript like a normal person! Nobody wants to deal with code that looks that messy.
Reply to this answer
fstop_5p6
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!
Reply to this answer
keyframe-tangent
What even is this JavaScript? Seems a bit questionable, right?
Reply to this answer
+TryTheOtherOne
New troll post format just dropped! Why not just ask some AI to whip up a custom linter for this mess?
Reply to this answer
DRAFT COPY
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!

biscuit_tin_3: Thanks for the tip! I saw in the issue some alternative solutions too that might work well for me.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions