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 code formatting #753

Closed
wants to merge 29 commits into from

Conversation

jason-fox
Copy link
Contributor

Adds prettier and husky. All JavaScript files are autoformated on commit using the Git Hooks hook.

running npm run prettier will format all files with standardized whitespace.

package.json Outdated
@@ -57,12 +59,26 @@
},
"devDependencies": {
"coveralls": "~3.0.2",
"husky": "^1.1.0",
Copy link
Member

@fgalan fgalan Jan 28, 2019

Choose a reason for hiding this comment

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

Please use ~ dependencies (instead of ^)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 20bd137

Copy link
Member

Choose a reason for hiding this comment

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

I see husky has been fixed, but some others still:

"lint-staged": "^7.3.0",
"prettier": "^1.14.2",

Could you fix them in the same way, pls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 3536dbf

"should": "13.2.3",
"timekeeper": "2.1.2",
"watch": "~1.0.2"
},
"husky": {
Copy link
Member

Choose a reason for hiding this comment

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

How this exactly works? I mean:

  • Does some configuration needs to be do on the "Settings / Webhook" sections in the repository to have this working?
  • How the "autoformated on commit" works? Adding an additional commit automatically with the fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kind of, Husky builds a local precommit hook for you on your local instance - there is no need to add anything extra to the repository on GitHub. It means that the code gets properly formatted prior commit.

see:

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the links! I didn't know about pre-commit git hooks. Seems cool :)

NTC

package.json Outdated
@@ -33,6 +34,7 @@
"test": "mocha --recursive 'test/**/*.js' --reporter spec --timeout 3000 --ui bdd --exit",
"test:watch": "npm run test -- -w ./lib",
"lint": "jshint lib/ --config .jshintrc && jshint test/ --config test/.jshintrc",
"prettier": "prettier --single-quote --trailing-comma es5 --write **/*.js *.js",
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@jason-fox jason-fox Jan 29, 2019

Choose a reason for hiding this comment

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

Fixed 75c0c54 - Of course you shouldn't need to run this manually as Husky will do it for you.

Copy link
Member

@fgalan fgalan Jan 30, 2019

Choose a reason for hiding this comment

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

Looking to prettier CLI options, I'd suggest to included --tab-width 4 to keep the current indentation style.

I'd like to start a passionate discussion on indentation length ;), but the fact is that this repository code uses 4 from its very begining, so let's keep that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed a4979bd + 998f379 - This should make the diff less messy.

On the whole I've gone with the defaults suggested by prettier themselves, but double/single quote is repo preference and trailing-comma es5 is a step to the future (it adds the trailing quote only where ecma5 accepts it

Ideally once node 6 support is deprecated this can be updated to be consistent for ecma6 and use let and const and arrow functions and so on, but at least it is a start (and won't break the code)

@fgalan
Copy link
Member

fgalan commented Jan 28, 2019

I have had a quick look to the diff of changes but it is very hard to get the detail in a +1,189 −1,005 lines PR :)

To check if I'm understand correctly... it seems the only change is to standarize the indent to 2 whitespaces in each level (changing from current 4 spaces indent). Is that all? Or is there any other change done by this prettier library?

@jason-fox
Copy link
Contributor Author

jason-fox commented Jan 29, 2019

it seems the only change is to standarize the indent to 2 whitespaces in each level (changing from current 4 spaces indent). Is that all? Or is there any other change done by this prettier library?

Basically yes, prettier is an opinionated code formatter, it will standardize whitespace, break up long lines etc, but deliberately doesn't give you too many options to play with.

Prettier needs to be run once over the whole codebase, the Husky lint-staged will do it automatically over each commit thereafter, so changes will be minimised and easier review since there won't be any extraneous whitespace or odd formatting within the commits.

package.json Outdated
},
"lint-staged": {
"*.js": [
"prettier --parser flow --single-quote --trailing-comma es5 --write",
Copy link
Member

Choose a reason for hiding this comment

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

It would be great to avoid sentece repetition and just use:

"npm prettier",

However, I've realized that the sentence is not exaclly the same, --write vs. --write **/*.js *.js... Is there any solution to "factorize" common sentence in just one place to be used in both cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is I haven't found it yet. lint-staged is running prettier across each individual JavaScript file within the commit, the npm run prettier script is a convenience function for running across all files - it shouldn't need to be used much, but I find it useful to format my files locally whilst I'm working.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. Pitty.

NTC

Copy link
Member

@fgalan fgalan left a comment

Choose a reason for hiding this comment

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

The only pending thing seems to be changing ^ -> ~ in a couple of new dependencies in packages.json. Appart from that, LGTM.

In the meanwhile, passing the ball to @dcalvoalonso to get also his opinion on this PR.

@dcalvoalonso
Copy link
Contributor

Nice job @jason-fox ! LGTM

@fgalan
Copy link
Member

fgalan commented Jan 30, 2019

I have realized we have other 6 PRs that may suffer the style fixing.

I wouldn't suggest for block the merge of this (some of the other PRs are quite old) but at least lest provide a "migration path" their authors. For instancia:

  1. Add the following devDepencencies to packages.json in your PR branch:
    • "prettier": "~1.14.2",
  2. npm install to install the new dependencies.
  3. Run prettier --tab-width 4 --single-quote --trailing-comma es5 --write **/*.js *.js
  4. Check changes (giff diff) and commit them.
  5. Merge master branch into your branch. No style conflic should appear, given both master and your branch would be "in style sync" after step 4. Of course, conflicts may occur, but due to regular causes.

@jason-fox what do you think? Is the procedure complete and precise enough?

@jason-fox
Copy link
Contributor Author

@fgalan - yes that procedure makes sense, especially for small PRs.
However the total will be more than 6 PRs - as I've created prettier PRs for 7 other repos.

@fgalan
Copy link
Member

fgalan commented Jan 30, 2019

Yes... I was meaning 6 in this repo but of course, considered in a wider sense, there are more.

Let's analyze the situation repo by repo. In particular, in this repo we have:

@jason-fox
Copy link
Contributor Author

@fgalan @dcalvoalonso - I have just updated this PR to remove any conflicts from the 2.10.0 codebase. Could you re-review as necessary and merge or close so I no longer have to keep updating the PR?

@fgalan
Copy link
Member

fgalan commented Aug 21, 2019

If this PR is merged other PRs will need adaptation to be merged. Originally we thought that this adaptation would be straightforward (we even define a procedure for that: #753 (comment)). However, that procedure overestimated the difficulties due to exception marks (see telefonicaid/fiware-sth-comet#471 (comment)). This can cause difficulties to other PRs authors and to close PRs with valuable functionality in the worst case. As at then end happened with telefonicaid/fiware-sth-comet#471 (comment)... his author never reopened a new PR for that :(

Taking into account that past experience and the existence of important PRs yet to be merged (as #753 or #800) I'm afraid we cannot merge this one yet. However, I would't opt for closing it, as I think you have done a valuable work here and I would be a pitty lossing it (having said that if you feel more confortable closing this PR or don't want to invest extra effort updating it in the future, it's also fine).

Sorry the inconveniences...

@jason-fox jason-fox mentioned this pull request Nov 30, 2019
@jason-fox
Copy link
Contributor Author

Withdrawn - superseded by #831

#753 prettifies code, #831 does the same job and more (via the eslint prettier plugin.)

@jason-fox jason-fox closed this Jul 15, 2020
@jason-fox jason-fox deleted the feature/prettier branch November 20, 2020 14:13
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.

None yet

3 participants