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 Jun 27, 2022
1 parent 7c67cb5 commit 0bc4a31
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 342 deletions.
45 changes: 23 additions & 22 deletions Gemfile.lock
Expand Up @@ -9,70 +9,71 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actionview (6.1.4.4)
activesupport (= 6.1.4.4)
actionview (6.1.6)
activesupport (= 6.1.6)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (6.1.4.4)
activesupport (6.1.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.2)
builder (3.2.4)
concurrent-ruby (1.1.9)
concurrent-ruby (1.1.10)
crass (1.0.6)
erubi (1.10.0)
i18n (1.8.11)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
loofah (2.13.0)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
minitest (5.15.0)
nokogiri (1.13.1-x86_64-darwin)
minitest (5.16.1)
nokogiri (1.13.6-x86_64-darwin)
racc (~> 1.4)
parallel (1.21.0)
parser (3.1.0.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
racc (1.6.0)
rack (2.2.3)
rack (2.2.3.1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
rails-html-sanitizer (1.4.3)
loofah (~> 2.3)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.2.0)
regexp_parser (2.5.0)
rexml (3.2.5)
rubocop (1.25.1)
rubocop (1.31.0)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.15.1, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.18.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
rubocop-performance (1.13.2)
rubocop-ast (1.18.0)
parser (>= 3.1.1.0)
rubocop-performance (1.14.2)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.13.2)
rubocop-rails (2.15.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.7.0, < 2.0)
ruby-progressbar (1.11.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.1.0)
zeitwerk (2.5.3)
unicode-display_width (2.2.0)
zeitwerk (2.6.0)

PLATFORMS
x86_64-darwin-19
x86_64-darwin-20

DEPENDENCIES
Expand Down
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

0 comments on commit 0bc4a31

Please sign in to comment.