简体   繁体   中英

ESLint rule for whitespace before single-line inline comment

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?

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.

No, unfortunately there is no rule for that in the list.

But if you use eslint with the eslint-plugin-prettier plugin, a missing space after a semicolon before a comment is flagged. prettier itself is not a linter, but an opinionated formatter, so configuration options for this plugin are limited. Basically, you like prettiers formatting style, or you don't.

To make prettier and eslint work together nicely, you can use eslint-config-prettier . This disables eslint rules, that conflict with prettier.

Two more alternatives:

  1. Implement your rule as an eslint plugin
  2. Raise an issue on GitHub. Maybe it's just a bug in the semi-spacing rule. https://github.com/eslint/eslint/issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM