Skip to content

Improve formatting of .json and .yml files #1605

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

Open
wants to merge 21 commits into
base: trunk
Choose a base branch
from

Conversation

devansh016
Copy link
Contributor

@devansh016 devansh016 commented Oct 18, 2024

Summary

Fixes #1603

Relevant technical choices

The following files have been formatted by using npm run format-js
Note: .yml and .json files were not ignored to ensure they are properly formatted as well.

.github/dependabot.yml
.github/workflows/codeql-analysis.yml
.github/workflows/deploy-plugins.yml
.github/workflows/js-lint.yml
.github/workflows/php-lint.yml
.github/workflows/php-test-plugins.yml
.github/workflows/pr-validation.yml
.github/workflows/props-bot.yml
.github/workflows/spell-check.yml
.wp-env.json
composer.json
package-lock.json
package.json
plugins.json
plugins/performance-lab/.wordpress-org/blueprints/blueprint.json
tsconfig.json

@westonruter westonruter added no milestone PRs that do not have a defined milestone for release [Type] Bug An existing feature is broken labels Oct 18, 2024
@devansh016 devansh016 marked this pull request as ready for review October 18, 2024 05:04
Copy link

github-actions bot commented Oct 18, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: devansh016 <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: thelovekesh <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: phanduynam <[email protected]>
Co-authored-by: dilipom13 <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter
Copy link
Member

Let's also do linting of JSON and YML files in the lint-staged config so this doesn't happen again. Also, why isn't the formatting not causing a failure on the GHA checks?

@westonruter
Copy link
Member

Will Dependabot updates undo the changes to composer.json, package-lock.json, and package.json?

@devansh016
Copy link
Contributor Author

Also, why isn't the formatting not causing a failure on the GHA checks?

npm run lint-js doesn't check for .yml and .json file and hence not causing a failure on the GHA checks.

@thelovekesh
Copy link
Member

I think we squash and merge this one and include an ignore commit for the merge commit to hide it in git blame. Ref: https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view.

@swissspidy
Copy link
Member

We should exclude generated lock files from Prettier, just to avoid conflicts

@thelovekesh
Copy link
Member

We should exclude generated lock files from Prettier, just to avoid conflicts

Yes, we should ignore any auto-generated files. @devansh016 can you please add a https://prettier.io/docs/en/ignore.html with such files?

@westonruter
Copy link
Member

See also WordPress/gutenberg#30714 in which Gutenberg started applying formatting to JSON files.

@westonruter
Copy link
Member

Will Dependabot updates undo the changes to composer.json, package-lock.json, and package.json?

I think we still need to confirm Dependabot's behavior. We wouldn't want it constantly reverting tabs back to spaces.

@devansh016
Copy link
Contributor Author

Will Dependabot updates undo the changes to composer.json, package-lock.json, and package.json?

I think we still need to confirm Dependabot's behavior. We wouldn't want it constantly reverting tabs back to spaces.

Dependabot generally respects the indentation style in package.json when updating the lockfile so it should not revert tabs back to spaces. Ref

…ansh016/wp-performance into Format-files-using-npm-run-format-js
@phanduynam

This comment was marked as spam.

Comment on lines -9 to +14
indent_size = 4
tab_width = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
Copy link
Member

Choose a reason for hiding this comment

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

Why this change? The tab width for actual code (PHP and JS) should be 4, so I don't see why this needs to be removed here.

It doesn't apply to .json and .yml, but then we should have specific rules for these file types.

Copy link
Contributor Author

@devansh016 devansh016 Jun 3, 2025

Choose a reason for hiding this comment

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

@felixarntz
When both .editorconfig and .prettierrc are present, Prettier gives precedence to .prettierrc, which ensures PHP and JS files retain their intended 4-space/tab indentation.

I removed the global indent_size = 4 from .editorconfig since it was unintentionally being inherited by .json and .yml files. These files conventionally follow 2-space indentation, so I added specific overrides for them instead.

These updates follow the coding standards defined in the WordPress Core and Gutenberg repositories to stay aligned with WordPress development practices.

Please let me know if you'd like me to make any adjustments or if there's anything I might have missed. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Following up on this, I am still not sure about it.

You mention it was removed because it made .json and .yml files use the indent of 4, but that's exactly what is currently happening: All .json and .yml files use the indent of 4. So from that perspective, I don't see why having this here would be problematic.

Copy link
Contributor Author

@devansh016 devansh016 Jun 10, 2025

Choose a reason for hiding this comment

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

Looking back at this more carefully, I realize I may have overcomplicated the situation. The global indent_size = 4 shouldn't actually cause issues with .json and .yml files if we have proper Prettier configuration in place, since Prettier takes precedence for formatting.

But still we should keep the .editorconfig aligned with WordPress Core and Gutenberg repositories for consistency.

@felixarntz felixarntz requested a review from swissspidy April 14, 2025 17:15
@swissspidy swissspidy changed the title Formats json and yml files using npm run format Improve formatting of .json and .yml files Apr 15, 2025
@swissspidy
Copy link
Member

Note that there are now some merge conflicts too

@devansh016 devansh016 requested a review from westonruter as a code owner June 3, 2025 07:17
Copy link

codecov bot commented Jun 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.02%. Comparing base (d1663c9) to head (ebeefd3).
Report is 3 commits behind head on trunk.

Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #1605   +/-   ##
=======================================
  Coverage   68.02%   68.02%           
=======================================
  Files          92       92           
  Lines        7627     7627           
=======================================
  Hits         5188     5188           
  Misses       2439     2439           
Flag Coverage Δ
multisite 68.02% <ø> (ø)
single 36.98% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@devansh016 devansh016 requested a review from felixarntz June 3, 2025 07:58
.prettierrc.js Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Is this any different than the .prettierrc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, functionally they're identical in this case. Both .prettierrc.js and .prettierrc contain the same configuration options and produce the same formatting results.
We can remove .prettierrc.js here

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, it could be added to package.json:

"prettier": "@wordpress/prettier-config",

@westonruter
Copy link
Member

The following files are now being formatted when running npm run format-js:

.eslintrc.js 19ms
.github/dependabot.yml 11ms
.github/workflows/bump-wordpress-tested-up-to.yml 3ms
.github/workflows/codeql-analysis.yml 2ms
.github/workflows/deploy-plugins.yml 5ms
.github/workflows/e2e-test.yml 1ms
.github/workflows/js-lint.yml 1ms
.github/workflows/php-lint.yml 2ms
.github/workflows/php-test-plugins.yml 4ms
.github/workflows/pr-validation.yml 1ms
.github/workflows/props-bot.yml 2ms
.github/workflows/spell-check.yml 0ms
.prettierrc.js 2ms
.wp-env.json 1ms
.wp-env.override.json 1ms
bin/plugin/cli.js 6ms
bin/plugin/commands/changelog.js 11ms
bin/plugin/commands/readme.js 4ms
bin/plugin/commands/since.js 4ms
bin/plugin/commands/versions.js 2ms
bin/plugin/config.js 0ms
bin/plugin/lib/logger.js 1ms
bin/plugin/lib/milestone.js 2ms
codecov.yml 0ms
composer.json 1ms
lint-staged.config.js 1ms
package.json 0ms
plugins.json 0ms
plugins/auto-sizes/tests/e2e/specs/improve-calculate-sizes.spec.js 2ms
plugins/embed-optimizer/detect.js 1ms
plugins/embed-optimizer/lazy-load.js 1ms
plugins/image-prioritizer/detect.js 2ms
plugins/image-prioritizer/lazy-load-bg-image.js 1ms
plugins/image-prioritizer/lazy-load-video.js 1ms
plugins/image-prioritizer/tests/test-cases/preload-links-with-one-half-stale-group/url-metrics.json 6ms
plugins/optimization-detective/detect.js 11ms
plugins/optimization-detective/tests/data/url-metrics/tablet-viewport-half-stale.json 5ms
plugins/optimization-detective/types.ts 58ms
plugins/performance-lab/.wordpress-org/blueprints/blueprint.json 0ms
plugins/performance-lab/includes/admin/plugin-activate-ajax.js 2ms
plugins/view-transitions/js/types.ts 2ms
plugins/view-transitions/js/view-transitions.js 3ms
tools/e2e/playwright.config.ts 2ms
tools/webpack/utils.js 2ms
tsconfig.json 0ms
webpack.config.js 4ms

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

This LGTM. @swissspidy could you second?

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@devansh016 Thank you for the iterations.

I'm still slightly confused by some of the changes in this PR, which seem misaligned / inconsistent in itself.

Comment on lines -9 to +14
indent_size = 4
tab_width = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
Copy link
Member

Choose a reason for hiding this comment

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

Following up on this, I am still not sure about it.

You mention it was removed because it made .json and .yml files use the indent of 4, but that's exactly what is currently happening: All .json and .yml files use the indent of 4. So from that perspective, I don't see why having this here would be problematic.

.editorconfig Outdated
Comment on lines 16 to 18
[*.{yml,yaml}]
indent_style = space
indent_size = 2
Copy link
Member

Choose a reason for hiding this comment

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

This is confusing to me. As far as I can tell, all the .yml files were now updated to use indent size 4, which looks right. But then why does this say indent size 2 here? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right about the inconsistency. Looking at the actual formatting changes in this PR, all .yml files are indeed using an indent size of 4, which aligns with the existing codebase standards.

The indent_size = 2 in the .editorconfig is misleading since Prettier takes precedence and formats these files with 4-space indentation. I initially copied this configuration from the Gutenberg and Core repositories, but it seems now .editorconfig file may not be updated correctly in them.

For consistency and clarity, we should update this to indent_size = 4 to accurately reflect how the files are actually being formatted. This way, the .editorconfig and the actual file formatting are aligned, even though Prettier is the one enforcing the formatting rules.

Would you prefer I update it to indent_size = 4, or we could remove the YAML-specific configuration entirely since Prettier is handling the formatting?

Copy link
Member

Choose a reason for hiding this comment

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

Probably add indent_size = 4 since then the editor will automatically format it properly without requiring Prettier to run, potentially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running npm run format-js formats more files than expected
7 participants