Skip to content

Commit

Permalink
Use Cop::Base API
Browse files Browse the repository at this point in the history
Follow rubocop/rubocop#7868.

The legacy `Cop::Cop` API is soft deprecated and this PR use
new `Cop::Base` API instead.

> maintain any RuboCop extensions, as the legacy API will be removed in
> RuboCop 2.0.

https://metaredux.com/posts/2020/10/21/rubocop-1-0.html

This new API requires RuboCop 0.87 or higher.
https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md#0870-2020-07-06

Denpendency version of `s.add_dependency "rubocop"` does not need to be
updated in the gemspec if github#86 has been merged first.
  • Loading branch information
koic committed Apr 29, 2021
1 parent 527a7e0 commit 54d65ef
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 318 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/github/insecure_hash_algorithm.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class InsecureHashAlgorithm < Cop
class InsecureHashAlgorithm < Base
MSG = "This hash function is not allowed"
UUID_V3_MSG = "uuid_v3 uses MD5, which is not allowed"
UUID_V5_MSG = "uuid_v5 uses SHA1, which is not allowed"
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/github/rails_application_record.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsApplicationRecord < Cop
class RailsApplicationRecord < Base
MSG = "Models should subclass from ApplicationRecord"

def_node_matcher :active_record_base_const?, <<-PATTERN
Expand All @@ -20,7 +20,7 @@ def on_class(node)
klass, superclass, _ = *node

if active_record_base_const?(superclass) && !(application_record_const?(klass))
add_offense(superclass, location: :expression)
add_offense(superclass)
end
end
end
Expand Down
16 changes: 11 additions & 5 deletions lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
Expand Up @@ -5,7 +5,9 @@
module RuboCop
module Cop
module GitHub
class RailsControllerRenderActionSymbol < Cop
class RailsControllerRenderActionSymbol < Base
extend AutoCorrector

MSG = "Prefer `render` with string instead of symbol"

def_node_matcher :render_sym?, <<-PATTERN
Expand All @@ -22,18 +24,22 @@ class RailsControllerRenderActionSymbol < Cop

def on_send(node)
if sym_node = render_sym?(node)
add_offense(sym_node, location: :expression)
add_offense(sym_node) do |corrector|
register_offense(sym_node, node)
end
elsif option_pairs = render_with_options?(node)
option_pairs.each do |pair|
if sym_node = action_key?(pair)
add_offense(sym_node, location: :expression)
register_offense(sym_node, node)
end
end
end
end

def autocorrect(node)
lambda do |corrector|
private

def register_offense(sym_node, node)
add_offense(sym_node) do |corrector|
corrector.replace(node.source_range, "\"#{node.children[0]}\"")
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/github/rails_controller_render_literal.rb
Expand Up @@ -6,7 +6,7 @@
module RuboCop
module Cop
module GitHub
class RailsControllerRenderLiteral < Cop
class RailsControllerRenderLiteral < Base
include RenderLiteralHelpers

MSG = "render must be used with a string literal or an instance of a Class"
Expand Down Expand Up @@ -66,29 +66,29 @@ def on_send(node)

if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
if !literal?(template_node)
add_offense(node, location: :expression)
add_offense(node)
return
end
else
add_offense(node, location: :expression)
add_offense(node)
return
end

if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
if !literal?(layout_node)
add_offense(node, location: :expression)
add_offense(node)
return
end
end
else
add_offense(node, location: :expression)
add_offense(node)
return
end

if render_literal?(node)
option_hash = node.arguments[1]
if option_hash && !option_hash.hash_type?
add_offense(node, location: :expression)
add_offense(node)
return
end
option_pairs = option_hash && option_hash.pairs
Expand All @@ -99,7 +99,7 @@ def on_send(node)
if option_pairs
locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first
if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
add_offense(node, location: :expression)
add_offense(node)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsControllerRenderPathsExist < Cop
class RailsControllerRenderPathsExist < Base
def_node_matcher :render?, <<-PATTERN
(send nil? {:render :render_to_string} $...)
PATTERN
Expand All @@ -28,7 +28,7 @@ def on_send(node)
if args = render_str?(node)
node, path = args
unless resolve_template(path.to_s)
add_offense(node, location: :expression, message: "Template could not be found")
add_offense(node, message: "Template could not be found")
end
elsif pairs = render_options?(node)
if pair = pairs.detect { |p| render_key?(p) }
Expand All @@ -37,11 +37,11 @@ def on_send(node)
case key
when :action, :template
unless resolve_template(path.to_s)
add_offense(node, location: :expression, message: "Template could not be found")
add_offense(node, message: "Template could not be found")
end
when :partial
unless resolve_partial(path.to_s)
add_offense(node, location: :expression, message: "Partial template could not be found")
add_offense(node, message: "Partial template could not be found")
end
end
end
Expand Down
15 changes: 4 additions & 11 deletions lib/rubocop/cop/github/rails_controller_render_shorthand.rb
Expand Up @@ -5,7 +5,9 @@
module RuboCop
module Cop
module GitHub
class RailsControllerRenderShorthand < Cop
class RailsControllerRenderShorthand < Base
extend AutoCorrector

MSG = "Prefer `render` template shorthand"

def_node_matcher :render_with_options?, <<-PATTERN
Expand All @@ -20,14 +22,6 @@ class RailsControllerRenderShorthand < Cop
({str sym} $_)
PATTERN

def investigate(*)
@autocorrect = {}
end

def autocorrect(node)
@autocorrect[node]
end

def on_send(node)
if option_pairs = render_with_options?(node)
option_pairs.each do |pair|
Expand All @@ -37,10 +31,9 @@ def on_send(node)
.sub(/#{pair.source}(,\s*)?/, "")
.sub("render ", "render \"#{str(value_node)}\"#{comma}")

@autocorrect[node] = lambda do |corrector|
add_offense(node, message: "Use `#{corrected_source}` instead") do |corrector|
corrector.replace(node.source_range, corrected_source)
end
add_offense(node, location: :expression, message: "Use `#{corrected_source}` instead")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/github/rails_render_inline.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsRenderInline < Cop
class RailsRenderInline < Base
MSG = "Avoid `render inline:`"

def_node_matcher :render_with_options?, <<-PATTERN
Expand All @@ -19,7 +19,7 @@ class RailsRenderInline < Cop
def on_send(node)
if option_pairs = render_with_options?(node)
if option_pairs.detect { |pair| inline_key?(pair) }
add_offense(node, location: :expression)
add_offense(node)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/github/rails_render_object_collection.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsRenderObjectCollection < Cop
class RailsRenderObjectCollection < Base
MSG = "Avoid `render object:`"

def_node_matcher :render_with_options?, <<-PATTERN
Expand Down Expand Up @@ -34,9 +34,9 @@ def on_send(node)
if partial_name.children[0].is_a?(String)
suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`"
end
add_offense(node, location: :expression, message: "Avoid `render object:`#{suggestion}")
add_offense(node, message: "Avoid `render object:`#{suggestion}")
when :collection, :spacer_template
add_offense(node, location: :expression, message: "Avoid `render collection:`")
add_offense(node, message: "Avoid `render collection:`")
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/github/rails_view_render_literal.rb
Expand Up @@ -6,7 +6,7 @@
module RuboCop
module Cop
module GitHub
class RailsViewRenderLiteral < Cop
class RailsViewRenderLiteral < Base
include RenderLiteralHelpers

MSG = "render must be used with a literal template and use literals for locals keys"
Expand Down Expand Up @@ -40,15 +40,15 @@ def on_send(node)

if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first
if !literal?(partial_node)
add_offense(node, location: :expression)
add_offense(node)
return
end
else
add_offense(node, location: :expression)
add_offense(node)
return
end
else
add_offense(node, location: :expression)
add_offense(node)
return
end

Expand All @@ -61,10 +61,10 @@ def on_send(node)
if locals
if locals.hash_type?
if !hash_with_literal_keys?(locals)
add_offense(node, location: :expression)
add_offense(node)
end
else
add_offense(node, location: :expression)
add_offense(node)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/github/rails_view_render_paths_exist.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsViewRenderPathsExist < Cop
class RailsViewRenderPathsExist < Base
def_node_matcher :render?, <<-PATTERN
(send nil? {:render :render_to_string} $...)
PATTERN
Expand All @@ -28,14 +28,14 @@ def on_send(node)
if args = render_str?(node)
node, path = args
unless resolve_partial(path.to_s)
add_offense(node, location: :expression, message: "Partial template could not be found")
add_offense(node, message: "Partial template could not be found")
end
elsif pairs = render_options?(node)
if pair = pairs.detect { |p| partial_key?(p) }
node, path = partial_key?(pair)

unless resolve_partial(path.to_s)
add_offense(node, location: :expression, message: "Partial template could not be found")
add_offense(node, message: "Partial template could not be found")
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/github/rails_view_render_shorthand.rb
Expand Up @@ -5,7 +5,7 @@
module RuboCop
module Cop
module GitHub
class RailsViewRenderShorthand < Cop
class RailsViewRenderShorthand < Base
MSG = "Prefer `render` partial shorthand"

def_node_matcher :render_with_options?, <<-PATTERN
Expand All @@ -26,9 +26,9 @@ def on_send(node)
locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first

if option_pairs.length == 1 && partial_key
add_offense(node, location: :expression, message: "Use `render #{partial_key.source}` instead")
add_offense(node, message: "Use `render #{partial_key.source}` instead")
elsif option_pairs.length == 2 && partial_key && locals_key
add_offense(node, location: :expression, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/cop_test.rb
Expand Up @@ -17,9 +17,9 @@ def setup

def investigate(cop, src, filename = nil)
processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f, filename)
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
commissioner.investigate(processed_source)
commissioner
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
report = team.investigate(processed_source)
report.offenses
end

def erb_investigate(cop, src, filename = nil)
Expand Down

0 comments on commit 54d65ef

Please sign in to comment.