Closed samouri closed 7 years ago
ccing people that I've seen discuss it: @nb @gziolo @dmsnell @aduth @youknowriad @Copons @ockham
There is also https://github.com/prettier/prettier-eslint which formats your code via prettier
, and then passes the result of that to eslint --fix
.
It is also possible to execute prettier in a linter mode. It requires some code, but it shouldn't be a big deal. See this comment about Jest integration: https://github.com/facebook/react/pull/9101#issuecomment-286610833.
The initial "run prettier" commit would mess with our git blame.
I guess it depends on the scope of those changes but this looks like a non-negligible downside to me.
GitHub makes it pretty easy to skip around blame history
Could you point me to a good example of this? I always found that very difficult to do but maybe I'm missing something.
here's what I used for the notifications client:
prettier --print-width=100 --single-quote --tab-width=4 --trailing-comma=es5
in my IDE I have it auto-format on save which is wonderful because prettier
doesn't just format code but it puts in things like parentheses and semicolons where needed. instantly I can see if I made a logical error due to something like precedence due to this auto-inserted syntax (removes code ambiguity).
on pre-commit it formats all of the changed files.
this has been an excellent way of removing a lot of cruft for me while trying to focus on more important matters. I think we could win in Calypso with it as well
there are things I don't like about it:
these annoyances have just been that: annoyances; I find the project as a whole is in a much better place because of it.
bias: I think JavaScript suffers by not having a standard format guide. I may not like the style but at least I don't have a choice… I use elm-format
in the same way when I code in Elm and I like gofmt
's stance and I use auto-format whenever possible
Could you point me to a good example of this? I always found that very difficult to do but maybe I'm missing something.
@stephanethomas ^^^
Wow, I've never noticed that feature. Thanks @dmsnell!
A linter like eslint cannot reliably fix style issues
Why not? All Prettier does is parse the code into an AST and apply a pretty printer output. There is little difference between this and what ESLint does. While it may be true that ESLint's fixer doesn't currently support all rules, it's not fair to claim it can't.
It reduces the learning curve for new OSS contributors
Except if they're coming from a WordPress background.
It acts as a teaching tool by wrapping expressions according to javascript's order of operations
Sounds like a good candidate for an ESLint error rule.
React, Immutable, jest, babel, and more are all using it (prettier is well tested)
Is this more a point to them not having a consistent and documented style of their own previously?
Are we addressing a real pain with this proposal?
While it may be true that ESLint's fixer doesn't currently support all rules, it's not fair to claim it can't.
fair. ESLint may some day support fixing all rules
Is this more a point to them not having a consistent and documented style of their own previously?
The reason I brought this up wasn't about pointing out the merits or problems with their previous style. It was to highlight the level of maturity / expected continued development & support for prettier
.
Are we addressing a real pain with this proposal?
The pain is totally subjective and will differ person to person. Its the reason I proposed a vote.
Are we addressing a real pain with this proposal?
for me we are. I would also argue that even if ESLint can "fix" these things it does a very poor job of it, which is one of the reasons given as the impetus for prettier
. style isn't "fixed" or "broken" - it just is.
prettier
makes it straightforward for us to completely remove that piece of coding from our minds when we're developing and for me that has been wonderful.
Except if they're coming from a WordPress background.
how is this? it seems like the jump from a "WordPress background" to Calypso is already huge, plus people have to learn our style guides. with an auto-formatter that last piece has been eliminated.
prettier
itself just happens to be the first JavaScript auto formatter that I have seen that does an incredibly good job. it's based off of work that has been popular in functional languages for a long time. its does really well (despite the bugs because of its infancy) and seems like the final way to end the style questions
Except if they're coming from a WordPress background.
how is this?
Because despite some slight variation (that I'd like to champion upstream) and revisions specific to ES2015+, there's significant overlap between our style guide and the core JavaScript Coding Standards on which it was originally based.
I love the developer experience of the AST approach brings to the table, though it should be able to match our current style and, to be honest, it would be great that some of the bugs are ironed out, because each change in the tool leads to formatting changes in commits. And we all love those :)
it should be able to match our current style
@nb I think we will be guaranteed that prettier
won't ever match our current style. we have many deviations from it, many ambiguities, and a project goal is never having more than five configuration options.
@dmsnell outside of the ambiguities, we should be able to get our spaces, at least.
http://eslint.org/docs/rules/space-in-parens with --fix flag should help
@nb, you may be interested in: https://github.com/prettier/prettier/issues/40.
In summary though: some vocal and influential parts of the js community think that it would be best to not support most configuration. The only one that will probably actually get developed at some point is tabs vs. spaces. Definitely not our liberal spacing around parenthesis.
see also https://github.com/prettier/prettier-atom and other tools
Seems like one key question in this discussion is: Do we want to, or do we not want to continue to follow WordPress core's JS Coding Standards? By using prettier
(without any forks or tacked-on configuration), we would most certainly cease to do so.
Seems like one key question in this discussion is: Do we want to, or do we not want to continue to follow WordPress core's JS Coding Standards?
Agreed. A vote for 👍 is a vote for breaking with WP JS Coding Standards
Do we want to, or do we not want to continue to follow WordPress core's JS Coding Standards?
I still think the link and the value of the link is dubious. Our coding style is considerably different than what one will find in WordPress and that's happening at a much higher level than spaces and line-lengths.
A vote for this is also a vote to remove the obstacle of needing to learn any coding style at all, to eliminate all discussion about style, to handle styling deterministically, and to enjoy the benefits thereof :smile:
I've created a PR to figure out the finer details in case we decide to Use Prettier #12479
I came here to ask for a test case, as I'm still educating myself on the impact/caveats/pros-cons this would have, so :heart: for #12479 @samouri
If we introduce prettier
we should also mitigate issues with formatting when using codemods
. See related discussion: https://github.com/Automattic/wp-calypso/pull/12521#pullrequestreview-29108476.
To be honest, I don't think we should vote to decide if we deviate from WP-core standards ... I think this is the kind of decision it should come "from above", or, in any case, decided between the whole company (if we change our standards, we should do it in every automattic project, not just in calypso). A one-sided calypso change would be pretty terrible for anyone who usually works on more than one of our projects at the same time
decided between the whole company (if we change our standards, we should do it in every automattic project, not just in calypso). A one-sided calypso change would be pretty terrible for anyone who usually works on more than one of our projects at the same time
I disagree. As @dmsnell explained higher up, using prettier actually doesn't require you learn much new. Its a one time setup cost of integrating a new plugin to your editor/workflow. This shouldn't make working on multiple projects any harder.
The WP JS standards were adopted from the jQuery project with a slight eye towards being not too strange to WP PHP devs during WordCamp Boston in 2013. Most code in core was jQuery spaghetti, with only the Customizer existing, and the Backbone-based media library being in active development.
That was a JS epoch (or two) ago, and I would feel bad if they held us back from doing what makes the most sense for Calypso.
They released stable version of prettier: http://jlongster.com/prettier-1.0. In fact it's already v1.1.0. It contains a new option tabs vs spaces (https://github.com/prettier/prettier/pull/1026), which was missing before.
I disagree. As @dmsnell explained higher up, using prettier actually doesn't require you learn much new.
It's not just about writing code in the way you like... it's about the overhead of reading different codebases with different styles daily, and mainly, it's about education and getting used to the style automattic uses everywhere.
Don't get me wrong, I'm not the biggest fan of our style guide. I joined Automattic as a javascript wrangler because I've worked mainly as a javascript developer for years... and all the style guides I've been familiar with were totally different to WordPress one. It's a rough ride at first ( my first calypso PR had more than 100 comments... most of them about style guide violations :scream: ), but you learn it and you get used to it ( to the point to start using spaces inside parens even when you are writing prose ;) ) . So I'm not especially fond of our style guide, no. But does it mind? No, not at all... I think that pretty much any style guide is as valid as any other, as long as you are consistent and make sure people follows it.
And what it will happen if we make calypso SG to depart from WP standards? We would be breaking this consistency. We would have different javascript projects using different styles. And what is worst, we will lose our cross-language consistency.
It's not perfect, of course, but right now you can read the PHP WordPress.com API code and then jump into calypso code and the style is pretty similar. The PHP style is easy to learn for newbies who come from calypso because a big chunk of it is exactly the same they already used for javascript. And this is quite critical: Most of the people who start developing in calypso are javascript developers who are not very familiar with WordPress PHP standards (that was my case, and I'm pretty sure it's the case for the majority of us, javascript wranglers). And by learning calypso standards, you get used to the set of rules you will need to use when you do the jump and start working also on the API side. If we move calypso standards away from WP ones, we will be throwing every new person to the wild wild west world of the API (no linters, much looser code reviews, etc) without even being familiar with the code style rules.
All this could be ok if it was the counterpart of something big. But what do we win with moving to prettier / changing the style guide? Our automattic linter is already very good (major kudos to @aduth and everyone who worked on it), and even if Prettier could be better in some fringe cases, we are talking about getting to a 97% good platform to a 100% one, not about fixing some process that is currently broken. The only other major point that could be made is that moving to a more js-scene friendly set of rules could open the road for new OSS contributors ... and that's a good point and it would be amazing. But being honest, we don't have any evidence that it would happen, and it's a pretty major step to take just to test if it works.
Don't get me wrong, I'm not the biggest fan of our style guide.
This whole paragraph seemed like an argument for prettier ;)
it's about the overhead of reading different codebases with different styles daily
Personally, I find that in the JS world I do that every day anyway. Calypso has many external OSS dependencies (redux, react, etc) and each of those follow a radically different style than our own.
It's not perfect, of course, but right now you can read the PHP WordPress.com API code and then jump into calypso code and the style is pretty similar.
You bring up a point I hadn't really considered before. I personally haven't don't much wp-api work yet so don't feel comfortable weighing in here.
But what do we win with moving to prettier / changing the style guide?
This is essentially the same argument @aduth already brought up with: "Are we addressing a real pain" and @dmsnell answered that here: https://github.com/Automattic/wp-calypso/issues/12260#issuecomment-287445965.
https://github.com/prettier/prettier-eslint seems like it'll get us where we want to be format wise.
:+1: to what @blowery said. Prettier + eslint --fix will do the trick. I expect it won't work perfectly with max line width set by prettier, but it shouldn't be an issue for us. It's an edge case. We can add an integration with IDEs first to allow people to play with this tool. If it turns out to be useful we can add pre-commit hook and CircleCi verification.
related https://github.com/prettier/prettier-atom/issues/133
been trying that out with our !.eslintrc.js
line removed from .eslintignore
. Seems to work fairly well. It doesn't currently fix {foo}
to { foo }
, though eslint seems to support autofixing that one. Also doesn't trigger eslint to error...
Because I've been wanting to use it locally for development, I created a fork (wp-prettier) that keeps our spaces and uses tabs. In order to setup in vscode I did this (and installed the prettier plugin):
npm i wp-prettier
npm i --save-dev prettier
cp node_modules/wp-prettier/src/* node_modules/prettier/src
I created a fork (wp-prettier)
hrm… not sure how I feel about that but good work!
As mentioned here https://github.com/Automattic/wp-calypso/pull/13759#issuecomment-300390056. If we want to effectively use codemods it would be very convenient to have prettier in our workflow. Otherwise we will have to fix issues manually from time to time, because I don't believe eslint --fix
is smart enough to resolve all styling issues.
for the sake of discussion, I would like to propose that we don't mix eslint formatting and prettier. if we introduce prettier then we should stop using eslint
altogether for any of its formatting work. leave it all to prettier and leave eslint exclusively for code linting.
why? prettier is designed to be a deterministic formatter and if we start mixing it we're not only defeating that purpose but also introducing potential races and cycles in the formatting between them.
I've opened a PR to incorporate a fork of prettier
into calypso as a devDependency. @blowery, @bluefuton, and I have already started using it within the Reader codebase. It was especially helpful with removing all of our usages of React.createClass
here.
cc @gziolo
Edit: forgot to link to the pr https://github.com/Automattic/wp-calypso/pull/14010
There is ongoing discussion about using prettier in WordPress core. There is similar set of issues that need to be resolved. @ntwb is planning to explore this further.
I found also this closed issue in Prettier's repository: https://github.com/prettier/prettier/issues/1303. It seems like they aren't totally against this rule. If there would be a bigger group requesting this feature they would at least consider it.
Over the last few weeks, I spent some time playing with our prettier fork (originally created by @samouri) and I did some work that might be interesting for Calypso and WordPress developers:
I turned the changes into a proper option (--paren-spacing
) that can be enabled or disabled. This makes the patch at least theoretically mergeable into the official version. If the maintainers ever decide to include the option, we have a PR ready.
I rebased the patch two times already against official releases -- first time from 1.1 to 1.2, second time 1.2 to 1.5. It's not a very big deal and I can commit to doing the upgrade regularly as needed and to fix any related bugs.
There is a calypso-1.5 branch in the fork repo that contains a sequence of independent and easy-to-maintain commits. One adds the --paren-spacing
option, another sets the Calypso preferences as defaults and ignores any configuration passed from outside, another updates the README.md.
If we decide to use the fork, I think it's now in a very good shape and easy to maintain.
I don't know the status of the WordPress core discussion mentioned by @gziolo and the explorations done by @ntwb. I'd love to learn more about that.
Thanks for maintaining that @jsnajdr - it's still a big patch and I'm not sure how easy it would be to maintain if someone else had to jump into it, especially if the official repo diverges much.
I'm more in favor every day of just accepting all the default options and letting it be. We even recently made the jump to spaces over tabs in the notifications client (and I prefer tabs!) and so far it hasn't caused one little headache at all. The benefits of not having to think about styling are so huge and the auto-format while I code makes me that much quicker.
I'm more in favor every day of just accepting all the default options and letting it be.
If that ever happens and the style guide for Calypso (or WordPress in general) changes, that's going to be great, of course. Meanwhile, if someone wants to just press ⌃⌥F
in their editor today and get the code formatted, the fork seems like the best option for that. And that option just got a little bit better.
@jsnajdr is there a PR out there to update calypso to use the newest fork?
is there a PR out there to update calypso to use the newest fork?
Just created one: https://github.com/Automattic/wp-calypso/pull/15942
With the release of React 16, and the important role that codemods will play in getting us to a state where we can upgrade, I think its important to revisit the question of whether or not we want to use a code auto-formatter for the entire repo. Besides for all of the points mentioned in this thread so far, the one I'm focusing on right now is that running calypso-prettier
on the entire wp-calypso codebase will make running codemods significantly easier.
Here are some updates on calypso-prettier
and its usage within Calypso.
prettier
seems to have a monthly release cycle so far. We now have a very smooth process of upgrading. After merging in a prettier upgrade, we simply run npm run reformat-files
.@format
docblock to any modified file. This allows us to know whether or not a file's formatting is being controlled by prettier. This functionality is also in the process of being merged back upstream to prettier proper.@format
docblock to ensure that an automatically formatted file stays automatically formattedeslines
to ignore formatting rules for any file containing the format flagclient
is currently being managed by calypso-prettier
. Thanks to all the people that have been making calypso-prettier
possible: @jsnajdr, @sirreal, @nosolosw, @dmsnell, @jsnmoon, and @blowery (I might have missed someone).
Note: @dmsnell attempted this months back: https://github.com/Automattic/wp-calypso/pull/14801
Related: p4TIVU-89g-p2
Speaking from my recent trial perspective, I have to say that calypso-prettier was immensely helpful in getting up to speed with the formatting style. It meant I could focus on producing output right away rather than worrying about formatting details (while learning them along the way by looking at the changes made by calypso-prettier).
Two key advantages here: It makes the onboarding experience for new OSS contributors considerably smoother. It cuts down the time spent on PR reviews considerably, no more loops just to correct formatting means time saved on both ends.
So from a trial / new hire perspective definitely a :+1: for calypso-prettier.
My evolution with prettier
:
prettier
XMy vote is yes!
My evolution with prettier
:
gofmt
was the greatest decision made in that languageelmformat
is so lovelyeslint
so bad with formatting?prettier
? Community-wide JS code standards? AWESOME!prettier
My vote has been openly yes!
Shall we call this one closed by ~#18453~ #18282?
Over the past few weeks, the prettier javascript auto-formatter has come up many times in calypso-framework chat. For anyone who wants a fun introduction to what prettier is, feel free to watch the creators lightning talk at ReactConf: https://www.youtube.com/watch?v=hkfBvpEfWdA
tldw:
git blame
. GitHub makes it pretty easy to skip around blame historyI think we all know that we could debate styles until the end of time, so I figure lets just call for a vote and be done with it. Should we spin up a PR that formats all of calypso, modifies our eslint style rules, and updates our docs?
Thumbs up for yes, thumbs down for no.