Class: RuboCop::Cop::FormulaAuditStrict::Desc

Inherits:
RuboCop::Cop::FormulaCop show all
Defined in:
brew/Library/Homebrew/rubocops/formula_desc.rb

Overview

This cop audits desc in Formulae.

  • Checks for leading/trailing whitespace in desc
  • Checks if desc begins with an article
  • Checks for correct usage of command-line in desc
  • Checks description starts with a capital letter
  • Checks if desc contains the formula name
  • Checks if desc ends with a full stop (apart from in the case of “etc.”)

Constant Summary collapse

VALID_LOWERCASE_WORDS =
%w[
  ex
  eXtensible
  iOS
  macOS
  malloc
  ooc
  preexec
  x86
  xUnit
].freeze

Instance Attribute Summary

Attributes inherited from RuboCop::Cop::FormulaCop

#file_path

Instance Method Summary collapse

Methods inherited from RuboCop::Cop::FormulaCop

#audit_comments, #audit_urls, #block_size, #caveats_strings, #check_precedence, #class_name, #component_precedes?, #depends_on?, #depends_on_name_type?, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_func_call_by_name, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #formula_tap, #get_checksum_node, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_called_in_block?, #method_name, #node_equals?, #offending_node, #on_class, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content, #versioned_formula?

Instance Method Details

#audit_formula(_node, _class_node, _parent_class_node, body_node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'brew/Library/Homebrew/rubocops/formula_desc.rb', line 63

def audit_formula(_node, _class_node, _parent_class_node, body_node)
  desc_call = find_node_method_by_name(body_node, :desc)
  return if desc_call.nil?

  desc = parameters(desc_call).first

  # Check for leading whitespace.
  problem "Description shouldn't have a leading space" if regex_match_group(desc, /^\s+/)

  # Check for trailing whitespace.
  problem "Description shouldn't have a trailing space" if regex_match_group(desc, /\s+$/)

  # Check if command-line is wrongly used in formula's desc
  if match = regex_match_group(desc, /(command ?line)/i)
    c = match.to_s.chars.first
    problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\""
  end

  # Check if a/an are used in a formula's desc
  if match = regex_match_group(desc, /^(an?)\s/i)
    problem "Description shouldn't start with an indefinite article, i.e. \"#{match.to_s.strip}\""
  end

  # Check if invalid uppercase words are at the start of a
  # formula's desc
  if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) &&
     regex_match_group(desc, /^[a-z]/)
    problem "Description should start with a capital letter"
  end

  # Check if formula's desc starts with formula's name
  if regex_match_group(desc, /^#{@formula_name} /i)
    problem "Description shouldn't start with the formula name"
  end

  # Check if a full stop is used at the end of a formula's desc (apart from in the case of "etc.")
  return unless regex_match_group(desc, /\.$/) && !string_content(desc).end_with?("etc.")

  problem "Description shouldn't end with a full stop"
end

#autocorrect(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'brew/Library/Homebrew/rubocops/formula_desc.rb', line 104

def autocorrect(node)
  lambda do |corrector|
    correction = node.source
    first_word = string_content(node).split.first
    unless VALID_LOWERCASE_WORDS.include?(first_word)
      first_char = first_word.to_s.chars.first
      correction.sub!(/^(['"]?)([a-z])/, "\\1#{first_char.upcase}")
    end
    correction.sub!(/^(['"]?)an?\s/i, "\\1")
    correction.gsub!(/(ommand ?line)/i, "ommand-line")
    correction.gsub!(/(^|[^a-z])#{@formula_name}([^a-z]|$)/i, "\\1\\2")
    correction.gsub!(/^(['"]?)\s+/, "\\1")
    correction.gsub!(/\s+(['"]?)$/, "\\1")
    correction.gsub!(/\.(['"]?)$/, "\\1")
    correction.gsub!(/^\s+/, "")
    correction.gsub!(/\s+$/, "")
    corrector.insert_before(node.source_range, correction)
    corrector.remove(node.source_range)
  end
end