Skip to content

Commit 53a0a37

Browse files
authored
Run bundle exec rubocop -a (or -A) on all the code in this repo
- 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
1 parent 7df97a7 commit 53a0a37

14 files changed

+37
-69
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: [:test, :rubocop]
7+
task default: %i[test rubocop]
88

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

lib/rubocop/cop/github/insecure_hash_algorithm.rb

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

6565
def insecure_algorithm?(val)
6666
return false if val == :Digest # Don't match "Digest::Digest".
67+
6768
case alg_name(val)
6869
when *allowed_hash_functions
6970
false
@@ -80,7 +81,7 @@ def not_just_encoding?(val)
8081
end
8182

8283
def just_encoding?(val)
83-
val == :hexencode || val == :bubblebabble
84+
%i[hexencode bubblebabble].include?(val)
8485
end
8586

8687
# Built-in hash functions are listed in these docs:
@@ -99,6 +100,7 @@ def allowed_hash_functions
99100
def alg_name(val)
100101
return :nil if val.nil?
101102
return val.to_s.downcase unless val.is_a?(RuboCop::AST::Node)
103+
102104
case val.type
103105
when :sym, :str
104106
val.children.first.to_s.downcase
@@ -108,28 +110,19 @@ def alg_name(val)
108110
end
109111

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

116116
def on_send(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)
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)
131124
add_offense(send_node, message: MSG)
132-
when insecure_hash_lookup?(send_node)
125+
elsif insecure_hash_lookup?(send_node)
133126
add_offense(send_node, message: MSG)
134127
end
135128
end

lib/rubocop/cop/github/rails_application_record.rb

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

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

22-
if active_record_base_const?(superclass) && !(application_record_const?(klass))
23-
add_offense(superclass)
24-
end
22+
add_offense(superclass) if active_record_base_const?(superclass) && !application_record_const?(klass)
2523
end
2624
end
2725
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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ def on_send(node)
6060
elsif option_pairs = render_with_options?(node)
6161
option_pairs = option_pairs.reject { |pair| options_key?(pair) }
6262

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

6765
if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
68-
if !literal?(template_node)
66+
unless literal?(template_node)
6967
add_offense(node)
7068
return
7169
end
@@ -75,7 +73,7 @@ def on_send(node)
7573
end
7674

7775
if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
78-
if !literal?(layout_node)
76+
unless literal?(layout_node)
7977
add_offense(node)
8078
return
8179
end
@@ -91,16 +89,14 @@ def on_send(node)
9189
add_offense(node)
9290
return
9391
end
94-
option_pairs = option_hash && option_hash.pairs
92+
option_pairs = option_hash&.pairs
9593
else
9694
option_pairs = node.arguments[0].pairs
9795
end
9896

9997
if option_pairs
10098
locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first
101-
if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
102-
add_offense(node)
103-
end
99+
add_offense(node) if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
104100
end
105101
end
106102
end

lib/rubocop/cop/github/rails_controller_render_paths_exist.rb

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

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

3735
case key
3836
when :action, :template
39-
unless resolve_template(path.to_s)
40-
add_offense(node, message: "Template could not be found")
41-
end
37+
add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s)
4238
when :partial
43-
unless resolve_partial(path.to_s)
44-
add_offense(node, message: "Partial template could not be found")
45-
end
39+
add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s)
4640
end
4741
end
4842
end
4943
end
5044

5145
def resolve_template(path)
5246
cop_config["ViewPath"].each do |view_path|
53-
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
47+
if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first
5448
return m
5549
end
5650
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class RailsRenderInline < Base
1818

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

lib/rubocop/cop/github/rails_render_object_collection.rb

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

3232
case object_sym
3333
when :object
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
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)
3735
add_offense(node, message: "Avoid `render object:`#{suggestion}")
3836
when :collection, :spacer_template
3937
add_offense(node, message: "Avoid `render collection:`")

lib/rubocop/cop/github/rails_view_render_literal.rb

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

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

4139
if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first
42-
if !literal?(partial_node)
40+
unless literal?(partial_node)
4341
add_offense(node)
4442
return
4543
end
@@ -60,9 +58,7 @@ def on_send(node)
6058

6159
if locals
6260
if locals.hash_type?
63-
if !hash_with_literal_keys?(locals)
64-
add_offense(node)
65-
end
61+
add_offense(node) unless hash_with_literal_keys?(locals)
6662
else
6763
add_offense(node)
6864
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy