Skip to content

Add --check option#5629

Merged
ikatyang merged 16 commits intoprettier:masterfrom
kachkaev:check-option
Dec 19, 2018
Merged

Add --check option#5629
ikatyang merged 16 commits intoprettier:masterfrom
kachkaev:check-option

Conversation

@kachkaev
Copy link
Copy Markdown
Member

@kachkaev kachkaev commented Dec 12, 2018

Closes #5520

  • I’ve implemented the functionality.
  • I’ve added tests to confirm my change works.
  • I’ve documented the changes I’ve made (see preview).
  • I’ve read the contributing guidelines.

WDYT folks?

@ikatyang
Copy link
Copy Markdown
Member

I think it'd be better to use a reporter-like solution:

prettier --reporter my-reporter.js
// my-reporter.js

module.exports = function ({ filename, source, formatted }, { logger }) {
  // ...
}

(It looks like we can even rewrite --write and --list-different as reporters.)

@ikatyang ikatyang added the status:wip Pull requests that are a work in progress and should not be merged label Dec 12, 2018
@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 12, 2018

Hi @ikatyang! 👋

Will a project owner have to add my-reporter.js to their code base? It will be unfortunate if so given that the idea of --check is to encourage usage of Prettier in CI while keeping things simple and accessible. The concept of a reporter make sense to me from the architectural point of view – it'd be great if Prettier could come with a couple of easily invokable reporters.

One more thought: implementing --check with its humane output requires printing a summary line before paths to files. This means that the reporter's signature would have to support that.

Optionally, we can park the concept of reporters till 2.0 as suggested by @lydell in #5520 (comment) and simply focus on giving users a simple working solution.

@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Dec 12, 2018

I’m wondering if --check should display a diff, as requested in #4612.

@ikatyang
Copy link
Copy Markdown
Member

I meant that the reporter could be a sharable module just like eslint --format <my-format>. And of course there should be some built-in reporters.

Optionally, we can park the concept of reporters till 2.0 as suggested by @lydell in #5520 (comment) and simply focus on giving users a simple working solution.

I'm fine with either way as I think we probably need to rewrite our API/CLI in 2.0, and it does not look like it'll happen soon.

@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 12, 2018

@j-f1 good point about the --diff@ikatyang's reporter suggestion makes even more sense with it!

@ikatyang:
I'm fine with either way as I think we probably need to rewrite our API/CLI in 2.0

2.0 feels like a good time to reconsider the API design, so perhaps the introduction of custom / built-in reporters can be done later. Showing diffs fits the idea of reporters quite well, but I'm not sure that it should be enabled by default. I was recently helping to switch one rather large project into Prettier, which involved adding prettier-check to the pipeline. If we were shown Prettier diffs during the transition period, a message saying Forgot to run Prettier? would turn into a needle in a massive haystack 😅

The main use case for --check as I see it is in CI to avoid merging unformatted code into master. Making a simple variation of --list-different should add enough value for now IMO as it will propagate the right habit across the community. In 2.0, --check and --list-different can be merged due to a shift to the reporters API, but either way this won't be a painful change.

Let's wait for objections to what I wrote in the docs for a few more days and I'll go for implementing a minimal change, which will make this feature real 😉

@lydell
Copy link
Copy Markdown
Member

lydell commented Dec 12, 2018

I'm 👍 on the stuff described in the docs.

@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 15, 2018

RFC 🕺 A few notes:

I had to change the proposed output from

Forgot to run Prettier? There are files without correct code style:
src/fileA.js
src/fileB.js

to

Checking formatting...
src/fileA.js
src/fileB.js
Code style issues found in the above file(s). Forgot to run Prettier?

This because the file list is constructed asynchronously and it would be challenging to "hold" it until all formatting is done in order to print the summary message above. The docs are updated accordingly. prettier-check, which inspired me, spawns Prettier using execa and this is what lets it gather the list before producing the final output. Streaming the list as it gets populated is potentially beneficial in large projects.


The implementation of --check is rather simple and I would even say boring. No massive refactoring or new abstractions, only minimal necessary changes to do what the new docs promise. Hope that this is fine, especially given a possibility of switching to reporters in 2.0.

Added tests are also quite boring – I just duplicated the ones for --list-different. New kinds of tests include:

  • making sure that --check and --list-different cannot be used together
  • making sure that -c works just like --check (and -l just like --list-different, for symmetry)

I noticed a tiny glitch in output with --list-different + unformatted differs when piped – the test was returning unformatted.jsunformatted.js. This is now fixed.

See below

@kachkaev
Copy link
Copy Markdown
Member Author

Would it be reasonable to assign this feature to 1.16 milestone? 🤔 It'd be great if it was released together with the next batch of perks! ✌️

@j-f1 j-f1 added this to the 1.16 milestone Dec 16, 2018
@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Dec 16, 2018

What do you think about displaying a line with the filename currently being formatted that gets deleted and rewritten if the file passes? This would give the user more feedback so it wouldn’t look like Prettier was frozen.

@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 16, 2018

Good point @j-f1! I've implemented this. The filename currently being formatted is also shown in TTY for --list-different now, for symmetry with --check. This should not be a breaking change.

@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 16, 2018

Just tried yarn add -D kachkaev/prettier#check-option in one project and replaced prettier-check with prettier --check. All worked as the new docs say! 🎉

@kachkaev
Copy link
Copy Markdown
Member Author

kachkaev commented Dec 17, 2018

We can probably remove status:wip if this helps with getting more reviews or with merging 😉

@duailibe duailibe removed the status:wip Pull requests that are a work in progress and should not be merged label Dec 17, 2018
@ikatyang ikatyang merged commit 711c6d7 into prettier:master Dec 19, 2018
@ikatyang
Copy link
Copy Markdown
Member

Thanks!

@daKmoR
Copy link
Copy Markdown

daKmoR commented Dec 20, 2018

this is not released yet right?

it's a little confusing as it's already shown at https://prettier.io/docs/en/cli.html - I just wanted to set this up and found out that it's not working

@lydell
Copy link
Copy Markdown
Member

lydell commented Dec 20, 2018

That’s true, we should comment out the docs.

@kachkaev
Copy link
Copy Markdown
Member Author

@daKmoR thanks for reporting this problem with the docs – it's truly confusing. There is a temporary fix in #5671 as well as a discussion of how to prevent the problem in future.

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Mar 21, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants