Skip to content

Commit 55b4ece

Browse files
authored
Revert "Run bundle exec rubocop -a (or -A) on all the code in this repo"
This reverts commit 53a0a37. This is because we've now disabled all the rules that would have been `DisabledByDefault`. This is so that downstream consumers don't get any nasty surprises. As a result, there should be no RuboCop linting violations in the code in this repo, so we can back out the changes to slim the diff: they're now redundant. Of course, it would be a good idea to go and make these changes at a later date, but we can do that when we enable the correct rules having matched each one up with its rationale in our styleguide.
1 parent 60a3a04 commit 55b4ece

14 files changed

+69
-37
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require "bundler/gem_tasks"
44
require "rake/testtask"
55
require "rubocop/rake_task"
66

7-
task default: %i[test rubocop]
7+
task default: [:test, :rubocop]
88

99
Rake::TestTask.new
1010
RuboCop::RakeTask.new

lib/rubocop/cop/github/insecure_hash_algorithm.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class InsecureHashAlgorithm < Base
6464

6565
def insecure_algorithm?(val)
6666
return false if val == :Digest # Don't match "Digest::Digest".
67-
6867
case alg_name(val)
6968
when *allowed_hash_functions
7069
false
@@ -81,7 +80,7 @@ def not_just_encoding?(val)
8180
end
8281

8382
def just_encoding?(val)
84-
%i[hexencode bubblebabble].include?(val)
83+
val == :hexencode || val == :bubblebabble
8584
end
8685

8786
# Built-in hash functions are listed in these docs:
@@ -100,7 +99,6 @@ def allowed_hash_functions
10099
def alg_name(val)
101100
return :nil if val.nil?
102101
return val.to_s.downcase unless val.is_a?(RuboCop::AST::Node)
103-
104102
case val.type
105103
when :sym, :str
106104
val.children.first.to_s.downcase
@@ -110,19 +108,28 @@ def alg_name(val)
110108
end
111109

112110
def on_const(const_node)
113-
add_offense(const_node, message: MSG) if insecure_const?(const_node) && !digest_uuid?(const_node)
111+
if insecure_const?(const_node) && !digest_uuid?(const_node)
112+
add_offense(const_node, message: MSG)
113+
end
114114
end
115115

116116
def on_send(send_node)
117-
if uuid_v3?(send_node)
118-
add_offense(send_node, message: UUID_V3_MSG) unless allowed_hash_functions.include?("md5")
119-
elsif uuid_v5?(send_node)
120-
add_offense(send_node, message: UUID_V5_MSG) unless allowed_hash_functions.include?("sha1")
121-
elsif openssl_hmac_new?(send_node)
122-
add_offense(send_node, message: MSG) if openssl_hmac_new_insecure?(send_node)
123-
elsif insecure_digest?(send_node)
117+
case
118+
when uuid_v3?(send_node)
119+
unless allowed_hash_functions.include?("md5")
120+
add_offense(send_node, message: UUID_V3_MSG)
121+
end
122+
when uuid_v5?(send_node)
123+
unless allowed_hash_functions.include?("sha1")
124+
add_offense(send_node, message: UUID_V5_MSG)
125+
end
126+
when openssl_hmac_new?(send_node)
127+
if openssl_hmac_new_insecure?(send_node)
128+
add_offense(send_node, message: MSG)
129+
end
130+
when insecure_digest?(send_node)
124131
add_offense(send_node, message: MSG)
125-
elsif insecure_hash_lookup?(send_node)
132+
when insecure_hash_lookup?(send_node)
126133
add_offense(send_node, message: MSG)
127134
end
128135
end

lib/rubocop/cop/github/rails_application_record.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class RailsApplicationRecord < Base
1717
PATTERN
1818

1919
def on_class(node)
20-
klass, superclass, = *node
20+
klass, superclass, _ = *node
2121

22-
add_offense(superclass) if active_record_base_const?(superclass) && !application_record_const?(klass)
22+
if active_record_base_const?(superclass) && !(application_record_const?(klass))
23+
add_offense(superclass)
24+
end
2325
end
2426
end
2527
end

lib/rubocop/cop/github/rails_controller_render_action_symbol.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RailsControllerRenderActionSymbol < Base
2424

2525
def on_send(node)
2626
if sym_node = render_sym?(node)
27-
add_offense(sym_node) do |_corrector|
27+
add_offense(sym_node) do |corrector|
2828
register_offense(sym_node, node)
2929
end
3030
elsif option_pairs = render_with_options?(node)

lib/rubocop/cop/github/rails_controller_render_literal.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def on_send(node)
6060
elsif option_pairs = render_with_options?(node)
6161
option_pairs = option_pairs.reject { |pair| options_key?(pair) }
6262

63-
return if option_pairs.any? { |pair| ignore_key?(pair) }
63+
if option_pairs.any? { |pair| ignore_key?(pair) }
64+
return
65+
end
6466

6567
if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
66-
unless literal?(template_node)
68+
if !literal?(template_node)
6769
add_offense(node)
6870
return
6971
end
@@ -73,7 +75,7 @@ def on_send(node)
7375
end
7476

7577
if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
76-
unless literal?(layout_node)
78+
if !literal?(layout_node)
7779
add_offense(node)
7880
return
7981
end
@@ -89,14 +91,16 @@ def on_send(node)
8991
add_offense(node)
9092
return
9193
end
92-
option_pairs = option_hash&.pairs
94+
option_pairs = option_hash && option_hash.pairs
9395
else
9496
option_pairs = node.arguments[0].pairs
9597
end
9698

9799
if option_pairs
98100
locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first
99-
add_offense(node) if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
101+
if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
102+
add_offense(node)
103+
end
100104
end
101105
end
102106
end

lib/rubocop/cop/github/rails_controller_render_paths_exist.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,30 @@ def on_send(node)
2727

2828
if args = render_str?(node)
2929
node, path = args
30-
add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s)
30+
unless resolve_template(path.to_s)
31+
add_offense(node, message: "Template could not be found")
32+
end
3133
elsif pairs = render_options?(node)
3234
if pair = pairs.detect { |p| render_key?(p) }
3335
key, node, path = render_key?(pair)
3436

3537
case key
3638
when :action, :template
37-
add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s)
39+
unless resolve_template(path.to_s)
40+
add_offense(node, message: "Template could not be found")
41+
end
3842
when :partial
39-
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
43+
unless resolve_partial(path.to_s)
44+
add_offense(node, message: "Partial template could not be found")
45+
end
4046
end
4147
end
4248
end
4349
end
4450

4551
def resolve_template(path)
4652
cop_config["ViewPath"].each do |view_path|
47-
if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first
53+
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
4854
return m
4955
end
5056
end

lib/rubocop/cop/github/rails_controller_render_shorthand.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def on_send(node)
2828
if value_node = action_key?(pair)
2929
comma = option_pairs.length > 1 ? ", " : ""
3030
corrected_source = node.source
31-
.sub(/#{pair.source}(,\s*)?/, "")
32-
.sub("render ", "render \"#{str(value_node)}\"#{comma}")
31+
.sub(/#{pair.source}(,\s*)?/, "")
32+
.sub("render ", "render \"#{str(value_node)}\"#{comma}")
3333

3434
add_offense(node, message: "Use `#{corrected_source}` instead") do |corrector|
3535
corrector.replace(node.source_range, corrected_source)

lib/rubocop/cop/github/rails_render_inline.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class RailsRenderInline < Base
1818

1919
def on_send(node)
2020
if option_pairs = render_with_options?(node)
21-
add_offense(node) if option_pairs.detect { |pair| inline_key?(pair) }
21+
if option_pairs.detect { |pair| inline_key?(pair) }
22+
add_offense(node)
23+
end
2224
end
2325
end
2426
end

lib/rubocop/cop/github/rails_render_object_collection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def on_send(node)
3131

3232
case object_sym
3333
when :object
34-
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)
34+
if partial_name.children[0].is_a?(String)
35+
suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`"
36+
end
3537
add_offense(node, message: "Avoid `render object:`#{suggestion}")
3638
when :collection, :spacer_template
3739
add_offense(node, message: "Avoid `render collection:`")

lib/rubocop/cop/github/rails_view_render_literal.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ def on_send(node)
3434

3535
if render_literal?(node)
3636
elsif option_pairs = render_with_options?(node)
37-
return if option_pairs.any? { |pair| ignore_key?(pair) }
37+
if option_pairs.any? { |pair| ignore_key?(pair) }
38+
return
39+
end
3840

3941
if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first
40-
unless literal?(partial_node)
42+
if !literal?(partial_node)
4143
add_offense(node)
4244
return
4345
end
@@ -58,7 +60,9 @@ def on_send(node)
5860

5961
if locals
6062
if locals.hash_type?
61-
add_offense(node) unless hash_with_literal_keys?(locals)
63+
if !hash_with_literal_keys?(locals)
64+
add_offense(node)
65+
end
6266
else
6367
add_offense(node)
6468
end

lib/rubocop/cop/github/rails_view_render_paths_exist.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ def on_send(node)
2727

2828
if args = render_str?(node)
2929
node, path = args
30-
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
30+
unless resolve_partial(path.to_s)
31+
add_offense(node, message: "Partial template could not be found")
32+
end
3133
elsif pairs = render_options?(node)
3234
if pair = pairs.detect { |p| partial_key?(p) }
3335
node, path = partial_key?(pair)
3436

35-
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
37+
unless resolve_partial(path.to_s)
38+
add_offense(node, message: "Partial template could not be found")
39+
end
3640
end
3741
end
3842
end
@@ -43,7 +47,7 @@ def resolve_partial(path)
4347
path = parts.join(File::SEPARATOR)
4448

4549
cop_config["ViewPath"].each do |view_path|
46-
if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first
50+
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
4751
return m
4852
end
4953
end

lib/rubocop/cop/github/render_literal_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
2-
2+
#
33
require "rubocop"
44

55
module RuboCop

test/test_insecure_hash_algorithm.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def cop_class
1010
end
1111

1212
def make_cop(allowed:)
13-
config = RuboCop::Config.new({ "GitHub/InsecureHashAlgorithm" => { "Allowed" => allowed } })
13+
config = RuboCop::Config.new({"GitHub/InsecureHashAlgorithm" => {"Allowed" => allowed}})
1414
cop_class.new(config)
1515
end
1616

test/test_rails_controller_render_literal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def index
442442
assert_equal 1, offenses.count
443443
end
444444

445+
445446
def test_render_literal_dynamic_local_key_offense
446447
offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb"
447448
class ProductsController < ActionController::Base

0 commit comments

Comments
 (0)