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