rubocops/desc: use rubocop v1 API
This commit is contained in:
parent
929e481dce
commit
23b8d0ccb8
@ -11,19 +11,15 @@ module RuboCop
|
||||
module Cask
|
||||
# This cop audits `desc` in casks.
|
||||
# See the {DescHelper} module for details of the checks.
|
||||
class Desc < Cop
|
||||
class Desc < Base
|
||||
include OnDescStanza
|
||||
include DescHelper
|
||||
extend AutoCorrector
|
||||
|
||||
def on_desc_stanza(stanza)
|
||||
name = cask_block.header.cask_token
|
||||
@name = cask_block.header.cask_token
|
||||
desc_call = stanza.stanza_node
|
||||
audit_desc(:cask, name, desc_call)
|
||||
end
|
||||
|
||||
def autocorrect(node)
|
||||
name = cask_block.header.cask_token
|
||||
autocorrect_desc(node, name)
|
||||
audit_desc(:cask, @name, desc_call)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -12,14 +12,12 @@ module RuboCop
|
||||
# See the {DescHelper} module for details of the checks.
|
||||
class Desc < FormulaCop
|
||||
include DescHelper
|
||||
extend AutoCorrector
|
||||
|
||||
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||
@name = @formula_name
|
||||
desc_call = find_node_method_by_name(body_node, :desc)
|
||||
audit_desc(:formula, @formula_name, desc_call)
|
||||
end
|
||||
|
||||
def autocorrect(node)
|
||||
autocorrect_desc(node, @formula_name)
|
||||
audit_desc(:formula, @name, desc_call)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -38,39 +38,41 @@ module RuboCop
|
||||
end
|
||||
|
||||
# Check the desc for leading whitespace.
|
||||
problem "Description shouldn't have leading spaces." if regex_match_group(desc, /^\s+/)
|
||||
desc_problem "Description shouldn't have leading spaces." if regex_match_group(desc, /^\s+/)
|
||||
|
||||
# Check the desc for trailing whitespace.
|
||||
problem "Description shouldn't have trailing spaces." if regex_match_group(desc, /\s+$/)
|
||||
desc_problem "Description shouldn't have trailing spaces." if regex_match_group(desc, /\s+$/)
|
||||
|
||||
# Check if "command-line" is spelled incorrectly in the desc.
|
||||
if match = regex_match_group(desc, /(command ?line)/i)
|
||||
c = match.to_s[0]
|
||||
problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\"."
|
||||
desc_problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\"."
|
||||
end
|
||||
|
||||
# Check if the desc starts with an article.
|
||||
problem "Description shouldn't start with an article." if regex_match_group(desc, /^(the|an?)(?=\s)/i)
|
||||
desc_problem "Description shouldn't start with an article." if regex_match_group(desc, /^(the|an?)(?=\s)/i)
|
||||
|
||||
# Check if invalid lowercase words are at the start of a 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."
|
||||
desc_problem "Description should start with a capital letter."
|
||||
end
|
||||
|
||||
# Check if the desc starts with the formula's or cask's name.
|
||||
name_regex = name.delete("-").split("").join('[\s\-]?')
|
||||
problem "Description shouldn't start with the #{type} name." if regex_match_group(desc, /^#{name_regex}\b/i)
|
||||
if regex_match_group(desc, /^#{name_regex}\b/i)
|
||||
desc_problem "Description shouldn't start with the #{type} name."
|
||||
end
|
||||
|
||||
if type == :cask &&
|
||||
(match = regex_match_group(desc, /\b(macOS|Mac( ?OS( ?X)?)?|OS ?X)(?! virtual machines?)\b/i)) &&
|
||||
match[1] != "MAC"
|
||||
problem "Description shouldn't contain the platform."
|
||||
desc_problem "Description shouldn't contain the platform."
|
||||
end
|
||||
|
||||
# Check if a full stop is used at the end of a desc (apart from in the case of "etc.").
|
||||
if regex_match_group(desc, /\.$/) && !string_content(desc).end_with?("etc.")
|
||||
problem "Description shouldn't end with a full stop."
|
||||
desc_problem "Description shouldn't end with a full stop."
|
||||
end
|
||||
|
||||
# Check if the desc length exceeds maximum length.
|
||||
@ -80,9 +82,10 @@ module RuboCop
|
||||
"The current length is #{desc_length}."
|
||||
end
|
||||
|
||||
def autocorrect_desc(node, name)
|
||||
lambda do |corrector|
|
||||
/\A(?<quote>["'])(?<correction>.*)(?:\k<quote>)\Z/ =~ node.source
|
||||
# Auto correct desc problems. `regex_match_group` must be called before this to populate @offense_source_range.
|
||||
def desc_problem(message)
|
||||
add_offense(@offensive_source_range, message: message) do |corrector|
|
||||
/\A(?<quote>["'])(?<correction>.*)(?:\k<quote>)\Z/ =~ @offensive_node.source
|
||||
|
||||
next if correction.nil?
|
||||
|
||||
@ -98,12 +101,12 @@ module RuboCop
|
||||
end
|
||||
|
||||
correction.gsub!(/(ommand ?line)/i, "ommand-line")
|
||||
correction.gsub!(/(^|[^a-z])#{name}([^a-z]|$)/i, "\\1\\2")
|
||||
correction.gsub!(/(^|[^a-z])#{@name}([^a-z]|$)/i, "\\1\\2")
|
||||
correction.gsub!(/^\s+/, "")
|
||||
correction.gsub!(/\s+$/, "")
|
||||
correction.gsub!(/\.$/, "")
|
||||
|
||||
corrector.replace(node.source_range, "#{quote}#{correction}#{quote}")
|
||||
corrector.replace(@offensive_node.source_range, "#{quote}#{correction}#{quote}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,8 +6,8 @@ require "rubocops/formula_desc"
|
||||
describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
subject(:cop) { described_class.new }
|
||||
|
||||
context "When auditing formula desc" do
|
||||
it "When there is no desc" do
|
||||
context "When auditing formula `desc` methods" do
|
||||
it "reports an offense when there is no `desc`" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
^^^^^^^^^^^^^^^^^^^ Formula should have a desc (Description).
|
||||
@ -16,7 +16,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When desc is an empty string" do
|
||||
it "reports an offense when `desc` is an empty string" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -26,7 +26,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When desc is too long" do
|
||||
it "reports an offense when `desc` is too long" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -36,7 +36,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When desc is a multiline string" do
|
||||
it "reports an offense when `desc` is a multiline string" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -48,39 +48,39 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
end
|
||||
end
|
||||
|
||||
context "When auditing formula desc" do
|
||||
it "When the description starts with a leading space" do
|
||||
context "When auditing formula description texts" do
|
||||
it "reports an offense when the description starts with a leading space" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
desc ' Description with a leading space'
|
||||
^ Description shouldn\'t have leading spaces.
|
||||
^ Description shouldn't have leading spaces.
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When the description ends with a trailing space" do
|
||||
it "reports an offense when the description ends with a trailing space" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
desc 'Description with a trailing space '
|
||||
^ Description shouldn\'t have trailing spaces.
|
||||
^ Description shouldn't have trailing spaces.
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When \"command-line\" is incorrectly spelled in the desc" do
|
||||
it "reports an offense when \"command-line\" is incorrectly spelled in the description" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
desc 'command line'
|
||||
^ Description should start with a capital letter.
|
||||
^^^^^^^^^^^^ Description should use \"command-line\" instead of \"command line\".
|
||||
^^^^^^^^^^^^ Description should use "command-line" instead of "command line".
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When an article is used in the desc" do
|
||||
it "reports an offense when an article is used in the description" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -98,7 +98,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When the desc starts with a lowercase letter" do
|
||||
it "reports an offense when the description starts with a lowercase letter" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -108,7 +108,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When the desc starts with the formula name" do
|
||||
it "reports an offense when the description starts with the formula name" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -118,7 +118,7 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "When the description ends with a full stop" do
|
||||
it "reports an offense when the description ends with a full stop" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
@ -128,23 +128,23 @@ describe RuboCop::Cop::FormulaAudit::Desc do
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "autocorrects all rules" do
|
||||
source = <<~RUBY
|
||||
it "reports and corrects all rules for description text" do
|
||||
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
desc ' an bar: commandline foo '
|
||||
^ Description shouldn't have trailing spaces.
|
||||
^^^^^^^^^^^ Description should use "command-line" instead of "commandline".
|
||||
^ Description shouldn't have leading spaces.
|
||||
end
|
||||
RUBY
|
||||
|
||||
correct_source = <<~RUBY
|
||||
expect_correction(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
desc 'Bar: command-line'
|
||||
end
|
||||
RUBY
|
||||
|
||||
corrected_source = autocorrect_source(source, "/homebrew-core/Formula/foo.rb")
|
||||
expect(corrected_source).to eq(correct_source)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user