-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Prettier: Update format-js to use default config, and update editor docs usage #20036
Conversation
It would be great to relax this check: gutenberg/packages/scripts/scripts/format-js.js Lines 73 to 83 in 056d7c1
If no Prettier config found, we can always defer to the default config provided in the config folder inside the package. |
@gziolo Updates see it doesn't quit when no config, switched it to provide the config using the fromConfigRoot. This works when running the format-js script. However, the pre-commit hook is using something else, eslint-plugin-prettier, so after deleting the .prettierrc.js file it uses the default config is incorrect |
I think I have an idea how to fix it. You can provide |
Quick and dirty but works: diff --git a/packages/eslint-plugin/configs/recommended.js b/packages/eslint-plugin/configs/recommended.js
index 9e1659685..234d0dbbf 100644
--- a/packages/eslint-plugin/configs/recommended.js
+++ b/packages/eslint-plugin/configs/recommended.js
@@ -1,7 +1,22 @@
+// TODO: Use the actual function that detects whether Prettier config exists.
+const hasProjectPrettierConfig = false;
+const prettierOptions = hasProjectPrettierConfig
+ ? {}
+ : require( '../../scripts/config/.prettierrc' );
+
module.exports = {
extends: [
require.resolve( './recommended-with-formatting.js' ),
'plugin:prettier/recommended',
'prettier/react',
],
+ rules: {
+ 'prettier/prettier': [
+ 'error',
+ prettierOptions,
+ {
+ usePrettierrc: false,
+ },
+ ],
+ },
}; Although, now that I think about it. Maybe we should apply this rule inside the config provided for |
The reason I added this check is to make editor integrations more reliable. If you have a Prettier plugin in your editor and execute the "format with Prettier" command, the editor will search for Prettier config in the default location. The editor has no knowledge about the To ensure that both |
I don't think this PR is as necessary, the top-level .prettierrc.js is loading from scripts already using:
|
Yes, for Gutenberg it's all fine. As usual 😄 The issue is when you are using |
@gziolo Ok, so I think not deleting the top-level will fix the issue. |
Yes, it fixes the issue because ESLint will use it in Gutenberg for linting. I guess it's enough to move forward and we can discuss the issue with |
… on workplace settings
It's fine but it makes it hard to use outside of Gutenberg. That's why @mkaz included detailed docs how to setup Prettier with Visual Studio Code and linked to Pretier docs for other editors. I admit it isn't perfect and your proposal makes a lot of sense. However, the issue is with The next steps after this PR lands would be to conditionally load the shared Prettier config for In addition, the check you implemented could be added as part of the script execution but maybe it should be more like the information shared or prompt to action that automates the process of IDE integration. |
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.
Let's wait also for @jsnajdr to ensure we are fine with the plan I outlined.
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.
Yes, this looks correct 👍
I opened #20071 with the fix for The last remaining task is to improve integration with IDEs. We probably should cover more commands:
I need to test with https://github.com/WordPress/gutenberg-examples. |
Description
Updates format-js instead of failing if it does not find a default config, to use the default in the packages/scripts config. This supports using the script outside of the Gutenberg project where a default config might not already exist, for example developing a block plugin.
Updates documentation, clarifying editor usage, including part about workpsace settings in Visual Studio Code and adds link to other editor integrations.
How has this been tested?
npm run format-js
works as expectedformat-js
script without a prettier config, and should still workTypes of changes
Updates format-js script moving the check to configure the default config and not fail.
Minor update to getting started docs.