diff --git a/Library/Homebrew/rubocops/formula_desc_cop.rb b/Library/Homebrew/rubocops/formula_desc_cop.rb index faffd42f6c..abdaf869fe 100644 --- a/Library/Homebrew/rubocops/formula_desc_cop.rb +++ b/Library/Homebrew/rubocops/formula_desc_cop.rb @@ -38,6 +38,7 @@ module RuboCop module FormulaAuditStrict # 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 @@ -62,6 +63,16 @@ module RuboCop desc = parameters(desc_call).first + # Check for leading whitespace. + if regex_match_group(desc, /^\s+/) + problem "Description shouldn't have a leading space" + end + + # Check for trailing whitespace. + if regex_match_group(desc, /\s+$/) + problem "Description shouldn't have a trailing space" + end + # 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 @@ -104,6 +115,8 @@ module RuboCop 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 diff --git a/Library/Homebrew/test/rubocops/formula_desc_cop_spec.rb b/Library/Homebrew/test/rubocops/formula_desc_cop_spec.rb index cc838a6bfc..76782652c7 100644 --- a/Library/Homebrew/test/rubocops/formula_desc_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/formula_desc_cop_spec.rb @@ -65,7 +65,7 @@ describe RuboCop::Cop::FormulaAuditStrict::Desc do expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") class Foo < Formula url 'http://example.com/foo-1.0.tgz' - desc 'An ' + desc 'An aardvark' ^^^ Description shouldn\'t start with an indefinite article i.e. \"An\" end RUBY @@ -101,6 +101,26 @@ describe RuboCop::Cop::FormulaAuditStrict::Desc do RUBY end + it "When the description starts with a leading space" do + expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + desc ' Description with a leading space' + ^ Description shouldn\'t have a leading space + end + RUBY + end + + it "When the description ends with a trailing space" do + expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + desc 'Description with a trailing space ' + ^ Description shouldn\'t have a trailing space + end + RUBY + end + it "autocorrects all rules" do source = <<~RUBY class Foo < Formula