Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing conflict between Prettier (code formatter) and ESLint/TSLint

I am working on an Angular project in VS Code, using the "Prettier" plugin for code formatting, and the ESLint/TSLint plugins for standards enforcement.

I know this isn't an "error" but I strongly prefer clean builds and like to remove warnings whenever possible.

  groupMouseDown = d => {
   ...
  }; <--- This semicolon is the issue

The above is a function in Typescript, using the "fat arrow" syntax so that the function is inline. Notice that, at the end of the line, is a semi-colon.

I'm not here to start an argument about whether or not the semi-colon should be here, there are lots of those on the web. Rather, I just need to deal with it.

Prettier is considered an "opinionated" formatter, and their stance on the subject is that they believe it should be there. Which is fine, but they also refuse to add an option to NOT put it there, hence the "opinionated' part.

The linters on the other hand, believe that a semicolon should NOT be there, and so they flag it as an unnecessary semicolon. So far, I have not found an option to NOT report this as a warning unless I remove it for every case.

So, at the end of the day, I have to tools the disagree and leave me with no options to just ignore this and do what I want them to do, which is play nicely together.

Has anyone else come up against this? Is there a way to either: 1) Stop prettier from adding the semicolon ONLY IN THIS SPECIFIC CASE, or... 2) Stop the linters from reporting this as a warning ONLY IN THIS SPECIFIC CASE?

What I DON'T want to do is tell Prettier to not put ANY semicolons where needed, nor do I want to tell the linters to ignore ALL unnecessary semicolons.

like image 276
Todd Davis Avatar asked Jun 14 '19 19:06

Todd Davis


People also ask

Can Prettier and ESLint work together?

ESLint and Prettier files can conflict with each other because there are occasions when they end up checking the same rules which can lead to duplication. Fortunately, it is possible to get them both to work together.

How do you make ESLint and Prettier work together?

How to combine ESLint and Prettier. We will start by installing the Prettier and ESLint extension/plugin for your editor/IDE. For instance, in VSCode you can find Prettier and ESLint extensions on the VS Code Marketplace. It might be quite similar for your IDE/editor of choice.

Does Prettier conflict with ESLint?

One of the most common problem people are experiencing with Prettier/ESLint is having conflicting warnings and lot of red lining errors. A good way to avoid this problem is using Prettier as a ESLint plugin. Once it's installed, you have to tell ESLint to use Prettier as a plugin: module.

Should I use ESLint or TSLint?

TSLint can only be used for TypeScript, while ESLint supports both JavaScript and TypeScript. It is likely, within a large project that you may use both JavaScript and TypeScript.


1 Answers

No accepted answer here so I'll share my two cents :

I totally agree with @Josh on the fact that you should let prettier do its thing and make the linter agree with its style choices. However I don't want to depend on an additional plugin and the TSLINT article is a little bit too long to read so here's the specific rule your need to adjust :

If you're using TSLINT, you can add the following rule to your tslint.json file

  • "semicolon": [true, "always", "ignore-bound-class-methods"]
  • source: tslint documentation

I'm not using ESLINT (yet) so I don't know if the linter complains about this use case, and if it does I didn't find any option to deal with it

like image 90
Alfred Avatar answered Sep 24 '22 07:09

Alfred