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

Use Cop::Base API #87

Merged
merged 2 commits into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -71,6 +71,7 @@ GEM
unicode-display_width (2.2.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
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