Please forgive me if this issue turns out to be a duplicate. I did a couple of searches through other issues (both closed and opened) with some keywords that came to mind and found nothing relevant.


Question

Is there a way to run prettier in a “diagnostic” sort of way where it outputs the issues it finds with your code? The desired result would be something along the lines of what ESLint does. Consider the following screenshot.

image

Background Info

The reason I’m asking this? I want to make sure all code in my repository is run through a linter and/or style checks pre-commit. However simply running prettier with the --write flag comes with one big issue: https://github.com/okonet/lint-staged/issues/62. In short: Running prettier --write changes the code if it finds an error, this fixed code should be added to the same commit. Simply running git add on the changed file adds all of the contents of that file to the commit, even if you only wanted to commit a part of that file. (Read https://github.com/okonet/lint-staged/issues/62 for details…)

As a workaround I decided that just running all the linters and style-checks would be good enough. If a linter or style check fails the commit won’t be made and the developer can then manually deal with the problems.

I got this part working. Turns out running prettier with the --list-different flag will exit with a non-zero exit code if it finds any problems. On top of that the command will (as the flag suggests) list all of the files that have style problems in them.

But this doesn’t make for the best developer experience. Telling the developer “prettier found some problems” and then just listing a some file(s) seems a bit confusing. Consider the following screenshot.

screenshot

Hopes and Dreams

In a perfect world I would want prettier to have the option to provide a detailed report of the issues it has found and the rules that are broken.

prettier --check

Alternative

Alternatively I think you could improve the developer experience by adding some format & extra information to what --list-different already does and call that --check. The following code snippet would be an example printout.

prettier --check

Prettier found some issues in the following files:
  - somefile.js
  - css/styles.css
  - README.md

Run 'prettier --fix' to automatically fix these issues.

I know it is possible to add prettier as a plugin for ESLint—but that doesn’t work for all cases, since ESLint only works for JavaScript files (as far as I know) and prettier does more than that.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ikatyangcommented, Jul 30, 2018

Added in #4914. I guess this issue can be closed then?

2reactions
jessevdpcommented, Jul 30, 2018

precise-commits doesn’t solve all my problems—it does solve the problems related to prettier, but not the other linters (that too have --fix like options). And since I want a unified approach…

Anyway, prettylint seems to do the trick indeed! Thanks! I did a quick search both through the README and the related projects page. Neither of them seems to mention prettylint. Perhaps it should be added somewhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

CLI - Prettier
When you want to check if your files are formatted, you can run Prettier with the --check flag (or -c ). This will...
Read more >
prettier-check - npm
Check that all files match prettier code style.. Latest version: 2.0.0, last published: 5 years ago. Start using prettier-check in your ...
Read more >
CLI | Dev Cheatsheets - Michael Currin
See the Prettier CLI page of the Prettier docs for more info. ... Print the names of files that are different from Prettier's...
Read more >
If list-different detects differences, it should print command for ...
Alternative 3. prettier --check "**/*.{css,html,js,md,yml}" Checking formatting...
Read more >
How to Format All Files in a Directory with Prettier
From the directory you want to format, run Prettier with --write : prettier --write . This will format the entire directory recursively with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found