Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to ESLint and apply Airbnb JavaScript Style Guide #150

Closed
wants to merge 7 commits into from
Closed

Migrate to ESLint and apply Airbnb JavaScript Style Guide #150

wants to merge 7 commits into from

Conversation

IGassmann
Copy link

This PR migrate from TSLint to ESLint since the former has been deprecated.

It also configures ESLint and Prettier to align themselves with the Airbnb JavaScript Style Guide, which is the most popular style guide. It is well documented and its rules have been thoroughly discussed by the community. Thanks to typescript-eslint it can also be used with TypeScript.

After merging this, the idea is to normalize the linting configuration between the different Nest official projects and documentation as discussed here.

@@ -0,0 +1,5 @@
module.exports = {
singleQuote: true,
printWidth: 100,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove printWidth option here? (and leave the default value)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule being followed here is https://airbnb.io/javascript/#whitespace--max-len.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but at the same time, 120 is the default option proposed by prettier. So far, we've used prettier's one so I think that we shouldn't change this.

package.json Outdated Show resolved Hide resolved

@Controller()
export class AppController {
export default class AppController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default exports are not recommended with Nest and should be permitted rather than suggested

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getHello(): string {
return 'Hello World!';
return this.helloWorld;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we revert this change to stay consistent with all tutorials & guides & courses on the web?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you would like to turn off this eslint rule?
https://eslint.org/docs/rules/class-methods-use-this

@IGassmann
Copy link
Author

Concerning the changes in the code style. I've adjusted the code to follow the rules of the Airbnb Style Guide.

When adopting a style guide, I highly recommend not tweaking the rules of the guide to fit personal preferences. The whole point of adopting a guide is consistency and to get rid of bikeshedding.

Many organizations and projects use a style guide and are intransigent with their specified rules. Google is an example of such organization:

On matters of style, the style guide is the absolute authority. Any purely style point (whitespace, etc.) that is not in the style guide is a matter of personal preference. The style should be consistent with what is there. If there is no previous style, accept the author’s.

Source: https://google.github.io/eng-practices/review/reviewer/standard.html

@kamilmysliwiec
Copy link
Member

@IGassmann I haven't proposed changes that are in line with my preferences, but the framework recommendations according to the documentation and the entire ecosystem. default shouldn't be used.


async function bootstrap() {
async function bootstrap(): Promise<void> {
Copy link
Member

@kamilmysliwiec kamilmysliwiec Sep 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<void> doesn't bring any relevant & useful information here. I know that it's forced by the Style Guide, but I doubt we should follow this. TypeScript type inference is sufficient in such scenarios.

@IGassmann
Copy link
Author

IGassmann commented Sep 11, 2019

@IGassmann I haven't proposed changes that are in line with my preferences, but the framework recommendations according to the documentation and the entire ecosystem. default shouldn't be used.

@kamilmysliwiec Yes. I should have written preferences in general.

The point I'm trying to communicate is the value of having a documented style guide that we can use as a reference and not break its rules. That would mean updating the style of the projects and documentation to align with the style of the guide.

Another proposition I would have if we want to keep some existing styles is to write a Nest Style Guide and build an ESLint plugin or adopt another style guide that is closer to Nest's style and also has the tools ready. Maybe Google's one would be a viable solution. It is adopted by the Angular projects.

Personally, I would recommend adopting Airbnb's one, even if that means slightly changing the current style of the projects. I believe the internet doesn't need one more JavaScript style guide and we would be able to take advantage of the already present tools around this style guide.

@kamilmysliwiec
Copy link
Member

It doesn't make too much sense to create a dedicated style guide if we just want to override a few rules to make all repos & documentation consistent. I do agree with the majority of AirBnb rules, although, some decisions aren't the best and we shouldn't just stick to it because the inherited style guide enforces them. Both eslint and tslint allow to override rules and that's what makes them powerful. Obviously, we should reduce the number of them as much as possible, but people eventually will use either our CLI, typescript-starter, samples from the main repo or docs to start their projects. If we ensure that all these repos share the same eslint file with the same set of rules - that would be totally fine, regardless of a couple of modified rules.

@IGassmann
Copy link
Author

IGassmann commented Sep 12, 2019

@kamilmysliwiec Yes, we could override those rules and we would still solve the problem of consistency since projects would share the same rules.

However, by overriding those rules we would sacrifice the value of having a fully documented style guide. Because our rules wouldn't be anymore following the Airbnb ones completely. That also gives precedence to allow more divergence and discussion about rules in the future.

The value of a documented style guide is that it reduces bikeshedding. If in the future someone starts a trivial argument about one style or another, we can point to the documentation and indicate that it is the source of truth. There the person will find examples and the reason behind the rule. Then we can spend more time on things that matter more.

Let me know if the value of the documentation makes sense to you.

If you feel strong about the currently used style preferences, we could also fork the Airbnb Style Guide and write our own guide.

@kamilmysliwiec
Copy link
Member

As I said above:

It doesn't make too much sense to create a dedicated style guide if we just want to override a few rules to make all repos & documentation consistent.

Regarding this:

However, by overriding those rules we would sacrifice the value of having a fully documented style guide. Because our rules wouldn't be anymore following the Airbnb ones completely. That also gives precedence to allow more divergence and discussion about rules in the future.

I don't have anything against custom rules. As I mentioned here:

Both eslint and tslint allow to override rules and that's what makes them powerful.

Every organization can override rules, adjust them to their preferences - and it's totally fine.

What we can do, as a framework, is to remain consistent across resources that we provide (CLI, samples, repos, documentation) regardless of whether we will override 0 or 5 rules from the original doc. Furthermore, if anyone wants to override our rules/add even more rules within the organization, it's not a big deal. We provide recommendations, suggestions, and they will be clearly visible in all locations + editable from the configuration files. However, we cannot force them to use these rules anyway.

To recap, we definitely won't create our own style guide because that would create even more divergence in the ecosystem. At the same time, I don't see any issue in overriding a few rules which are useless, especially this one with default imports which has been in doubt (even for AirBnb style guide users) for a long time.

@IGassmann
Copy link
Author

Thanks for keeping answering. I appreciate the discussion.

I will try to restate what you're saying to be sure I'm understanding your thinking. Please correct me, if I misinterpreted you.

As I understand, you're okay with us (the Nest community) not having a documented style guide that serves as a source of truth that we can point to.

If that is right, does my bikeshedding argument makes sense to you?

Every organization can override rules, adjust them to their preferences - and it's totally fine.

What we can do, as a framework, is to remain consistent across resources that we provide (CLI, samples, repos, documentation) regardless of whether we will override 0 or 5 rules from the original doc. Furthermore, if anyone wants to override our rules/add even more rules within the organization, it's not a big deal. We provide recommendations, suggestions, and they will be clearly visible in all locations + editable from the configuration files. However, we cannot force them to use these rules anyway.

Totally agree with you here. I'm not suggesting to enforce organizations to follow our style.

To recap, we definitely won't create our own style guide because that would create even more divergence in the ecosystem.

Agreed. That's why I believe it is better to follow the Airbnb one. But to be clear, by follow, I mean follow strictly. If we a break a few rules, the guide ceases to be a source of truth and then all rules can be debated, and we're back at bikeshedding.

At the same time, I don't see any issue in overriding a few rules which are useless, especially this one with default imports which has been in doubt (even for AirBnb style guide users) for a long time.

The issue being is that we lose the source of truth. I also wouldn't say that they necessarily are useless. Although, the value of some of those rules is an endless debate. I personally prefer, named exports for example. But there are good arguments for preferring default exports. But it is most of the time an unproductive discussion to debate about these things. That's where one of the main values of a style guide resides, like well explained by Standard JS's FAQ:

I disagree with rule X, can you change it?

No. The whole point of standard is to save you time by avoiding bikeshedding about code style. There are lots of debates online about tabs vs. spaces, etc. that will never be resolved. These debates just distract from getting stuff done. At the end of the day you have to 'just pick something', and that's the whole philosophy of standard -- its a bunch of sensible 'just pick something' opinions. Hopefully, users see the value in that over defending their own opinions.

@BrunnerLivio
Copy link
Member

@kamilmysliwiec wouldn't it make more sense to wait until Angular migrates to ESLint? NestJS can run in an Angular project, therefore we would want to have the same ESLint ruleset. I'd rather wait until they have published one before we start doing our own thing. angular/angular#13732

@kamilmysliwiec
Copy link
Member

@BrunnerLivio Nest can also run in React and Vue project 😄 Anyway, I think I got what you mean. We can wait a little to see which route Angular will take for some additional inspiration.

@IGassmann
Copy link
Author

@BrunnerLivio @kamilmysliwiec makes sense for me to wait for Angular's migration. They will probably set it up with the Google JavaScript Style Guide rules, which is also a well-documented style guide.

@pumano
Copy link

pumano commented Sep 25, 2019

@IGassmann you going to good direction, but airbnb use react where export default is preferred. Angular not use default exports. You should explicitly import what you need. Its reason, why that rule not fit here. NestJS currently going tightly with Angular.

@IGassmann
Copy link
Author

@pumano yes, I could see how Google's style guide would be a better fit if the Nest wants to align with the Angular project.

However, Google's style guide has some questionable decisions too like requering JSDoc for all classes, methods, and functions. Other things are also different from what Nest has been doing, like asking for annotation to be written in camelCase.

One could also argue that Nest is agnostic of frontend frameworks, and then there's no necessity for it to be aligned with Angular style.

@THPubs
Copy link

THPubs commented Nov 13, 2019

Hey guys, shall we finalize this? If there's a debate in which one to use, Google or Airbnb, you can refer to this article: https://medium.com/@uistephen/style-guides-for-linting-ecmascript-2015-eslint-common-google-airbnb-6c25fd3dff0 or some others around there. I personally go with Airbnb since it's the most popular one and also since it have more React support. But it's up-to you.

If @kamilmysliwiec want to modify it, I'm completely ok with it since it does look like this framework need some modifications. But the thing is, tslint have announce their deprecation February this year. Now we have waited too long for it's adoption which is not good at all. We can't wait for Angular guys. So, shall we come to any common agreement and move forward with this? @IGassmann If you need any help I'm free. 🙂 Will it be ok if I help with the changes?

@IGassmann
Copy link
Author

IGassmann commented Nov 13, 2019

I believe it's still okay for us to wait. TSLint is still accepting PRs for bug and security fixes. However, I also understand the need to get this done, so new NestJS projects won't need to make the switch to ESLint themselves.

The angular project might be preparing an ESLint configuration that would accommodate their style guide, which ain't exactly the Google one. So it might be useful for us to follow this new configuration.

As a note, we have adopted internally the Airbnb Style Guide since September for our NestJS project. It's been working fine, even though there are some divergences with the Angular rules, such as the use of default exports.

@THPubs
Copy link

THPubs commented Nov 13, 2019

@IGassmann No offense but this is not an Angular project. This is a back-end framework used by many other developers as well. I agree this project follow many good standards set by Angular. But that does not mean we need to follow them in all the other cases. Also note that Angular focus on Front end and this focus on the Back end.

@IGassmann
Copy link
Author

@THPubs no offense taken. I'm bringing Angular, because the project was inspired by it and many have come from it. I personally don't use Angular.

In my own opinion, I would adopt Airbnb, or create a custom one, but have it documented somewhere by forking the documentation of an existing one.

@kamilmysliwiec
Copy link
Member

@THPubs there is a good chance we won't follow exactly the same set of rules as Angular. However, since they already started working on this, it would be great to ensure our ESLint configurations are as close as possible to each other. Let's be patient, they should release it shortly.

@trylovetom
Copy link

Any update bro? It's 2020 now.

@evilsprut
Copy link

Switching to ESLint is a forced measure because TSLint will not be updated soon (sadly, ESLint is much slower). But Airbnb rules are a bad idea, I think. It has a lot of front-end specific rules and it doesn't let you work quietly. Instead of focusing on writing code, you have to worry about satisfying Airbnb rules. Can we make our own rules for Nest based on Airbnb experience?

@zbeyens
Copy link

zbeyens commented Jan 9, 2020

I would extend airbnb as it nearly became a convention to do it. Then I just disabled the unsatisfying rules. I am now using the same eslint config for my front-end and back-end without any conflict.

@IGassmann
Copy link
Author

Extend/Fork Airbnb's would be a good strategy. I would also add TypeScript specific rules since Airbnb targets only JavaScript. Nest would then have its own set of rules and style guide like other frameworks out there.

@kamilmysliwiec
Copy link
Member

Let's track this here #161

Initially, we wanted to wait until Angular publishes a new release. Unfortunately, Angular 9 (new major release) is already a few months delayed, and the migration (from tslint to eslint) has been postponed until Angular 10.

More information in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch to ESLint?
9 participants