Skip to content

Commit b606322

Browse files
Copilotjonrohan
andauthored
Upgrade RuboCop cops to v1 API (#3512)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jonrohan <[email protected]> Co-authored-by: Jon Rohan <[email protected]> Co-authored-by: Jon Rohan <[email protected]>
1 parent 223a4c6 commit b606322

16 files changed

+83
-86
lines changed

.changeset/neat-taxis-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/view-components": patch
3+
---
4+
5+
Upgrade RuboCop cops to v1 API

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ demo/public/vite-test/
5454
demo/public/vite-dev/
5555
demo/public/vite/
5656
demo/app/assets/builds/
57+
File::NULL

Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ group :development do
5454
gem "capybara", "~> 3.40.0"
5555
gem "cuprite", "~> 0.15"
5656
gem "debug"
57-
gem "erb_lint", "~> 0.6"
58-
gem "erblint-github", "~> 1.0"
57+
gem "erb_lint", "~> 0.9"
58+
gem "erblint-github", "~> 1.0.1"
5959
gem "listen", "~> 3.9"
6060
gem "matrix", "~> 0.4.2"
6161
gem "mocha"
6262
gem "rubocop"
63-
gem "rubocop-github", "~> 0.20.0"
63+
gem "rubocop-github", "~> 0.25.0"
6464
gem "rubocop-performance"
6565
gem "rubocop-rails"
6666
gem "rubocop-rails-accessibility", "~> 1.0"

Gemfile.lock

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ GEM
4646
allocation_stats (0.1.5)
4747
ansi (1.5.0)
4848
ast (2.4.3)
49-
base64 (0.2.0)
49+
base64 (0.3.0)
5050
benchmark (0.4.1)
5151
benchmark-ips (2.14.0)
5252
better_html (2.1.1)
@@ -56,7 +56,7 @@ GEM
5656
erubi (~> 1.4)
5757
parser (>= 2.4)
5858
smart_properties
59-
bigdecimal (3.1.9)
59+
bigdecimal (3.2.2)
6060
bootsnap (1.18.6)
6161
msgpack (~> 1.2)
6262
builder (3.3.0)
@@ -87,9 +87,9 @@ GEM
8787
irb (~> 1.10)
8888
reline (>= 0.3.8)
8989
docile (1.4.1)
90-
drb (2.2.1)
90+
drb (2.2.3)
9191
dry-cli (1.2.0)
92-
erb_lint (0.6.0)
92+
erb_lint (0.9.0)
9393
activesupport
9494
better_html (>= 2.0.1)
9595
parser (>= 2.7.1.4)
@@ -210,33 +210,34 @@ GEM
210210
io-console (~> 0.5)
211211
rexml (3.4.1)
212212
rouge (4.5.2)
213-
rubocop (1.75.8)
213+
rubocop (1.76.0)
214214
json (~> 2.3)
215215
language_server-protocol (~> 3.17.0.2)
216216
lint_roller (~> 1.1.0)
217217
parallel (~> 1.10)
218218
parser (>= 3.3.0.2)
219219
rainbow (>= 2.2.2, < 4.0)
220220
regexp_parser (>= 2.9.3, < 3.0)
221-
rubocop-ast (>= 1.44.0, < 2.0)
221+
rubocop-ast (>= 1.45.0, < 2.0)
222222
ruby-progressbar (~> 1.7)
223223
unicode-display_width (>= 2.4.0, < 4.0)
224-
rubocop-ast (1.44.1)
224+
rubocop-ast (1.45.0)
225225
parser (>= 3.3.7.2)
226226
prism (~> 1.4)
227-
rubocop-github (0.20.0)
228-
rubocop (>= 1.37)
229-
rubocop-performance (>= 1.15)
230-
rubocop-rails (>= 2.17)
227+
rubocop-github (0.25.0)
228+
rubocop (>= 1.72)
229+
rubocop-performance (>= 1.24)
230+
rubocop-rails (>= 2.23)
231231
rubocop-performance (1.25.0)
232232
lint_roller (~> 1.1)
233233
rubocop (>= 1.75.0, < 2.0)
234234
rubocop-ast (>= 1.38.0, < 2.0)
235-
rubocop-rails (2.25.0)
235+
rubocop-rails (2.32.0)
236236
activesupport (>= 4.2.0)
237+
lint_roller (~> 1.1)
237238
rack (>= 1.1)
238-
rubocop (>= 1.33.0, < 2.0)
239-
rubocop-ast (>= 1.31.1, < 2.0)
239+
rubocop (>= 1.75.0, < 2.0)
240+
rubocop-ast (>= 1.44.0, < 2.0)
240241
rubocop-rails-accessibility (1.0.1)
241242
rubocop (>= 1.0.0)
242243
ruby-progressbar (1.13.0)
@@ -323,8 +324,8 @@ DEPENDENCIES
323324
cssbundling-rails (~> 1.4)
324325
cuprite (~> 0.15)
325326
debug
326-
erb_lint (~> 0.6)
327-
erblint-github (~> 1.0)
327+
erb_lint (~> 0.9)
328+
erblint-github (~> 1.0.1)
328329
kramdown (~> 2.5)
329330
listen (~> 3.9)
330331
lookbook (~> 2.3.9)
@@ -336,7 +337,7 @@ DEPENDENCIES
336337
railties (= 8.0.2)
337338
rake (~> 13.3)
338339
rubocop
339-
rubocop-github (~> 0.20.0)
340+
rubocop-github (~> 0.25.0)
340341
rubocop-performance
341342
rubocop-rails
342343
rubocop-rails-accessibility (~> 1.0)

lib/primer/accessibility.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ module Accessibility
3737
}
3838
}.freeze
3939

40+
# rubocop:disable Style/ClassMethodsDefinitions
4041
class << self
42+
# rubocop:enable Style/ClassMethodsDefinitions
43+
4144
def ignore_preview?(preview_class)
4245
IGNORED_PREVIEWS.include?(preview_class)
4346
end

lib/primer/view_components/linters/migrations/iconbutton_component.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module RuboCop
44
module Cop
55
module Migrations
66
# Lint and autocorrect deprecated IconButton
7-
class IconButtonComponent < RuboCop::Cop::Cop
7+
class IconButtonComponent < RuboCop::Cop::Base
8+
extend AutoCorrector
89
INVALID_MESSAGE = <<~STR
910
`Primer::IconButton` is deprecated. Please use `Primer::Beta::IconButton` instead.
1011
STR
@@ -20,13 +21,9 @@ class IconButtonComponent < RuboCop::Cop::Cop
2021
def on_send(node)
2122
return unless icon_button(node)
2223

23-
add_offense(node, message: INVALID_MESSAGE)
24-
end
25-
26-
def autocorrect(node)
27-
return if hash_with_box_value?(node.arguments.first)
24+
add_offense(node, message: INVALID_MESSAGE) do |corrector|
25+
next if hash_with_box_value?(node.arguments.first)
2826

29-
lambda do |corrector|
3027
corrector.replace(icon_button(node), "Primer::Beta::IconButton")
3128
end
3229
end

lib/primer/view_components/linters/migrations/truncate_component.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module RuboCop
44
module Cop
55
module Migrations
66
# Lint & autocorrect Truncate components
7-
class TruncateComponent < RuboCop::Cop::Cop
7+
class TruncateComponent < RuboCop::Cop::Base
8+
extend AutoCorrector
89
INVALID_MESSAGE = <<~STR
910
`Primer::Truncate` is deprecated. Please use `Primer::Beta::Truncate` instead!
1011
STR
@@ -24,13 +25,9 @@ class TruncateComponent < RuboCop::Cop::Cop
2425
def on_send(node)
2526
return unless truncate_component(node)
2627

27-
add_offense(node, message: INVALID_MESSAGE)
28-
end
29-
30-
def autocorrect(node)
31-
return if hash_with_inline_value?(node.arguments.first)
28+
add_offense(node, message: INVALID_MESSAGE) do |corrector|
29+
next if hash_with_inline_value?(node.arguments.first)
3230

33-
lambda do |corrector|
3431
if node.arguments.first.nil? == false
3532
corrector.replace(node.children.first, "Primer::Beta::Truncate")
3633
corrector.insert_after(node.arguments.first, ", tag: :div") unless truncate_with_tag?(node.arguments.first)

lib/rubocop/cop/primer/base_cop.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module RuboCop
88
module Cop
99
module Primer
1010
# :nodoc:
11-
class BaseCop < RuboCop::Cop::Cop
11+
class BaseCop < RuboCop::Cop::Base
1212
# We only verify SystemArguments if it's a `.new` call on a component or
1313
# a ViewHeleper call.
1414
def valid_node?(node)

lib/rubocop/cop/primer/component_name_migration.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ module Primer
1515
# good
1616
# Primer::Beta::ComponentName.new()
1717
class ComponentNameMigration < BaseCop
18+
extend AutoCorrector
19+
1820
def on_send(node)
1921
return unless node.method_name == :new && !node.receiver.nil? && ::Primer::Deprecations.deprecated?(node.receiver.const_name)
2022

2123
message = ::Primer::Deprecations.deprecation_message(node.receiver.const_name)
22-
add_offense(node.receiver, message: message)
23-
end
24-
25-
def autocorrect(node)
26-
lambda do |corrector|
27-
component_name = node.const_name
28-
return unless ::Primer::Deprecations.correctable?(component_name)
24+
25+
add_offense(node.receiver, message: message) do |corrector|
26+
component_name = node.receiver.const_name
27+
next unless ::Primer::Deprecations.correctable?(component_name)
2928

3029
replacement = ::Primer::Deprecations.replacement(component_name)
31-
corrector.replace(node, replacement) if replacement.present?
30+
corrector.replace(node.receiver, replacement) if replacement.present?
3231
end
3332
end
3433
end

lib/rubocop/cop/primer/deprecated_arguments.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Primer
1414
# good
1515
# Component.new(foo: :bar)
1616
class DeprecatedArguments < BaseCop
17+
extend AutoCorrector
1718
INVALID_MESSAGE = <<~STR
1819
Avoid using deprecated arguments: https://primer.style/view-components/deprecated.
1920
STR
@@ -283,17 +284,15 @@ def on_send(node)
283284
key, value = extract_kv_from(pair)
284285
next unless DEPRECATED.key?(key) && DEPRECATED[key].key?(value)
285286

286-
add_offense(pair, message: INVALID_MESSAGE)
287+
add_offense(pair, message: INVALID_MESSAGE) do |corrector|
288+
key, value = extract_kv_from(pair)
289+
replacement = DEPRECATED[key][value]
290+
corrector.replace(pair, replacement) if replacement.present?
291+
end
287292
end
288293
end
289294

290-
def autocorrect(node)
291-
lambda do |corrector|
292-
key, value = extract_kv_from(node)
293-
replacement = DEPRECATED[key][value]
294-
corrector.replace(node, replacement) if replacement.present?
295-
end
296-
end
295+
297296

298297
def extract_kv_from(pair)
299298
key = pair.key.value

lib/rubocop/cop/primer/deprecated_button_arguments.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Primer
1414
# good
1515
# ButtonComponent.new(size: :small)
1616
class DeprecatedButtonArguments < BaseCop
17+
extend AutoCorrector
1718
INVALID_MESSAGE = <<~STR
1819
`variant` is deprecated. Use `size` instead.
1920
STR
@@ -37,14 +38,12 @@ def on_send(node)
3738

3839
return if pair.nil?
3940

40-
add_offense(pair.key, message: INVALID_MESSAGE)
41-
end
42-
43-
def autocorrect(node)
44-
lambda do |corrector|
45-
corrector.replace(node, DEPRECATIONS[node.value])
41+
add_offense(pair.key, message: INVALID_MESSAGE) do |corrector|
42+
corrector.replace(pair.key, DEPRECATIONS[pair.key.value])
4643
end
4744
end
45+
46+
4847
end
4948
end
5049
end

lib/rubocop/cop/primer/deprecated_label_schemes.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Primer
1414
# good
1515
# Primer::Beta::Label.new(scheme: :accent)
1616
class DeprecatedLabelSchemes < BaseCop
17+
extend AutoCorrector
1718
INVALID_MESSAGE = <<~STR
1819
Avoid using deprecated schemes: https://primer.style/view-components/deprecated#labelcomponent.
1920
STR
@@ -44,16 +45,14 @@ def on_send(node)
4445

4546
next unless DEPRECATIONS.key?(value)
4647

47-
add_offense(pair.value, message: INVALID_MESSAGE)
48+
add_offense(pair.value, message: INVALID_MESSAGE) do |corrector|
49+
replacement = DEPRECATIONS[pair.value.value.to_sym]
50+
corrector.replace(pair.value, replacement)
51+
end
4852
end
4953
end
5054

51-
def autocorrect(node)
52-
lambda do |corrector|
53-
replacement = DEPRECATIONS[node.value.to_sym]
54-
corrector.replace(node, replacement)
55-
end
56-
end
55+
5756

5857
private
5958

lib/rubocop/cop/primer/deprecated_label_variants.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Primer
2020
# good
2121
# Primer::Beta::Label.new(inline: true)
2222
class DeprecatedLabelVariants < BaseCop
23+
extend AutoCorrector
2324
def on_send(node)
2425
return unless label_node?(node)
2526
return unless node.arguments?
@@ -37,26 +38,18 @@ def on_send(node)
3738

3839
case pair.value.value
3940
when :large, "large"
40-
add_offense(pair, message: "Avoid using `variant: :large` with `LabelComponent`. Use `size: :large` instead.")
41+
add_offense(pair, message: "Avoid using `variant: :large` with `LabelComponent`. Use `size: :large` instead.") do |corrector|
42+
corrector.replace(pair, "size: :large")
43+
end
4144
when :inline, "inline"
42-
add_offense(pair, message: "Avoid using `variant: :inline` with `LabelComponent`. Use `inline: true` instead.")
45+
add_offense(pair, message: "Avoid using `variant: :inline` with `LabelComponent`. Use `inline: true` instead.") do |corrector|
46+
corrector.replace(pair, "inline: true")
47+
end
4348
end
4449
end
4550
end
4651

47-
def autocorrect(node)
48-
lambda do |corrector|
49-
replacement =
50-
case node.value.value
51-
when :large, "large"
52-
"size: :large"
53-
when :inline, "inline"
54-
"inline: true"
55-
end
5652

57-
corrector.replace(node, replacement)
58-
end
59-
end
6053

6154
private
6255

lib/rubocop/cop/primer/no_tag_memoize.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Primer
1616
#
1717
# good
1818
# @system_arguments[:tag] = :h2
19-
class NoTagMemoize < RuboCop::Cop::Cop
19+
class NoTagMemoize < RuboCop::Cop::Base
2020
INVALID_MESSAGE = <<~STR
2121
Avoid `[:tag] ||=`. Instead, try one of the following:
2222
- Don't allow consumers to update the tag by having a fixed tag (e.g. `system_arguments[:tag] = :div`)

lib/rubocop/cop/primer/primer_octicon.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module Primer
2222
# primer_octicon(:"icon-with-daashes")
2323
# primer_octicon(@ivar)
2424
# primer_octicon(condition > "icon" : "other-icon")
25-
class PrimerOcticon < RuboCop::Cop::Cop
25+
class PrimerOcticon < RuboCop::Cop::Base
26+
extend AutoCorrector
2627
INVALID_MESSAGE = <<~STR
2728
Replace the octicon helper with primer_octicon. See https://primer.style/view-components/components/octicon for details.
2829
STR
@@ -65,11 +66,7 @@ def on_send(node)
6566
return if invalid_classes.present?
6667
end
6768

68-
add_offense(node, message: INVALID_MESSAGE)
69-
end
70-
71-
def autocorrect(node)
72-
lambda do |corrector|
69+
add_offense(node, message: INVALID_MESSAGE) do |corrector|
7370
kwargs = kwargs(node)
7471

7572
# Converting arguments for the component
@@ -86,6 +83,8 @@ def autocorrect(node)
8683
end
8784
end
8885

86+
87+
8988
private
9089

9190
def transform_sizes(kwargs)

0 commit comments

Comments
 (0)