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

Add a simple linter and a pre-commit hook for it #3029

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

acicovic
Copy link
Collaborator

@acicovic acicovic commented Dec 13, 2024

Description

With this PR we're introducing a very simplistic in-house linter, with optional auto-fixing support for simple linting violations. The linter has been included as a pre-commit hook and currently supports 2 rules related to unneeded extra lines.

We used php lint.php --fix to auto-fix all the linting violations present in this PR.
The linter also supports just displaying any violations, using the php lint.php command.

Know issues:

  • The linter uses ANSI colors in its output that look great in my terminal, but don't look good in the VSCode terminal (the markup is shown instead of the colors). This isn't important because in the end of the day, our goal is still achieved. Apparently, there are VSCode extensions that can improve the situation but I didn't dig further.
  • We don't show any output in --fix mode, as I didn't want to take more time with this. Again, we can live without this and it can be adjusted in the future.
  • If we add lots of rules in the future, there might be more performant ways to do certain things. Currently not a problem, as it is blazingly fast.

Motivation and context

  • I've been noticing a few extra blank lines in our codebase here and there. However, it's unproductive to fix them manually, and they can slip through the cracks during code reviews. Introducing the linter and including it as a pre-commit hook should solve this issue.
  • By introducing this, we now have the option create new linting rules in the future.

How has this been tested?

  • Ran the linter multiple times in display mode.
  • Ran the linter in auto-fix mode to get the fixes, verifying that there weren't any unwanted side-effects.

Screenshots

Our linter in display mode:

image

Summary by CodeRabbit

  • New Features

    • Added persona and tone settings for the PCH Excerpt Suggestions feature.
    • Integrated the Excerpt Suggestions feature into the PCH sidebar.
    • Enhanced the PCH Performance Stats to display statistics for posts with a trackable status.
    • Introduced a new Recommendations Block.
  • Bug Fixes

    • Resolved AMP validation issues.
    • Fixed deprecation warnings in the browser console.
  • Tests

    • Expanded test coverage for API credentials validation and Parse.ly Stats column behavior.
  • Documentation

    • Updated changelog to reflect changes in version 3.17.0.

@acicovic acicovic added this to the 3.18.0 milestone Dec 13, 2024
@acicovic acicovic self-assigned this Dec 13, 2024
Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

📝 Walkthrough

Walkthrough

The pull request introduces enhancements to the pre-commit process by adding a series of linting and analysis commands in the .husky/pre-commit hook. Additionally, it updates the changelog for version 3.17.0 of the Parsely WordPress plugin, reflecting new features, functional changes, and bug fixes. A new linter file is added to detect and fix code style violations, and various classes and methods across the codebase are modified to improve functionality and maintainability, including enhancements to REST API endpoints and testing coverage.

Changes

File Change Summary
.husky/pre-commit Added commands for PHP linting, JavaScript linting, Composer coding standards check, and PHPStan static analysis.
CHANGELOG.md Updated for version 3.17.0; added new features for PCH Excerpt Suggestions, refactored internal API, migrated tests to Playwright, and fixed various issues.
lint.php Introduced a linter class with methods for detecting and fixing code style violations across multiple file types.
src/@types/assets/window.d.ts Added optional properties to the global Window interface for new features.
src/Telemetry/Tracks/class-tracks-pixel.php No significant changes; retained existing methods for event handling.
src/content-helper/common/class-content-helper-feature.php Added a method for credential messages; updated inline script injection logic.
src/content-helper/common/components/input-range/style.scss Removed empty lines; no other style changes.
src/content-helper/editor-sidebar/performance-stats/performance-stats.scss Updated styles for performance panel components, including gaps and hover effects.
src/content-helper/editor-sidebar/related-posts/related-posts.scss Added new styles and structural modifications for related posts layout.
src/rest-api/content-helper/class-endpoint-smart-linking.php Added new REST API routes and updated methods for smart link handling.
src/rest-api/settings/class-base-settings-endpoint.php Added a method for retrieving nested settings specifications.
src/services/class-base-api-service.php Added a method to access the Parsely instance.
src/services/content-api/endpoints/class-endpoint-analytics-posts.php Enhanced logic for handling API request parameters.
tests/Integration/ContentHelper/ContentHelperPostListStatsTest.php Added new tests for Parse.ly Stats column behavior under various conditions.
tests/Integration/RestAPI/BaseAPIControllerTest.php Introduced an anonymous class to test protected methods of the Base_API_Controller.
tests/Integration/RestAPI/ContentHelper/ContentHelperFeatureTestTrait.php Added abstract methods for testing feature availability based on user roles.
tests/Integration/UI/SettingsPageTest.php Added tests for API credentials validation and settings handling.
wp-parsely.php Updated initialization functions for various components and added a new Recommendations Block.

Possibly related PRs

Suggested labels

github_actions

Suggested reviewers

  • vaurdan

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@acicovic acicovic marked this pull request as ready for review December 13, 2024 15:51
@acicovic acicovic requested a review from a team as a code owner December 13, 2024 15:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
lint.php (2)

45-45: Prefix class names to adhere to WordPress coding standards

According to WordPress coding standards, class names should be prefixed with a unique project-specific prefix to prevent potential naming conflicts. Consider renaming Lint_Rule to include a prefix specific to your project.

Apply this diff to rename the class:

-class Lint_Rule {
+class YourPrefix_Lint_Rule {

175-175: Ensure inline comments end with a period

The inline comment on line 175 does not end with a period. As per WordPress documentation standards, all inline comments should conclude with a period.

Apply this diff to correct the issue:

-						return false; // Skip excluded directories
+						return false; // Skip excluded directories.
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03a4eef and 76fbe9c.

📒 Files selected for processing (18)
  • .husky/pre-commit (1 hunks)
  • CHANGELOG.md (0 hunks)
  • lint.php (1 hunks)
  • src/@types/assets/window.d.ts (0 hunks)
  • src/Telemetry/Tracks/class-tracks-pixel.php (0 hunks)
  • src/content-helper/common/class-content-helper-feature.php (0 hunks)
  • src/content-helper/common/components/input-range/style.scss (0 hunks)
  • src/content-helper/editor-sidebar/performance-stats/performance-stats.scss (0 hunks)
  • src/content-helper/editor-sidebar/related-posts/related-posts.scss (0 hunks)
  • src/rest-api/content-helper/class-endpoint-smart-linking.php (0 hunks)
  • src/rest-api/settings/class-base-settings-endpoint.php (0 hunks)
  • src/services/class-base-api-service.php (0 hunks)
  • src/services/content-api/endpoints/class-endpoint-analytics-posts.php (0 hunks)
  • tests/Integration/ContentHelper/ContentHelperPostListStatsTest.php (0 hunks)
  • tests/Integration/RestAPI/BaseAPIControllerTest.php (0 hunks)
  • tests/Integration/RestAPI/ContentHelper/ContentHelperFeatureTestTrait.php (0 hunks)
  • tests/Integration/UI/SettingsPageTest.php (0 hunks)
  • wp-parsely.php (0 hunks)
💤 Files with no reviewable changes (16)
  • src/content-helper/common/components/input-range/style.scss
  • src/services/class-base-api-service.php
  • src/content-helper/common/class-content-helper-feature.php
  • src/@types/assets/window.d.ts
  • src/content-helper/editor-sidebar/related-posts/related-posts.scss
  • src/content-helper/editor-sidebar/performance-stats/performance-stats.scss
  • src/rest-api/settings/class-base-settings-endpoint.php
  • src/Telemetry/Tracks/class-tracks-pixel.php
  • tests/Integration/RestAPI/BaseAPIControllerTest.php
  • src/services/content-api/endpoints/class-endpoint-analytics-posts.php
  • wp-parsely.php
  • tests/Integration/RestAPI/ContentHelper/ContentHelperFeatureTestTrait.php
  • CHANGELOG.md
  • tests/Integration/ContentHelper/ContentHelperPostListStatsTest.php
  • tests/Integration/UI/SettingsPageTest.php
  • src/rest-api/content-helper/class-endpoint-smart-linking.php
🧰 Additional context used
📓 Path-based instructions (1)
lint.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (1)
.husky/pre-commit (1)

1-4: Pre-commit hooks are properly configured

The added pre-commit commands enhance code quality checks and align with best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant