Skip to content

Upgrade RuboCop cops to v1 API #3512

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

Merged
merged 11 commits into from
Jun 10, 2025
Merged

Upgrade RuboCop cops to v1 API #3512

merged 11 commits into from
Jun 10, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 2, 2025

This PR upgrades all RuboCop cops in the repository to use the RuboCop v1 API, eliminating deprecation warnings and ensuring compatibility with modern RuboCop versions.

Changes Made

Class Inheritance Updates

  • Updated all cops to inherit from RuboCop::Cop::Base instead of the deprecated RuboCop::Cop::Cop
  • Affected files:
    • lib/rubocop/cop/primer/base_cop.rb
    • lib/rubocop/cop/primer/no_tag_memoize.rb
    • lib/rubocop/cop/primer/primer_octicon.rb
    • lib/primer/view_components/linters/migrations/iconbutton_component.rb
    • lib/primer/view_components/linters/migrations/truncate_component.rb

Autocorrect Method Migration

  • Replaced deprecated autocorrect methods with the new RuboCop v1 pattern:
    • Added extend AutoCorrector to cop classes
    • Moved correction logic into add_offense blocks
  • Updated 8 cops with autocorrect functionality:
    • ComponentNameMigration
    • PrimerOcticon
    • IconButtonComponent and TruncateComponent migrations
    • DeprecatedArguments, DeprecatedButtonArguments
    • DeprecatedLabelSchemes, DeprecatedLabelVariants

Test Framework Updates

  • Updated CopTestCase to work with RuboCop v1's new Commissioner API
  • Ensured backward compatibility for existing test methods accessing cop.offenses

Before/After

Before (RuboCop v0 style):

class MyCop < RuboCop::Cop::Cop
  def on_send(node)
    add_offense(node, message: "Bad code")
  end

  def autocorrect(node)
    lambda do |corrector|
      corrector.replace(node, "good_code")
    end
  end
end

After (RuboCop v1 style):

class MyCop < RuboCop::Cop::Base
  extend AutoCorrector

  def on_send(node)
    add_offense(node, message: "Bad code") do |corrector|
      corrector.replace(node, "good_code")
    end
  end
end

Testing

  • All existing RuboCop cop tests pass
  • No RuboCop deprecation warnings remain
  • Autocorrect functionality verified for all migrated cops

Fixes #3351.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link

changeset-bot bot commented Jun 2, 2025

🦋 Changeset detected

Latest commit: 6c151a2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/view-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Copilot Copilot AI changed the title [WIP] Upgrade rubocop cops to v1 Upgrade RuboCop cops to v1 API Jun 2, 2025
@Copilot Copilot AI requested a review from jonrohan June 2, 2025 20:41
Copilot finished work on behalf of jonrohan June 2, 2025 20:42
Copy link
Contributor

github-actions bot commented Jun 2, 2025

⚠️ Visual or ARIA snapshot differences found

Our visual and ARIA snapshot tests found UI differences. Please review the differences by viewing the files changed tab to ensure that the changes were intentional.

Review differences

@jonrohan
Copy link
Member

jonrohan commented Jun 3, 2025

@copilot can you revert the commit 5a126a1 in this PR and then merge main into the branch?

Copilot AI added 2 commits June 3, 2025 19:04
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Copy link
Contributor Author

Copilot AI commented Jun 3, 2025

@copilot can you revert the commit 5a126a1 in this PR and then merge main into the branch?

I've reverted commit 5a126a1 and merged main into the branch as requested. The changes are now available in commit d648739.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://168.63.129.16:80/machine/
    • Triggering command: /usr/bin/python3 -u bin/WALinuxAgent-2.13.1.1-py3.9.egg -collect-logs (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of jonrohan June 3, 2025 19:08
@jonrohan jonrohan marked this pull request as ready for review June 5, 2025 21:20
@Copilot Copilot AI review requested due to automatic review settings June 5, 2025 21:20
@jonrohan jonrohan requested a review from a team as a code owner June 5, 2025 21:20
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates all custom RuboCop cops to the v1 API by switching to the new RuboCop::Cop::Base base class, adding extend AutoCorrector and migrating deprecated autocorrect methods into add_offense blocks. It also adapts the test helper to use RuboCop’s Commissioner API.

  • Inherit from RuboCop::Cop::Base and add extend AutoCorrector in all cops
  • Move autocorrect logic into add_offense blocks and remove old autocorrect methods
  • Update CopTestCase to capture offenses from RuboCop v1’s Commissioner

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

File Description
test/lib/cop_test_case.rb Adapted investigate to use the new Commissioner API
lib/rubocop/cop/primer/primer_octicon.rb Switched to Base, added autocorrect block, cleaned up formatting
lib/rubocop/cop/primer/deprecated_label_schemes.rb Added autocorrect block, updated inheritance, minor formatting
Comments suppressed due to low confidence (2)

Comment on lines +23 to +28
result = commissioner.investigate(processed_source)
# Store offenses in the cop for backward compatibility with existing tests
def cop.offenses
@offenses ||= []
end
cop.instance_variable_set(:@offenses, result.offenses)
Copy link
Preview

Copilot AI Jun 5, 2025

Choose a reason for hiding this comment

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

The investigate method returns nil in RuboCop v1, so result.offenses will raise a NoMethodError. Instead, call commissioner.investigate(processed_source) without assignment and extract offenses from the cop instance (e.g., use cop.instance_variable_get(:@offenses) directly).

Suggested change
result = commissioner.investigate(processed_source)
# Store offenses in the cop for backward compatibility with existing tests
def cop.offenses
@offenses ||= []
end
cop.instance_variable_set(:@offenses, result.offenses)
commissioner.investigate(processed_source)
# Store offenses in the cop for backward compatibility with existing tests
def cop.offenses
@offenses ||= []
end
cop.instance_variable_set(:@offenses, cop.instance_variable_get(:@offenses))

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@joelhawksley joelhawksley left a comment

Choose a reason for hiding this comment

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

This seems... fine? I think we should of course verify these changes manually.

@@ -54,3 +54,4 @@ demo/public/vite-test/
demo/public/vite-dev/
demo/public/vite/
demo/app/assets/builds/
File::NULL
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this for?

Copy link
Member

Choose a reason for hiding this comment

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

$stderr.reopen(File.new("File::NULL", "w"))

After running rake, there was a file in my workspace, so I added to gitignore

Copy link
Contributor

Choose a reason for hiding this comment

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

Wild. Never seen it before!

@jonrohan jonrohan enabled auto-merge June 5, 2025 23:37
@jonrohan jonrohan disabled auto-merge June 10, 2025 18:17
@jonrohan jonrohan merged commit b606322 into main Jun 10, 2025
29 checks passed
@jonrohan jonrohan deleted the copilot/fix-3351 branch June 10, 2025 18:29
@primer primer bot mentioned this pull request Jun 10, 2025
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.

Upgrade rubocop cops to v1
3 participants