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

Use Prettier #1316

Merged
merged 6 commits into from
May 11, 2020
Merged

Use Prettier #1316

merged 6 commits into from
May 11, 2020

Conversation

wmertens
Copy link
Contributor

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.

@ylecuyer
Copy link
Contributor

If this hasn't changed recently, the project has already jshint taking care of this.

@wmertens
Copy link
Contributor Author

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

Copy link
Contributor

@Hirse Hirse left a 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.

.prettierignore Outdated Show resolved Hide resolved
.prettierrc Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
.prettierignore Outdated Show resolved Hide resolved
@wmertens
Copy link
Contributor Author

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?

@wmertens
Copy link
Contributor Author

oh, and do we want (err) => ... or err => ...? I've seen both styles.

Personally, I prefer tabs, no semicolumns, any chance of that happening? ;)

@wmertens
Copy link
Contributor Author

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.

@wmertens
Copy link
Contributor Author

🤔 looks like prettier on Windows always fails, the other tests just time out sometimes.
Possibly line endings?

@wmertens
Copy link
Contributor Author

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",
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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?

.prettierrc Show resolved Hide resolved
.prettierrc Outdated Show resolved Hide resolved
@wmertens
Copy link
Contributor Author

rebased, moved test into grunt, reverted arrowParens to default

@jung-kim
Copy link
Collaborator

Should prettier replace existing js-hint?

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.

@Hirse
Copy link
Contributor

Hirse commented Apr 28, 2020

@jung-kim Not quite.
JSHint and JSLint do "linting", i.e. static analysis, finding of potential errors in the code (consider unused variables and parameters) as well as general best practices (consider issues like using var instead of let/const.)
Both are superseded by ESLint.

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.

@wmertens
Copy link
Contributor Author

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.

Copy link
Collaborator

@campersau campersau left a 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.

Gruntfile.js Outdated Show resolved Hide resolved
.prettierrc Show resolved Hide resolved
@@ -1,3 +1,3 @@
* text=auto
* text=auto eol=lf
Copy link
Collaborator

Choose a reason for hiding this comment

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

package.json Show resolved Hide resolved
Gruntfile.js Outdated Show resolved Hide resolved
@wmertens wmertens force-pushed the prettier branch 2 times, most recently from 7f09a77 to e753177 Compare May 3, 2020 20:39
@wmertens
Copy link
Contributor Author

wmertens commented May 4, 2020

Changes applied. If you prefer, I can leave off the prettier commit so it will be easier to merge.

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:

git filter-branch --tree-filter 'git show $GIT_COMMIT --pretty= --name-only --diff-filter=AM -z | \
    xargs -r -0 prettier --config '"$PWD"'/../prettierrc --ignore-path '"$PWD"'/../prettierignore --write || true' -- --all

Copy link
Collaborator

@campersau campersau left a 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.

package.json Outdated Show resolved Hide resolved
Gruntfile.js Outdated Show resolved Hide resolved
@wmertens wmertens force-pushed the prettier branch 2 times, most recently from c4d08a4 to 6712526 Compare May 8, 2020 15:14
@wmertens
Copy link
Contributor Author

wmertens commented May 8, 2020

@campersau 👍 done

Copy link
Collaborator

@campersau campersau left a 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.

@wmertens
Copy link
Contributor Author

wmertens commented May 8, 2020

Should I add a prettier commit so people can see the result?

@campersau
Copy link
Collaborator

Yes, you can. Please run it twice!

.prettierignore Show resolved Hide resolved
Gruntfile.js Outdated Show resolved Hide resolved
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) => {
Copy link
Contributor

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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 ;.

Suggested change
console.error(stderr)
grunt.log.error(stderr);

@Hirse
Copy link
Contributor

Hirse commented May 8, 2020

After this PR (in its current state) I can see several different pattern that we might want to align:

  1. Grunt plugins
    less, watch, jshint
  2. Custom Grunt tasks calling JS APIs
    browserify (instead of grunt-browserify)
  3. Custom Grunt tasks calling CLI APIs
    prettier check
  4. Calling CLI APIs from npm scripts
    prettier format

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.

@wmertens
Copy link
Contributor Author

wmertens commented May 9, 2020

@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

@Hirse
Copy link
Contributor

Hirse commented May 9, 2020

@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.

@campersau campersau merged commit ac32c67 into FredrikNoren:master May 11, 2020
campersau pushed a commit that referenced this pull request May 11, 2020
* 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
@campersau campersau mentioned this pull request Jul 1, 2020
@wmertens wmertens deleted the prettier branch February 11, 2021 19:22
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.

5 participants