aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/rubocops
diff options
context:
space:
mode:
authorGautham Goli2017-06-16 19:44:14 +0530
committerGautham Goli2017-06-16 19:45:16 +0530
commit0e1c88e7aefb93a8e2cc927fa9a4e903ac015c57 (patch)
tree0c91ca4b66538b4d179fd7877fc2e2caf5368a48 /Library/Homebrew/rubocops
parent77da75e7d6ecc2ca749eb939d8e24ad9e3dfc5e1 (diff)
downloadbrew-0e1c88e7aefb93a8e2cc927fa9a4e903ac015c57.tar.bz2
Refactor Checksum cop to add autocorrect method
Diffstat (limited to 'Library/Homebrew/rubocops')
-rw-r--r--Library/Homebrew/rubocops/checksum_cop.rb36
-rw-r--r--Library/Homebrew/rubocops/extend/formula_cop.rb11
2 files changed, 34 insertions, 13 deletions
diff --git a/Library/Homebrew/rubocops/checksum_cop.rb b/Library/Homebrew/rubocops/checksum_cop.rb
index d9e81a4ef..dcaf60e7d 100644
--- a/Library/Homebrew/rubocops/checksum_cop.rb
+++ b/Library/Homebrew/rubocops/checksum_cop.rb
@@ -21,15 +21,6 @@ module RuboCop
end
end
- def get_checksum_node(call)
- return if parameters(call).empty? || parameters(call).nil?
- if parameters(call).first.str_type?
- parameters(call).first
- elsif parameters(call).first.hash_type?
- parameters(call).first.keys.first
- end
- end
-
def audit_sha256(checksum)
return if checksum.nil?
if regex_match_group(checksum, /^$/)
@@ -41,12 +32,31 @@ module RuboCop
problem "sha256 should be 64 characters"
end
- if regex_match_group(checksum, /[^a-f0-9]+/i)
- problem "sha256 contains invalid characters"
+ return unless regex_match_group(checksum, /[^a-f0-9]+/i)
+ problem "sha256 contains invalid characters"
+ end
+ end
+
+ class ChecksumCase < FormulaCop
+ def audit_formula(_node, _class_node, _parent_class_node, body_node)
+ return if body_node.nil?
+ sha256_calls = find_every_method_call_by_name(body_node, :sha256)
+ sha256_calls.each do |sha256_call|
+ checksum = get_checksum_node(sha256_call)
+ next if checksum.nil?
+ next unless regex_match_group(checksum, /[A-F]+/)
+ problem "sha256 should be lowercase"
end
+ end
+
+ private
- return unless regex_match_group(checksum, /[A-F]+/)
- problem "sha256 should be lowercase"
+ def autocorrect(node)
+ lambda do |corrector|
+ correction = node.source.downcase
+ corrector.insert_before(node.source_range, correction)
+ corrector.remove(node.source_range)
+ end
end
end
end
diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb
index 98280841c..439fde6a5 100644
--- a/Library/Homebrew/rubocops/extend/formula_cop.rb
+++ b/Library/Homebrew/rubocops/extend/formula_cop.rb
@@ -277,6 +277,17 @@ module RuboCop
end
end
+ # Returns the sha256 str node given a sha256 call node
+ def get_checksum_node(call)
+ return if parameters(call).empty? || parameters(call).nil?
+ if parameters(call).first.str_type?
+ parameters(call).first
+ # sha256 is passed as a key-value pair in bottle blocks
+ elsif parameters(call).first.hash_type?
+ parameters(call).first.keys.first
+ end
+ end
+
# Returns the begin position of the node's line in source code
def line_start_column(node)
node.source_range.source_buffer.line_range(node.loc.line).begin_pos