-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Enforce valid function names in the packages/block-library/src/*/*.php files #53438
Conversation
See my comment in the parent issue: #52769 (comment). I think it's fine to add code comments that ignore existing violations and enforce good practices moving on. |
It seems to be in a good shape, let me know when I can give a final review and approve. |
72d0e3f
to
b7583d3
Compare
packages/block-library/*/src/index.php
files
packages/block-library/*/src/index.php
files
I had to address an edge case and write proper testing instructions. |
@@ -87,11 +87,13 @@ function render_block_core_avatar( $attributes, $content, $block ) { | |||
/** | |||
* Generates class names and styles to apply the border support styles for | |||
* the Avatar block. | |||
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName |
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.
This and other files are going to be copied to WordPress core. Should this rule be present in both places and get prefixed with WordPress.
? Maybe it's fine to have it only in Gutenberg, just flagging the fact that these files are tricky to maintain.
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.
@gziolo To use the WordPress.
in the rule's name, it would need to become a part of the WordPress-Coding-Standards package (wpcs
). However, getting this rule into the wpcs
package might be challenging.
wpcs
is currently undergoing a major overhaul. It's possible that you may need to wait for an undefined period of time until it's released.
Since this rule is currently essential for Gutenberg, my suggestion is to commit it to Gutenberg first. If the wpcs
contributors agree that it should be included in wpcs
, it can always be removed from the Gutenberg package.
To address the concern about maintainability, I will relocate the //phpcs:disable
and //phpcs:enable
statements from the doc blocks to prevent any interference.
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.
We might have linting disabled on files copied from Gutenberg through WP packages, so you can double check that first before applying any changes. I guess we can live with the Gutenberg.
specific rule if it doesn't throw errors on CI in WP core.
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.
@gziolo
The //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName
statements shouldn't generate any linter errors in Core. However, personally, I'm not fond of the idea of having these comments in Core code.
I think I will add the possibility to ignore specific functions via the linter rule's configuration in phpcs.xml.dist. This way, we can avoid cluttering Core code with these phpcs: statements. I'll make the necessary updates to the PR and let you know.
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, we could list these function names as exceptions in the config file. Not ideal, but it should work.
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've updated the PR.
So it can be reviewed now.
Thanks!
@@ -144,12 +144,14 @@ function block_core_navigation_add_directives_to_submenu( $w, $block_attributes | |||
|
|||
/** | |||
* Replaces view script for the Navigation block with version using Interactivity API. | |||
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName | |||
* | |||
* @param array $metadata Block metadata as read in via block.json. | |||
* | |||
* @return array Filtered block type metadata. | |||
*/ | |||
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) { |
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 a little bit confused here with the gutenberg_
prefix and the usage of gutenberg_should_block_use_interactivity_api
. The latter will trigger a fatal error in WordPress core when the files gets copied. This code should leave outside of this file, so in that regard the lining rule confirms that it's very useful.
Aside, we should also enforce that you can't use functions prefixed with gutenberg_
with core blocks in the same set of files as they don't exist in WP core.
As for this PR, it's fine to proceed with the ignore comment, but the usage of gutenberg_should_block_use_interactivity_api
needs to be replaced with code present in WP core before we start backporting changes to WP core. @luisherranz, are you aware of this issue?
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.
Aside, we should also enforce that you can't use functions prefixed with gutenberg_ with core blocks in the same set of files as they don't exist in WP core.
Sure. This can enforced. I will update the PR, @gziolo.
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.
@luisherranz, are you aware of this issue?
Not at all. I don't have any experience backporting stuff to WP core yet, nor with how backporting requirements affect the code of Gutenberg. But happy to fix things if you give me instructions on what needs to be done, tho.
cc: @westonruter (pinging because Weston introduced the gutenberg_should_block_use_interactivity_api
function, just in case)
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.
Related to that, I think it could be valuable to include a mandatory banner in every PHP file we know gets backported to WordPress core with the link to the document explaining all the expected requirements enforced by the complex process. It will also give a hint to folks whenever they try to update those files directly in the WordPress core which is going to be replaced the next time someone runs npm run build
...
Obviously not in this PR.
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.
Sure. This can enforced. I will update the PR, @gziolo.
I didn't realize that the linting rule doesn't allow any other functions to be defined outside of the ones specified in the prefixes
config property.
Therefore, there's no need to modify the PR to restrict the gutenberg_
prefixed functions. 🤦♂️
The linting rule will only permit functions that start with the block_core_
, render_block_core_
, and register_block_core_ prefixes
(+ the directory name).
The two functions with the gutenberg_
prefix (gutenberg_block_core_file_update_interactive_view_script
and gutenberg_block_core_navigation_update_interactive_view_script
) were allowed because of the phpcs::disable
statements before them.
Given the discussion above, I'm uncertain about the course of action for these two gutenberg_
prefixed functions. Should they be allowed for now?
<property name="prefixes" type="array"> | ||
<element value="block_core_"/> | ||
<element value="render_block_core_"/> | ||
<element value="register_block_core_"/> |
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 see functions that start build_template_part_
, so maybe we could open also build_block_core_template_part
in case folks want to use that. I don't have strong opinions on that, as they could also do block_core_tempalte_part_build
.
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 don't have a preference either. However, please note that the linting rule not only checks the prefix name but also the directory name, as requested here.
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.
It's fine as is for now, but we should keep monitoring and extending the list if that is useful.
@@ -85,12 +85,14 @@ function register_block_core_footnotes() { | |||
|
|||
/** | |||
* Saves the footnotes meta value to the revision. | |||
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName |
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.
Unrelated to this PR. I'm slightly surprised that the Footnotes block works correctly with WordPress 6.3. We don't guard calling these WP hooks when they are present in the WP core, so essential we register them twice with the Gutenberg plugin. We should double-check it and if necessary move all the code that handles WP hooks to another file in lib/compat/
with some sort of check if it was already applied and override them only if necessary. At the same time, we would have to move the same code to a different place in WordPress core. @ellatrix, what do you think?
Flaky tests detected in 62d4d53. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5833436112
|
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 like how the new rule works after a few refinements. In a follow-up we should include guidelines for contributing in @wordpress/block-library
that explain how functions should be named. There is already a section for that here. We could use that later to reference in the message that the linting rule prints when an error gets detected.
I'm probably not the best person to review the code implementing the rule, so feel free to wait for another review from someone better suited for that task.
Thank you for the code review, @gziolo. |
Yes, it should be all good, but I wanted to set proper expectations regarding my review. The plan sounds perfect 😄 |
…ith an underscore.
f5be78b
to
1c25491
Compare
What?
This PR is focused on implementing a new PHPCS sniff, namely
Gutenberg.NamingConventions.ValidBlockLibraryFunctionName
.Fixes #52769.
Why?
All the PHP function names within the
./packages/block-library/src/*/*.php
files should start with one of the following prefixes:block_core_<folder_name>
render_block_core_<folder_name>
register_block_core_<folder_name>
Here,
<folder_name>
represents the name of the folder where the associated file is situated. The folder name is converted to lowercase, and any characters other than letters and digits are substituted with underscores.The introduction of this new PHPCS sniff is needed to ensure strict adherence to this naming pattern.
How?
The proposed
ValidBlockLibraryFunctionNameSniff
is configured to examine PHP function names contained within the./packages/block-library/src/*/*.php
files.Whenever a function name deviates from the naming convention listed above, the PHPCS sniff will add an error to the PHPCS report.
Testing Instructions
packages/block-library/src/calendar/index.php
file in your preferred code editor.npm run lint:php
orcomposer run lint
, based on your environment setup).block_core_clndr
.block_core_calendar
.render_block_core_calendar
andregister_block_core_calendar
.