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

Add check method to Prettier Node API. #1104

Merged
merged 4 commits into from Apr 3, 2017
Merged

Conversation

btholt
Copy link
Contributor

@btholt btholt commented Mar 30, 2017

Hi friends!

I figured this was better as a PR instead of an issue. We'd like to adopt Prettier and part of that is build it into our continuous integration. We run everything through Gulp (hence why I also forked btholt/gulp-prettier too) which makes use of the Node API. The list-different which is available to the CLI is precisely what we need but since that lives in the bin and not in the Node library it can't be used. This moves this logic back into the library exported from index.js as a tiny function that just makes use of the format function.

Is it very hard to keep ahold of the old string and compare it with the new string from Prettier? No, definitely not. This is what I did with my fork of gulp-prettier (which I borrowed the idea from facebook/react#9187). But I figured if my use case needed this as well as there's that it may be useful to support this use case in Prettier.

Let me know what you think and/or what I can fix!

index.js Outdated
@@ -95,6 +95,9 @@ module.exports = {
format: function(text, opts) {
return formatWithShebang(text, normalizeOptions(opts));
},
check: function(text, opts) {
return this.format(text, opts) === text;
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems weird that if it format throws (syntax error on the input), the check function would throw instead of returning false.

bin/prettier.js Outdated
} catch (e) {
// Add newline to split errors from filename line.
process.stdout.write("\n");
handleError(filename, e);
Copy link
Contributor

Choose a reason for hiding this comment

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

we actually don't want to print the error here, we just want to print all the filenames that are different. It's up to the user to actually run prettier on all those files to see what's going on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, looks like the existing implementation does it incorrectly, good thing you are looking into it :)

@vjeux
Copy link
Contributor

vjeux commented Mar 31, 2017

Oh man, crazy that you are looking into adopting it at Netflix!

That seems like a good idea to me. Could you fix my comments around error handling and this should be good to go. Given that the function can throw and you need to handle it more carefully, I think it makes sense to have a check function for it instead of pushing this unexpected complexity to the call sites.

@btholt
Copy link
Contributor Author

btholt commented Apr 3, 2017

@vjeux Great suggestions in your comments; I fixed your feedback. Did you want me to fix how write is done as well?

bin/prettier.js Outdated
process.exitCode = 1;
}
} catch (e) {
// should never hit this block since check catches errors before it returns
Copy link
Contributor

Choose a reason for hiding this comment

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

if it never reaches this, can you just remove the entire try catch? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, fair. I'm just used to providing for IE8. There's a pretty slim chance that this will be run in IE8. :D

bin/prettier.js Outdated
@@ -232,6 +232,19 @@ if (stdin) {

let output;

if (argv["list-different"]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put this just above const start = Date.now(); since we don't really need those two variables to do that block of code.

@vjeux
Copy link
Contributor

vjeux commented Apr 3, 2017

write is fine. We do want it to throw :)

bin/prettier.js Outdated
@@ -228,6 +228,16 @@ if (stdin) {
return;
}

if (argv["list-different"]) {
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think just a try parses!?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True! I fixed it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, did you forget to push your commit? This seems to be the last one on master. If you remove that try, I'll merge it!

@vjeux vjeux merged commit eff5af6 into prettier:master Apr 3, 2017
@vjeux
Copy link
Contributor

vjeux commented Apr 3, 2017

Yay, thanks!

@vjeux
Copy link
Contributor

vjeux commented Apr 3, 2017

I'll do a release sometimes this week. Now that a lot of people are using prettier, need to polish them a bit more :p

@btholt
Copy link
Contributor Author

btholt commented Apr 3, 2017

Works for me! I'll refactor the Gulp plugin and release it shortly thereafter. The maintainer of gulp-prettier is unresponsive so I'll just publish another package.

@leipert leipert mentioned this pull request Apr 19, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 21, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 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.

None yet

2 participants