Run phpcbf and phpcs before commit#177
Conversation
|
JS linting is failing on this PR since it seems the implementation is a little broken. Linting uses prettier, but it isn't been defined in I can create a separate PR to fix it or fix it in this PR itself whichever seems to be a better way. |
|
@eclarke1 Can you please add a "needs review" label and add @felixarntz as a reviewer? |
|
@kirtangajjar absolutely! All updated, thank you so much for reaching out. |
|
@dainemawer @mitogh Thank you for the feedback! I have made the changes. Please have a look at it again. |
|
@kirtangajjar about #177 (comment) I guess the checks was failing because was missing the .eslintignore file with this content: |
|
@erikyo I think that's happening because we haven't added prettier in P.S. Gutenberg has it too. |
|
that strange since prettier is a direct dependency of @wordpress/eslint-plugin the |
|
@kirtangajjar this is a working setup for actions inspired to the performance repo i've done today, and the only dependency is the maybe isn't working because the outdated wpscript version? prettier is a dependency of multiple modules and to me it is not the cause of the failed test |
|
@erikyo As you can see above, I just updated the version of As per their blog
My guess is this is why Gutenberg has prettier explicitly in its dependency too for this exact same reason. I have used wp-prettier instead of prettier as it's a fork of prettier which is also used by Gutenberg. The changes made by WP team is documented in its readme: |
|
I found out that the current implementation wasn't working for me in my local env. The problem was that we were trying to run the lint command inside of the container with When we commit any change, To remediate this, I think the best solution is to just run the command directly on the host. I think it's fair to assume most people will have composer and phpcs installed globally. So I have gone ahead with the change for now. @mitogh @dainemawer LMK if you have any feedback regarding this. |
|
I think that's a fair assumption @kirtangajjar, however I tested this locally and the husky command was not running when I staged some files. When running the script
|
|
Yeah agreed, I think thats solid. Nice work @kirtangajjar ! |
The husky command will run after you commit anything(because of the pre-commit hook). However, for testing purposes, staging some changes and running
That's really strange. The configuration of I performed the following steps in a newly cloned repo and it was running fine for me: I tried these same steps on a remote machine as well and it worked there too. |
Then this is a post-commit hook, not a pre-commit as the title of this PR indicates, from my perspective, this should run before the commit to ensuring all the appropriate lints are executed before a commit. @kirtangajjar Looks like had to pull out from your fork as your branch was not found locally, the linting happen correctly before the commit and when running the standalone script So all good from my end @kirtangajjar |
felixarntz
left a comment
There was a problem hiding this comment.
@kirtangajjar One question here, other than that lgtm.
package.json
Outdated
| "husky": "^7.0.4", | ||
| "lint-staged": "^12.3.4", | ||
| "lodash": "4.17.21", | ||
| "prettier": "npm:wp-prettier@^2.2.1-beta-1" |
There was a problem hiding this comment.
What's the reasoning for using a wp-prettier beta version here for the prettier dependency? That looks potentially problematic to me 🤔
There was a problem hiding this comment.
Hey @felixarntz,
I explained that choice in this comment. Pasting relevant contents here for easy reference:
I have used wp-prettier instead of prettier as it's a fork of prettier which is also used by Gutenberg. The changes made by the WP team is documented in its readme:
This is a fork of Prettier that adds a new command-line option --paren-spacing which inserts many extra spaces inside parentheses, the way how projects in the WordPress ecosystem (Calypso, Gutenberg, etc.) like to format their code.
Basically. we needed prettier for dependency and then I checked Gutenberg repo and they were using this exact version, so I used the same one.
I don't think the beta tag in the version should be a problem as this package is an internal fork of prettier maintained by WordPress. Also, the given version, 2.2.1-beta-1, was released 1 year ago and has been used by Gutenberg since then so I think it's safe to use.
felixarntz
left a comment
There was a problem hiding this comment.
@kirtangajjar This looks good to me. Can you resolve the merge conflict here?
|
@felixarntz Thank you. The merge conflicts have been resolved! |
|
@kirtangajjar Apologies, this just now got a second approval and there is now another merge conflict to resolve due to the version number change. @eugene-manuilov has rightfully raised this in #252 so that this won't happen again in the future. |
Summary
Fixes #175
Adds support for running
phpcbfandphpcsbefore commit usinghuskyandlint-stagednpm packages.Husky is a tool used to manage git hooks and lint-staged ensures that phpcs/phpcbf is only run on staged files, not the whole repo.
This git hook will first run
phpcbfto fix all phpcs errors it can fix. Then it will runphpcsto display errors that it cannot fix.If there are phpcs issues that the author has not fixed, it won't allow them to commit.