ESLint rule for whitespace before single-line inline comment

Viewed 1235

I'm looking for something that I thought would be standard in ESLint rules but somehow can't find in the list of rules.

There is the spaced-comment rule that enforces a space immediately after the opening // of single-line comment.

I would like to enforce a white space immediately before an inline comment.

test(); // This would be allowed
test();// This wouldn't be

Is there a rule I'm not seeing for this?

1 Answers

Unfortunately, as the other answer mentions, ESLint does not have a rule for requiring space before the double fwd slashed // at the beginning of a line comment. only allows you to set the amount of spaces after the double fwd slashes: //. ESLint v8.0 is a really exciting update, therefore I have read what it includes, and I know that it doesn't include a space before a comment, consequently, it would be reasonable to assume that ESLint leaves this small tidbit up to formatters, or the user, if that user does not use a formatter.



There are two solutions, but neither include an ESLint rule:

  • The first is extremely obvious, but more common than I once thought — Lint your code then format your code manually using your mouse and eyeballs.

  • The second is is probably the most popular option: use Prettier, __FYI: You Don't Need A Plugin for This". Prettier is biased, and spaces bore a inline comment starts is an unchangeable part of the Prettier Style Guide. Many people avoid prettier, because it use to be an extremely arduous task to add both ESLint & Prettier into the same project and achieving conflictless harmony between the two ECMAScript tools, however, the contemporary configuration required for the two is far easier to setup because of new extensions & plugins that have been created. If you use VSCode, you just need to install the ESLint & Prettier-ESLint extensions, plus you need to NPM install ESLint & Prettier as development environment extensions.



> "The Above is probably not exactly what you were looking for, but I felt it was worth mentioning it, because even if you already were aware of all I wrote, at least it confirms that where you stand, is where the Linter you are using also stands."

Its important to note that a linter is not a formatter, and visa versa, this is why people use both.

Related