5

Recently I started checking all various JavaScript style guides and I liked Airbnb the best.

However, when I configured ESLint with Airbnb style guide, I noticed that some rules defined in their docs are not checked at all. For example:

  1. Group all your consts and then group all your lets.
  2. Use shortcuts for booleans, but explicit comparisons for strings and numbers.
  3. Use // for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment unless it’s on the first line of a block.

So, if you take this code as an example:

const array = [1, 2, 3, 4, 5];
let foo = 'foo';
const bar = 'bar';

if (array.length) {
  console.log('Bar:', bar);
  // Chaniging foo
  foo = 'baz';
  console.log('Foo:', foo);
}

The linter should throw 3 errors:

  1. I didn't group all consts and then all lets, I defined them sequentially.
  2. I didn't use explicit comparation in if statement - array.length > 0.
  3. I didn't add new line before my comment.

However, linter does not throw any error.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • What rules are you seeing it [specifies](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb-base/rules)? Do those rules include the ones you listed above? – Alexander Staroselsky Dec 01 '21 at 22:07
  • What should I check in your link? I just extended ESLint config with `airbnb-base` in my `.eslintrc.json`. – NeNaD Dec 01 '21 at 22:14
  • That link is for the logic of eslint-config-airbnb-base package. You can go through each file and check which rules are included by the package and then compare whether the rules you listed in the question are included by package. If not, you can always add these rules in addition to what comes with airbnb-base. Your question was "Does that mean that eslint-config-airbnb-base does not include all rules that Airbnb style guide suggests", you can use the source code to check what is included and what is not. – Alexander Staroselsky Dec 01 '21 at 22:16
  • I see. I don't know the names of ESLint rules that do checks from my example, so it is hard to check if they exist in the files from your link. Since `eslint-config-airbnb-base` is created by them, I thought that it should support all rules that they added in their docs. I randomly checked different rules to validate them and these 3 was not validated, so I guess there are probably more of them. – NeNaD Dec 01 '21 at 22:23

1 Answers1

2

Airbnb style guide is a recommendation on how to write code.
ESLint is a tool created by a separate team, specifically Nicholas C. Zakas in the first place.
So the recommendation utilizes ESLint to automate checks for some of the rules, but not all.

Some Airbnb style guide rules have trailing eslint: rule-name, which indicates if it is checked or not. All 3 rules from the topic don't have it.
Also there is a page that elaborates on new rules addition. It states: "As of 2020, we only accept rules related to new ECMAScript features. We prefer that new rules be implemented in plugins".

Nikita Skrebets
  • 1,518
  • 2
  • 13
  • 19
  • Your answer is not that helpful. Anyway, I will give you the bounty so it won't be wasted. – NeNaD Dec 11 '21 at 17:04
  • @NenadMilosavljevic thank you! Luckily I just found an eslint rule that should set a new line before comment https://eslint.org/docs/rules/lines-around-comment. Strange that Airbnb style guide did not mention it. Also, there may be other implementations of checks on other rules, e.g. in Prettier, but I could not find ones. – Nikita Skrebets Dec 12 '21 at 15:45
  • Yeah, I also found that one. But I though that all of these should be included in official airbnb plugin config, since these guides are included in the docs. – NeNaD Dec 12 '21 at 16:22
  • @NenadMilosavljevic Well, config is just a combination of available options that eslint provides. If there is no such option, like there is something that eslint cannot do (and probably shouldn't be able to by the authors opinion), there can be no such config. And probably Airbnb never tried to automate checks for all their recommendations. – Nikita Skrebets Dec 12 '21 at 17:44
  • Yeah, I agree... They can create plugin and add new rules on their own though... I just didn't thought of that when I asked the question. – NeNaD Dec 12 '21 at 20:04