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 Prettier - an opinionated code formatter #932

Merged
merged 2 commits into from
Feb 13, 2020
Merged

Conversation

simison
Copy link
Contributor

@simison simison commented Dec 8, 2018

  • Adds a git pre-commit hook that automatically formats your code.
  • Reformats the code in this same PR

Eslint rules still need work, since now "prettier" rules are applied everywhere.

https://prettier.io/docs/en/eslint.html

Resolves #858

Testing

  • When you make changes to files and git commit, Prettier should process those files.

@simison
Copy link
Contributor Author

simison commented Dec 14, 2018

Can be useful here: https://www.npmjs.com/package/pretty-quick

@ahstro
Copy link
Contributor

ahstro commented Sep 1, 2019

What still needs to be done for this to be merged? Prettier is so convenient 😍

@nicksellen nicksellen mentioned this pull request Dec 13, 2019
5 tasks
@simison
Copy link
Contributor Author

simison commented Jan 2, 2020

What still needs to be done for this to be merged?

@ahstro I think the path for this starts to be clear; we've converted codebase to ES6, consolidated different Eslint configs and we're working on removing stuff from Gulp setup, too.

Wanna help out with this? :-)

@simison
Copy link
Contributor Author

simison commented Jan 4, 2020

We have some choices of different strategies here — any preferences which way to go?

  1. Aggressive reformatting on commit
  • Each staging file (in git commit) would get automatically run by Prettier
  • Those files would also get /* @format */ pragma added
  • Your editor will run Prettier only on files marked with @format
  • Everything just goes on automatically as we touch files (and someday we can reformat everything remaining)
  • Causes more noise in PRs since lots of unrelated changes will be in each file
  1. Manually reformat first
  • Any file that we want in Prettier has to be formatted and committed to master first.
  • We'll have /* @format */ pragma on those files
  • Your editor will run Prettier only on files marked with @format
  • Prettier will run for staged files on git commit only if a file has @format pragma, and otherwise keep them as-is
  • More manual work up-front, less noise in PRs
  1. Prettier everything
  • Do a mega PR by Prettier-reformatting everything at once
  • No @format pragma, Prettier always runs on all files (in your editor and on git commit)
  • Just works — HUUUGE PR — Potential rebase nightmare for open PRs?

Thoughts?

@simison simison force-pushed the update/add-prettier branch from cccea86 to 66ebc1e Compare January 4, 2020 17:25
@simison
Copy link
Contributor Author

simison commented Jan 4, 2020

I tested with some files and unrelated Prettier changes are so much noise that I think we're better off with option 2. One big mega PR we can do once we're doing PRs mainly to Prettified files so that we don't end up in rebase nightmare. I'll changes settings in this PR to that effect. Lemme know if you have thoughts or prefer otherwise!

You can test the noise level by checking this branch, make some whitespace changes in any file and commit it and see the git log/diff.

"pre-commit": "npm run -s install-dependencies && npx lint-staged"
}
},
"lint-staged": {
Copy link
Contributor Author

@simison simison Jan 4, 2020

Choose a reason for hiding this comment

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

An alternative for lint-staged could be pretty-quick but its geared towards Prettier only. With lint-staged we can also run linter on files.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, seems good to be able to have it doing general linting too. Although we also have linting in npm run lint:watch and in the webpack dev server... (for client files), linting everywhere... (not to mention my editor is doing it too).

Personally, I would not add the git hooks to my own local setup, I don't know why but they feel invasive, maybe I can learn to love them though. But not much unlinted makes it through the 3 lint stages above anyway.

Copy link
Contributor Author

@simison simison Jan 8, 2020

Choose a reason for hiding this comment

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

Hehe yeah. We should drop linting either here or in webpack dev-server+lint:watch. I like the simplicity of this setup and since I have continuous linting in editor, I don't really need the dev-server linting. I appreciate it might be easier to approach for new devs/volunteers, tho.

What do you think?

In future I'd like to add Stylelint to lint *.less files so it would get added here, too.

@simison
Copy link
Contributor Author

simison commented Jan 4, 2020

Once everything is in Prettier, we might be able to remove these Eslint rules from our config, since Prettier-eslint config turns them off anyway:

  • comma-dangle
  • comma-spacing
  • curly
  • indent
  • key-spacing
  • keyword-spacing
  • no-multi-spaces
  • no-trailing-spaces
  • object-curly-spacing
  • one-var-declaration-per-line
  • semi
  • space-before-function-paren
  • space-in-parens
  • quote-props
  • wrap-iife
  • arrow-spacing

For now, we need to keep them since not every file is Prettified.

@nicksellen
Copy link
Contributor

I actually don't mind mega PRs formatting change so much, I tend to find a way to run the prettier on my other PRs before trying to rebase/merge, so that it's more-or-less in the right shape anyway. Might be more fiddly getting the exact prettier config to use. But perhaps others are less happy doing PR fiddling, and so maybe your option 2 is best. I guess once we rebase our other PRs ontop, we could turn the @format thing on as we go...

@simison
Copy link
Contributor Author

simison commented Jan 8, 2020

I'm happy to do mega PR if you all are fine with it. I don't have that much to rebase as you all have. ;-)

@simison simison force-pushed the update/add-prettier branch from a75bf17 to 164adfe Compare January 19, 2020 20:08
@mrkvon mrkvon force-pushed the update/add-prettier branch from e64c0d8 to 6de6e6e Compare January 25, 2020 22:41
@mrkvon
Copy link
Contributor

mrkvon commented Jan 25, 2020

Rebased.

Copy link
Contributor

@mrkvon mrkvon left a comment

Choose a reason for hiding this comment

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

Interesting stuff! Never used this prettier before (if i don't count a random typescript tutorial 2 days ago).

How can we use this feature?

When i add the /** @format */ at the beginning of the ./modules/users/server/controllers/users.profile.server.controller.js and commit the change, i get a whole bunch of space-before-function-paren linter errors; as if the eslint and the prettier are fighting. I would expect it to do the formatting for me and finish gracefully.

And is it possible to format everything without adding the /** @format */ piece?

I don't have much idea about the 3 options of running prettier, but I like the option 3 for the reason that all the code is consistently formatted at all times. I haven't pondered it much though.

package.json Show resolved Hide resolved
@simison
Copy link
Contributor Author

simison commented Jan 26, 2020

And is it possible to format everything without adding the /** @Format */ piece?

Yes, I indeed changed it to that in the latest commit but didn't update the PR description yet. 🤦‍♂ I initially added format pragma just because I didn't want to do one mega-PR and choose portions of the code to convert in smaller chunks.

mrkvon pushed a commit that referenced this pull request Feb 12, 2020
Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier
@mrkvon mrkvon force-pushed the update/add-prettier branch from 54bf1e7 to cce1811 Compare February 12, 2020 00:23
@mrkvon
Copy link
Contributor

mrkvon commented Feb 12, 2020

I squashed the commits until now, removed some conflicting eslint rules and performed the prettier reformat-files on md, json, html and js files.

A few notes:
The original branch (before force-update) is saved at branch prettier-backup.
I made Prettier <prettier@prettier.io> to be the author of those formatting commits. I don't want to have my name everywhere to git blame and i don't want to mess up my coding statistics either.

Are we ready for such a large change?

Please, review the additional removed eslint rules efb6b86, see that you're happy with the rest, too.

We'll still want to squash those additional eslint rules into the "configuration" commit and maybe squash together the reformatting commits as a single separate commit.

Please try to run the app and see that it still works as expected.

@mrkvon
Copy link
Contributor

mrkvon commented Feb 12, 2020

We may want to undo reformatting of md files in .github/ and html files in modules/core/server/views/email-templates-text/, and possibly more.

@simison
Copy link
Contributor Author

simison commented Feb 12, 2020

Thanks, @mrkvon — on cursory looking Eslint config changes look good.

We may want to undo reformatting of md files in .github/ and html files in modules/core/server/views/email-templates-text/, and possibly more.

Should we just add those md & html files to eslintignore for now?

Alternatively, we could add <!-- prettier-ignore-start --> and <!-- prettier-ignore-end --> tags to those files.

What do you think?

@mrkvon mrkvon force-pushed the update/add-prettier branch 2 times, most recently from c57268e to 7d471b8 Compare February 12, 2020 19:37
Copy link
Contributor Author

@simison simison left a comment

Choose a reason for hiding this comment

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

I can't press "approve" since I opened this PR but your changes look great!

Let's do this! 💪

I've spot-checked different files, tested linter locally with npm run lint and running this locally works.

Also IDE integration with both Eslint and Prettier seems to work:

image

When I modify the file, Prettier adds that comma back.

Also committing file makes Prettier add comma back.

When committing a js file and letting Prettier run on it, I see this:

⚠ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

...so suppose we can remove it. :-) OK to merge as is and remove later, or if you remove please test that committing+Prettier still works.

mrkvon pushed a commit that referenced this pull request Feb 12, 2020
Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier
@mrkvon mrkvon force-pushed the update/add-prettier branch from 7d471b8 to 1a43c4e Compare February 12, 2020 20:26
@mrkvon
Copy link
Contributor

mrkvon commented Feb 12, 2020

I suppose the git add is necessary if we want the Prettier+commit work. (Haven't tested.)

We get warned rightfully. I find it scary indeed that there are changes which get committed and which a developer has no control over. It's just something I'll need to get used to. 🙂

I wonder whether to squash the four formatting commits into one (md, html, js, json). I'm inclined to do it, but I'd like a second opinion. Probably doesn't matter anyways. 🙂

@mrkvon
Copy link
Contributor

mrkvon commented Feb 12, 2020

I've squashed the commits a bit, so there are 2 commits left: config and formatting. I think it would make sense to keep it that way and use the Rebase and merge button.
If everything seems ok, @simison feel free to merge. 🙂
Otherwise I'll merge in 12 hours or so. gn.

@simison
Copy link
Contributor Author

simison commented Feb 12, 2020

One huge reformat commit seems fine instead of splitting by format.. Might make rebasing easier?

I also thought precommit hook needs the git add but the warning made me think it might make it on our behalf?

@simison
Copy link
Contributor Author

simison commented Feb 12, 2020

The idea that commits Prettified files on your behalf is to provide convenience. :-) I have it set in editor myself, so it reformats as I type or safe files.

You'll still get chance to see the edits before pushing and then finally before merging.

simison and others added 2 commits February 13, 2020 13:58
Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier
@mrkvon mrkvon force-pushed the update/add-prettier branch from 9f5912d to 95db69b Compare February 13, 2020 12:59
@mrkvon
Copy link
Contributor

mrkvon commented Feb 13, 2020

@simison Indeed, your understanding is correct. I've just tested that with the git add removed from lint-staged config, the prettier changes are still committed.

The change was included with the latest push --force

@mrkvon
Copy link
Contributor

mrkvon commented Feb 13, 2020

With that done, let's merge.

Kudos @simison 🎉

And let's see how rebasing after this will go...

Copy link
Contributor Author

@simison simison left a comment

Choose a reason for hiding this comment

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

Perfect! 👍 Thanks for testing the git-thing.

Feel free to merge.

@mrkvon mrkvon merged commit a611448 into master Feb 13, 2020
mrkvon pushed a commit that referenced this pull request Feb 13, 2020
Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier
nicksellen pushed a commit that referenced this pull request Feb 13, 2020
Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier
@mrkvon mrkvon deleted the update/add-prettier branch February 13, 2020 13:32
nicksellen added a commit that referenced this pull request Apr 4, 2020
* Add Thread component for /messages/<username>

* Marginally nicer tribes-in-common code

* Minor formatting update

* Style refinements, more flex, less row

* Indentation fix

* Implement infinite scroller for chat thread

* Update package lock

* Some code tidying

* Mark messages as read

* Add message sending and scrolling to bottom

* Small code tidy

* Don't send empty messages

* Save message draft in local storage

* Don't add fake messages on initial fetch

* Still show message input when no messages

* Add "You haven't been talking yet" bit in messages

* Add message for non-public user

* Add "Your profile seems quite empty" bit

* Use lodash like lodash/func not lodash.func

I was wrong!

* Add quick replies

* Remove premature optimization

Should probably use useMemo there anyway

* Preserve position on resize and styling fixes

* More styled-components, less inline style

* Extra a few components into their own files

* Add Prettier - an opinionated code formatter (#932)

Lint and prettify staged files in pre-commit hook
Reformat js, md, html and json files
Remove eslint rules that conflict with prettier

* Reformat messages thread files with prettier

* Add message when user does not exist

* Minor tidy

* Put all messages text into i18n

* A little refactor

* Use loading indictor

... and fixup some imports

* Add initial message thread tests

* Update translations

Not sure I should add this as some of the changes look odd...

* Add note about fake mongo id generator

* Move more general components into better places

* Implement paginated messages loading

* Fix tests to use new paginated API

* Remove comment

It's actually too hard to write a pagination test for now
because there is no scrollHeight in jsdom as it's not actually
doing the layout stuff, so can't really test that bit.

Would be nice to have a way to test the _logic_ though...

* Add some documentation about InfiniteMessages

* Remove undeeded @todos

* Redirect back to inbox if visiting self thread

* Remove old angular messages stuff

* Remove redundant error handling

* Use mongolib to generate test id

* Revert translations to master

* Don't use translation function with dynamic text

* Remove unused thread dimensions directive

* Fix messages-read API response

* Remove trailing <br> inside TrEditor

Moving it a bit closer to the source

* Show monkeybox for the correct chat user

* Refocus input after sending a message

* Fix link to profile in Monkeybox

* Always show reply box for existing conversations

* Add Monkeybox "Languages" heading to i18n

Co-authored-by: Mikael Korpela <mikael@ihminen.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prettier: Opinionated Code Formatter
4 participants