Skip to content

style(*): Automate Code Formatting #90

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

Merged
merged 5 commits into from
Jul 25, 2017
Merged

style(*): Automate Code Formatting #90

merged 5 commits into from
Jul 25, 2017

Conversation

jshcrowthe
Copy link
Contributor

@jshcrowthe jshcrowthe commented Jul 6, 2017

Adding in automation for prettier code formatting and running prettier on all the files.

NOTE: I have also removed the tslint.json file in this PR as I am going to add in some automated validation for code style after this commit. I will reintroduce the tslint.json and clean up the linter errors when I have an opportunity

@jshcrowthe jshcrowthe changed the title style(*): Leverage Prettier Code Formatting style(*): Automate Code Formatting Jul 6, 2017
@jshcrowthe jshcrowthe requested a review from sphippen July 6, 2017 22:45
Copy link
Contributor

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

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

This is probably fine, but what style is it using? It seems to be different than the internal google style for TypeScript (which e.g. prefers ' over "). It would be nice if we could be consistent so it's not too confusing for people that work in both worlds.

Also, why did you have to remove tslint.json? Is it conflicting with this style or something?

@jshcrowthe
Copy link
Contributor Author

@mikelehen this is using the tool Prettier for formatting. Prettier is in the same realm of tools as clang-format only specifically tailored to TS/JS. It has some differences but is optimized for TS/JS (has supported for things like JSX/TSX which clang-format breaks, etc).

Considering this is automatically runs on commit, you can write your code however you want it all gets standardized for the review. If there is some specific things we don't like about the style we can address those for sure.

@mikelehen
Copy link
Contributor

Fair point... I've been very impressed with clang-format so I'm biased towards it. I trust it to do the right thing in 99.8% of cases and I can live with the .2% where I'm annoyed with its formatting. I haven't used prettifier, so I'm nervous... and if we need to change settings or it changes behavior in the future, it'll create gratuitous diffs which make it harder to review code and pollutes the git history (so things like "git blame" are no longer useful).

But I'm not actually opposed. I spot checked a few files and don't see anything that really concerns me. So I can go ahead and approve....

@mikelehen
Copy link
Contributor

Oh! One last comment. If folks have large outstanding PRs, this may create a merge conflict nightmare... something to keep in mind.

mikelehen
mikelehen previously approved these changes Jul 7, 2017
@sphippen
Copy link
Contributor

sphippen commented Jul 7, 2017

The styling itself looks reasonable to me.

It's unclear to me why we shouldn't just leave the tslint.json file in though. Did the code pass all the lint checks before the reformat, but it doesn't afterward? Because if it didn't pass the checks before and it still doesn't afterward, I think we might as well just leave it there instead of having to add it back later when we address the lint errors.

@jshcrowthe
Copy link
Contributor Author

jshcrowthe commented Jul 7, 2017

The removal of the tslint.json is temporary as we weren't actually validating it and were failing all over the place when I actually checked it. I am going to do a linter fix PR in the coming weeks so that will reintroduce it. I'll automate validation of that as part of our CI (alongside prettier formatting).

tl;dr: It's only temporary till I can fix all the errors.

@jshcrowthe
Copy link
Contributor Author

@mikelehen brings up a good point though. @jsayol seeing as you have the large open PR in question, would you like me to hold off till we can get your PR merged?

@mikelehen
Copy link
Contributor

FWIW, fixing all the errors may be untenable. Internally we run tslint in such a way that it only complains about lines you actually changed. That way we don't regress, but it doesn't cause noise for all the existing violations (which grow over time as new rules are enabled, etc.)

@jshcrowthe
Copy link
Contributor Author

Haha, fair enough. I will be trying to fix as many as possible and making it so the linter only runs on changed files is a great idea.

That said, I feel like, if we change tslint rules, that those should be accompanied by a fix for violating areas of the code.

@mbleigh
Copy link

mbleigh commented Jul 7, 2017

I think we can configure prettier to prefer single quotes, no?

@jshcrowthe
Copy link
Contributor Author

We can sure, if that's something we want it's super trivial to do.

@mikelehen
Copy link
Contributor

It'd be interesting to run some code through prettifier and then clang-format (and vice-versa?) to see where they conflict and then see if we want to change any settings to make them more similar. If nothing else, I'm pretty curious to see if they are significantly different in any interesting ways. I'm okay if we don't want to invest the time for that though.

@jshcrowthe
Copy link
Contributor Author

I could probably spin up a branch with clang-format, i'll get you a diff here in a bit

@jsayol
Copy link
Contributor

jsayol commented Jul 7, 2017

The removal of the tslint.json is temporary as we weren't actually validating it and were failing all over the place when I actually checked it.

True that. I had to disable tslint in my local environment since my IDE was complaining about "errors" in almost every single line and it was unusable.

If folks have large outstanding PRs, this may create a merge conflict nightmare... something to keep in mind.
@jsayol seeing as you have the large open PR in question, would you like me to hold off till we can get your PR merged?

Yeah, that might actually be a good idea. It's already going to be quite a bit of work to rebase and solve conflicts as it stands right now (I'll get to it first thing tomorrow morning) but with all these other proposed changes it might turn into a bit of a nightmare. Thank you both for taking me into account, btw :)

Edit: I'm done rebasing PR #66.
There's still quite a bit of work left until I push the PR with persistence and I don't want to block this indefinitely, so go ahead with it. I'll merge and rebase that when the time comes.

@jshcrowthe
Copy link
Contributor Author

jshcrowthe commented Jul 8, 2017

@mikelehen I ran clang-format (default settings) on the codebase and pushed a branch, here is the diffs:

I personally think prettier does a better job. But that's just my .02

@jshcrowthe
Copy link
Contributor Author

We've got @jsayol's PR in and this branch has been re-pushed with his changes. Pending some stylistic adjustments (Full adjustment options are here: https://github.com/prettier/prettier#options) this should be G2G.

@gauntface
Copy link

Style seems generally fine to me, would keep the tslint in though.

@mikelehen
Copy link
Contributor

I noticed a few things I'm not a big fan of:

  1. Prettier seems to use 2 spaces for line continuation indents, while clang-format can use more (we use 4 internally, plus it'll sometimes use more to line up parameters, etc.).
  2. Prettier has a heuristic where it will sometimes use ' and sometimes " for strings (to reduce escaping), even in a single line of code. clang-format opts for consistency instead.
  3. Prettier seems to drop quotes on key strings in object literals, e.g. here: https://github.com/firebase/firebase-js-sdk/pull/90/files#diff-6f50da10b3fab2e68170ef0e0effffa9 ('TIMESTAMP': ... became just TIMESTAMP: ...) I'm not sure what we're using for minification these days, but with closure advanced compilation, this is likely to break our code since TIMESTAMP is now eligible for minification even though it's part of our public API and should not be minified.

Unfortunately none of these seem configurable. clang-format is much much more configurable it seems. I think item 3 may be a deal breaker (we need to figure out how to turn it off or verify that we're not minifying in such a way that it's impacted). But I don't feel strongly enough about the other 2 to push back too hard.

@sphippen
Copy link
Contributor

I want to agree with @mikelehen on point 3 actually. When we stopped using Closure Compiler for storage the library went from ~30 kb to ~55 kb in uncompressed size, so I would like to introduce the Closure Compiler back into the storage build process at some point. Unless we have a way to keep keys in object literals as string literals, that doesn't seem possible.

@jsayol
Copy link
Contributor

jsayol commented Jul 10, 2017

When we stopped using Closure Compiler for storage the library went from ~30 kb to ~55 kb in uncompressed size

@sphippen I'm actually working on a PR (#94) to reduce the size of the bundles. Preliminary results are good and I've made some more progress today. I may have something decent to merge either later today or tomorrow.

@jshcrowthe
Copy link
Contributor Author

@mikelehen, I'll address point 3 because that seems to be the main blocker. We are currently using the package uglify-js to do minification. This internally does not do sub-property mangling by default. I have started looking into using Closure Compiler for minification but it seems like we have some things we'd need to fix first as the current version of closure compiler chokes on the webpack output.

Having said that, I've seen some stuff about using tsickle to help properly format the typescript output for closure so I'll look into something with that to see if we can get the built size down more. #94 gets us to a much better place and the beta v1 of the UglifyJS Webpack Plugin uses uglify-es which I've seen examples where it outperforms closure. So lots to investigate in this area.

@jshcrowthe
Copy link
Contributor Author

@sphippen as of the release at 4.1.4, firebase-storage.js is back down to 33 kb uncompressed. In addition, I am going to try and standardize the underscore convention which would allow us to be much more deterministic with our mangling and prevent the overlap with the style tools.

@jshcrowthe
Copy link
Contributor Author

jshcrowthe commented Jul 24, 2017

All, if there are no more blockers on this I'm going to merge this and start working on the linter fixes and the underscore convention. Please let me know if you have any other concerns.

…in the build

Something breaks in version 2.4.1 of typescript. We are pinning to 2.3.0 till those things can be
triaged and fixed
Temporarily removing this file as we don't pass the checks
@jshcrowthe jshcrowthe merged commit bc1f67c into master Jul 25, 2017
@jshcrowthe jshcrowthe deleted the prettier-styling branch July 25, 2017 22:53
@firebase firebase locked and limited conversation to collaborators Oct 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants