-
Notifications
You must be signed in to change notification settings - Fork 638
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
Use Prettier #1316
Use Prettier #1316
Conversation
If this hasn't changed recently, the project has already jshint taking care of this. |
Ah - I didn't notice. In any case, as far as i know, prettier is the only formatter that formats the way it does, other formatters just follow some rigid rules, and prettier optimizes for legibility and clean diffs. It also formats markdown, html etc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm strongly in favor of using an automatic formatter, but this PR adds so many changes, it might we worth looking into prettier's pragma-mode.
That way, files can be formatted over time.
Additionally, to ensure code is formatted, consider adding prettier --check
to the npm test script.
changes applied. I decided against the pragma mode because it means adding a header to all files. If there's a problem, we can override it on a single directory. Should the jshint config change? |
oh, and do we want Personally, I prefer tabs, no semicolumns, any chance of that happening? ;) |
Ah, I see that jshint errors in the tests. What would be the sentiment around switching to eslint? I believe it's currently the more maintained one (just an impression) and it integrates with prettier. |
🤔 looks like prettier on Windows always fails, the other tests just time out sometimes. |
Ok that was it. Now the only test failures are random timeouts |
package.json
Outdated
@@ -6,8 +6,9 @@ | |||
"ungitPluginApiVersion": "0.2.0", | |||
"scripts": { | |||
"start": "node ./bin/ungit", | |||
"test": "grunt test", | |||
"test": "prettier --check . && grunt test", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently all tooling is run through grunt and while I think it might be good to switch to npm scripts in general, that might be a different change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my grunt is rusty, any quick hints for how to achieve this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend looking at Gruntfile.js
and grunt-prettier package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jung-kim I ended up doing it in plain grunt, would the grunt-prettier package add anything?
rebased, moved test into grunt, reverted arrowParens to default |
Should prettier replace existing I admit, I've never done js professional so I feel I'm always behind on "new standard" but it seems that prettier is supersedes js-hint which superseded js-lint. |
@jung-kim Not quite. Prettier on the other hand is an automatic code formatter, focusing on code style (spaces around operators, etc). Both (types of tools) can work well together when properly configured. |
As for linting: I'll gladly do an eslint one later, and in the nodegit branch I have the configuration to make VS Code use Intellisense on the JS files, which is great for detecting typing errors and method/argument completion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this PR affects (conflicts) with existing PRs, we will go through some of the other ones first before merging this.
@@ -1,3 +1,3 @@ | |||
* text=auto | |||
* text=auto eol=lf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference: prettier/prettier#7853 (comment)
7f09a77
to
e753177
Compare
Changes applied. If you prefer, I can leave off the Another option (reasonably nuclear) is to rewrite the whole repo history with prettier. If the forks do the same then the PRs can remain as-is, but formatted. It takes quite a while to run though:
|
6f0328f
to
293ca73
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: We have to run prettier
twice the first time, because it does not format server.js
and git-api.js
correctly in the first run.
c4d08a4
to
6712526
Compare
@campersau 👍 done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good now. I will wait until monday before merging to give others some time to comment since this will change a lot of code.
Should I add a prettier commit so people can see the result? |
Yes, you can. Please run it twice! |
Gruntfile.js
Outdated
@@ -188,6 +177,21 @@ module.exports = (grunt) => { | |||
} | |||
}); | |||
|
|||
grunt.registerTask('checkPrettier', 'Verify that all files are correctly formatted.', function() { | |||
const done = this.async(); | |||
childProcess.exec('prettier -l . bin/*', (err, stdout, stderr) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer using the prettier check API instead of childProcess.
Also, if we are using a command anyway, I do not see much value by adding it to Grunt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just tried to run grunt
but it didn't work because I don't have prettier
installed globally. Only npm run build
works now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree that that is the cleaner way of going about it, it is also a lot more work and I just want to get code formatted :)
On top of that, it's not clear that Grunt will stay in use. Personally, I havent used Grunt since 2014, and instead I rely on Webpack and nps for my entire workflow. Grunt is just so much more complex.
And even if Grunt will stay in use, eslint is imho better than jshint, and it has prettier support for checking and fixing.
I added npx
to the grunt command so that it will use the installed prettier.
Gruntfile.js
Outdated
return done(new Error(`Files with incorrect formatting (run "npm run format" and consider a Prettier plugin for your editor):\n${files}\n`)) | ||
} | ||
if (err) { | ||
console.error(stderr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use grunt.log
instead of console.
Also, the line is missing a ;
.
console.error(stderr) | |
grunt.log.error(stderr); |
After this PR (in its current state) I can see several different pattern that we might want to align:
What is the general idea for calling build steps/scripts? Given the slowed down (or discontinued) maintenance of Grunt plugins, we might want to reduce their usage. Should we also migrate away from Grunt itself? While this is not directly related to prettier, I wanted to mention it here, because this PR adds two more entries to my list above. |
@Hirse So I think webpack is more powerful than browserify. I have a setup where the dev server automatically updates the server (hot-patching the endpoints, reloading if not possible) and the client side (uses React to do the same), all from running a single command. The prod build converts only the files needed for the final bundle to the formats needed, thanks to require-based dependencies and pluggable loaders. This removes 90% of what grunt does, and does it as fast or faster. The other 10% are maintenance scripts that are annoying to write in Grunt; I instead run them with https://github.com/sezna/nps |
@wmertens I agree that webpack is powerful enough to handle most cases, but switching from browserify to webpack has lots of other consequences for the current plugin-infrastructure. As a first step, replacing grunt with individual scripts (whether running them directly or through nps) would be good. |
prettier refuses to format these
It should be the default but --check complains
* prettier: add and configure * fix html errors prettier refuses to format these * jslint: ignore prettier formatting * prettier: ensure lf line endings on windows It should be the default but --check complains * Move prettier check into grunt
It's very nice to not have to worry about formatting. Most of the time the result is very acceptable, and by formatting on save in editors that support it, you never have to spend time on getting the spacing right.
It even helps with spotting bugs, because the formatting will show nesting errors.