Skip to content

Commit

Permalink
Run bundle exec rubocop -a (or -A) on all the code in this repo
Browse files Browse the repository at this point in the history
- These rules seemed to make sense and were all autocorrectable:
  - Style/SymbolArray
  - Layout/EmptyLineAfterGuardClause
  - Style/MultipleComparison
  - Style/IfUnlessModifier (since we don't have line length limitations)
  - Style/EmptyCaseCondition
  - Style/TrailingUnderscoreVariable
  - Style/RedundantParentheses
  - Lint/UnusedBlockArgument
  - Style/NegatedIf
  - Style/StringConcatenation
  - Style/SafeNavigation
  - Layout/MultilineMethodCallIndentation
  - Layout/EmptyLineAfterMagicComment
  - Layout/SpaceInsideHashLiteralBraces
  - Layout/EmptyLines
  - Layout/EmptyLineBetweenDefs
  • Loading branch information
issyl0 authored Oct 10, 2022
1 parent 7df97a7 commit 53a0a37
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "bundler/gem_tasks"
require "rake/testtask"
require "rubocop/rake_task"

task default: [:test, :rubocop]
task default: %i[test rubocop]

Rake::TestTask.new
RuboCop::RakeTask.new
31 changes: 12 additions & 19 deletions lib/rubocop/cop/github/insecure_hash_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class InsecureHashAlgorithm < Base

def insecure_algorithm?(val)
return false if val == :Digest # Don't match "Digest::Digest".

case alg_name(val)
when *allowed_hash_functions
false
Expand All @@ -80,7 +81,7 @@ def not_just_encoding?(val)
end

def just_encoding?(val)
val == :hexencode || val == :bubblebabble
%i[hexencode bubblebabble].include?(val)
end

# Built-in hash functions are listed in these docs:
Expand All @@ -99,6 +100,7 @@ def allowed_hash_functions
def alg_name(val)
return :nil if val.nil?
return val.to_s.downcase unless val.is_a?(RuboCop::AST::Node)

case val.type
when :sym, :str
val.children.first.to_s.downcase
Expand All @@ -108,28 +110,19 @@ def alg_name(val)
end

def on_const(const_node)
if insecure_const?(const_node) && !digest_uuid?(const_node)
add_offense(const_node, message: MSG)
end
add_offense(const_node, message: MSG) if insecure_const?(const_node) && !digest_uuid?(const_node)
end

def on_send(send_node)
case
when uuid_v3?(send_node)
unless allowed_hash_functions.include?("md5")
add_offense(send_node, message: UUID_V3_MSG)
end
when uuid_v5?(send_node)
unless allowed_hash_functions.include?("sha1")
add_offense(send_node, message: UUID_V5_MSG)
end
when openssl_hmac_new?(send_node)
if openssl_hmac_new_insecure?(send_node)
add_offense(send_node, message: MSG)
end
when insecure_digest?(send_node)
if uuid_v3?(send_node)
add_offense(send_node, message: UUID_V3_MSG) unless allowed_hash_functions.include?("md5")
elsif uuid_v5?(send_node)
add_offense(send_node, message: UUID_V5_MSG) unless allowed_hash_functions.include?("sha1")
elsif openssl_hmac_new?(send_node)
add_offense(send_node, message: MSG) if openssl_hmac_new_insecure?(send_node)
elsif insecure_digest?(send_node)
add_offense(send_node, message: MSG)
when insecure_hash_lookup?(send_node)
elsif insecure_hash_lookup?(send_node)
add_offense(send_node, message: MSG)
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/github/rails_application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ class RailsApplicationRecord < Base
PATTERN

def on_class(node)
klass, superclass, _ = *node
klass, superclass, = *node

if active_record_base_const?(superclass) && !(application_record_const?(klass))
add_offense(superclass)
end
add_offense(superclass) if active_record_base_const?(superclass) && !application_record_const?(klass)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RailsControllerRenderActionSymbol < Base

def on_send(node)
if sym_node = render_sym?(node)
add_offense(sym_node) do |corrector|
add_offense(sym_node) do |_corrector|
register_offense(sym_node, node)
end
elsif option_pairs = render_with_options?(node)
Expand Down
14 changes: 5 additions & 9 deletions lib/rubocop/cop/github/rails_controller_render_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ def on_send(node)
elsif option_pairs = render_with_options?(node)
option_pairs = option_pairs.reject { |pair| options_key?(pair) }

if option_pairs.any? { |pair| ignore_key?(pair) }
return
end
return if option_pairs.any? { |pair| ignore_key?(pair) }

if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
if !literal?(template_node)
unless literal?(template_node)
add_offense(node)
return
end
Expand All @@ -75,7 +73,7 @@ def on_send(node)
end

if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
if !literal?(layout_node)
unless literal?(layout_node)
add_offense(node)
return
end
Expand All @@ -91,16 +89,14 @@ def on_send(node)
add_offense(node)
return
end
option_pairs = option_hash && option_hash.pairs
option_pairs = option_hash&.pairs
else
option_pairs = node.arguments[0].pairs
end

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)
end
add_offense(node) if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
end
end
end
Expand Down
14 changes: 4 additions & 10 deletions lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,24 @@ def on_send(node)

if args = render_str?(node)
node, path = args
unless resolve_template(path.to_s)
add_offense(node, message: "Template could not be found")
end
add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s)
elsif pairs = render_options?(node)
if pair = pairs.detect { |p| render_key?(p) }
key, node, path = render_key?(pair)

case key
when :action, :template
unless resolve_template(path.to_s)
add_offense(node, message: "Template could not be found")
end
add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s)
when :partial
unless resolve_partial(path.to_s)
add_offense(node, message: "Partial template could not be found")
end
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
end
end
end
end

def resolve_template(path)
cop_config["ViewPath"].each do |view_path|
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first
return m
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/github/rails_controller_render_shorthand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def on_send(node)
if value_node = action_key?(pair)
comma = option_pairs.length > 1 ? ", " : ""
corrected_source = node.source
.sub(/#{pair.source}(,\s*)?/, "")
.sub("render ", "render \"#{str(value_node)}\"#{comma}")
.sub(/#{pair.source}(,\s*)?/, "")
.sub("render ", "render \"#{str(value_node)}\"#{comma}")

add_offense(node, message: "Use `#{corrected_source}` instead") do |corrector|
corrector.replace(node.source_range, corrected_source)
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/github/rails_render_inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class RailsRenderInline < Base

def on_send(node)
if option_pairs = render_with_options?(node)
if option_pairs.detect { |pair| inline_key?(pair) }
add_offense(node)
end
add_offense(node) if option_pairs.detect { |pair| inline_key?(pair) }
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/github/rails_render_object_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def on_send(node)

case object_sym
when :object
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
suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`" if partial_name.children[0].is_a?(String)
add_offense(node, message: "Avoid `render object:`#{suggestion}")
when :collection, :spacer_template
add_offense(node, message: "Avoid `render collection:`")
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/github/rails_view_render_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ def on_send(node)

if render_literal?(node)
elsif option_pairs = render_with_options?(node)
if option_pairs.any? { |pair| ignore_key?(pair) }
return
end
return if option_pairs.any? { |pair| ignore_key?(pair) }

if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first
if !literal?(partial_node)
unless literal?(partial_node)
add_offense(node)
return
end
Expand All @@ -60,9 +58,7 @@ def on_send(node)

if locals
if locals.hash_type?
if !hash_with_literal_keys?(locals)
add_offense(node)
end
add_offense(node) unless hash_with_literal_keys?(locals)
else
add_offense(node)
end
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/github/rails_view_render_paths_exist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ def on_send(node)

if args = render_str?(node)
node, path = args
unless resolve_partial(path.to_s)
add_offense(node, message: "Partial template could not be found")
end
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
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, message: "Partial template could not be found")
end
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
end
end
end
Expand All @@ -47,7 +43,7 @@ def resolve_partial(path)
path = parts.join(File::SEPARATOR)

cop_config["ViewPath"].each do |view_path|
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first
return m
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/github/render_literal_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
#

require "rubocop"

module RuboCop
Expand Down
2 changes: 1 addition & 1 deletion test/test_insecure_hash_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def cop_class
end

def make_cop(allowed:)
config = RuboCop::Config.new({"GitHub/InsecureHashAlgorithm" => {"Allowed" => allowed}})
config = RuboCop::Config.new({ "GitHub/InsecureHashAlgorithm" => { "Allowed" => allowed } })
cop_class.new(config)
end

Expand Down
1 change: 0 additions & 1 deletion test/test_rails_controller_render_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ def index
assert_equal 1, offenses.count
end


def test_render_literal_dynamic_local_key_offense
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
class ProductsController < ActionController::Base
Expand Down

0 comments on commit 53a0a37

Please sign in to comment.