aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGautham Goli2017-03-12 02:55:21 +0800
committerGautham Goli2017-03-26 15:26:41 +0530
commitfebc1085984e3d7fa23ee4c18d11e087afe20cbe (patch)
tree1861da8466ae91dc03a07be6533f8bac4bc63b3a
parent4e57f4279aaf32855c6cc0ebc96bf9352feb1e17 (diff)
downloadbrew-febc1085984e3d7fa23ee4c18d11e087afe20cbe.tar.bz2
Encapsulate formula desc offense checking logic into methods
-rw-r--r--Library/Homebrew/rubocops/formula_desc_cop.rb88
1 files changed, 47 insertions, 41 deletions
diff --git a/Library/Homebrew/rubocops/formula_desc_cop.rb b/Library/Homebrew/rubocops/formula_desc_cop.rb
index dcd928d28..d276be5cc 100644
--- a/Library/Homebrew/rubocops/formula_desc_cop.rb
+++ b/Library/Homebrew/rubocops/formula_desc_cop.rb
@@ -13,6 +13,9 @@ module RuboCop
# - Checks if `desc` contains the formula name
class FormulaDesc < FormulaCop
+ attr_accessor :formula_name, :description, :source_buffer, :line_number, :line_begin_pos,
+ :desc_begin_pos, :call_node
+
def audit_formula(node, class_node, _parent_class_node, body)
check(node, body, class_node.const_name)
end
@@ -23,55 +26,58 @@ module RuboCop
body.each_child_node(:send) do |call_node|
_receiver, call_name, args = *call_node
next if call_name != :desc || args.children[0].empty?
- description = args.children[0]
-
- source_buffer = call_node.source_range.source_buffer
- line_number = call_node.loc.line
- line_begin_pos = call_node.source_range.source_buffer.line_range(call_node.loc.line).begin_pos
- desc_begin_pos = call_node.children[2].source_range.begin_pos
+ @formula_name = formula_name
+ @description = args.children[0]
+ @source_buffer = call_node.source_range.source_buffer
+ @line_number = call_node.loc.line
+ @line_begin_pos = call_node.source_range.source_buffer.line_range(call_node.loc.line).begin_pos
+ @desc_begin_pos = call_node.children[2].source_range.begin_pos
+ @call_node = call_node
- linelength = "#{formula_name}: #{description}".length
- if linelength > 80
- column = desc_begin_pos - line_begin_pos
- length = call_node.children[2].source_range.size
- sourcerange = source_range(source_buffer, line_number, column, length)
- message = <<-EOS.undent
- Description is too long. "name: desc" should be less than 80 characters.
- Length is calculated as #{formula_name} + desc. (currently #{linelength})
- EOS
- add_offense(call_node, sourcerange, message)
- end
+ check_for_desc_length_offense
- match_object = description.match(/(command ?line)/i)
- if match_object
- column = desc_begin_pos+match_object.begin(0)-line_begin_pos+1
- length = match_object.to_s.length
- sourcerange = source_range(source_buffer, line_number, column, length)
- message = "Description should use \"command-line\" instead of \"#{match_object}\""
- add_offense(call_node, sourcerange, message)
- end
+ check_for_offense(/(command ?line)/i,
+ "Description should use \"command-line\" instead of \"%s\"")
- match_object = description.match(/^(an?)\s/i)
- if match_object
- column = desc_begin_pos+match_object.begin(0)-line_begin_pos+1
- length = match_object.to_s.length
- sourcerange = source_range(source_buffer, line_number, column, length)
- message = "Description shouldn't start with an indefinite article (#{match_object})"
- add_offense(call_node, sourcerange, message)
- end
+ check_for_offense(/^(an?)\s/i,
+ "Description shouldn't start with an indefinite article (%s)")
- match_object = description.match(/^#{formula_name}/i)
- if match_object
- column = desc_begin_pos+match_object.begin(0)-line_begin_pos+1
- length = match_object.to_s.length
- sourcerange = source_range(source_buffer, line_number, column, length)
- message = "Description shouldn't include the formula name"
- add_offense(call_node, sourcerange, message)
- end
+ check_for_offense(/^#{formula_name}/i,
+ "Description shouldn't include the formula name")
return nil
end
add_offense(node, node.source_range, "Formula should have a desc (Description).")
end
+
+ def check_for_offense(regex, offense_msg)
+ # This method checks if particular regex has a match within formula's desc
+ # If so, adds a violation
+ match_object = @description.match(regex)
+ if match_object
+ column = @desc_begin_pos + match_object.begin(0) - @line_begin_pos + 1
+ length = match_object.to_s.length
+ offense_source_range = source_range(source_buffer, @line_number, column, length)
+ offense_msg = offense_msg % [match_object]
+ add_offense(@call_node, offense_source_range, offense_msg)
+ end
+ end
+
+ def check_for_desc_length_offense
+ # This method checks if desc length > max_desc_length
+ # If so, adds a violation
+ desc_length = "#{@formula_name}: #{@description}".length
+ max_desc_length = 80
+ if desc_length > max_desc_length
+ column = @desc_begin_pos - @line_begin_pos
+ length = @call_node.children[2].source_range.size
+ offense_source_range = source_range(source_buffer, @line_number, column, length)
+ desc_length_offense_msg = <<-EOS.undent
+ Description is too long. "name: desc" should be less than #{max_desc_length} characters.
+ Length is calculated as #{@formula_name} + desc. (currently #{"#{@formula_name}: #{@description}".length})
+ EOS
+ add_offense(@call_node, offense_source_range, desc_length_offense_msg)
+ end
+ end
end
end
end