style: check deprecate! reason
This commit is contained in:
parent
8db3471ff7
commit
6162799d63
@ -32,6 +32,43 @@ module RuboCop
|
||||
(pair (sym :date) $str)
|
||||
EOS
|
||||
end
|
||||
|
||||
# This cop audits deprecate! reason
|
||||
class DeprecateReason < FormulaCop
|
||||
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||
deprecate_node = find_node_method_by_name(body_node, :deprecate!)
|
||||
|
||||
return if deprecate_node.nil?
|
||||
|
||||
deprecate_reason(deprecate_node) do |reason_node|
|
||||
offending_node(reason_node)
|
||||
reason_string = string_content(reason_node)
|
||||
|
||||
problem "Do not start the reason with `it`" if reason_string.start_with?("it ")
|
||||
|
||||
problem "Do not end the reason with a punctuation mark" if %w[. ! ?].include?(reason_string[-1])
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
problem 'Add a reason for deprecation: `deprecate! because: "..."`'
|
||||
end
|
||||
|
||||
def autocorrect(node)
|
||||
return unless node.str_type?
|
||||
|
||||
lambda do |corrector|
|
||||
reason = string_content(node)
|
||||
reason = reason[3..] if reason.start_with?("it ")
|
||||
reason.chop! if %w[. ! ?].include?(reason[-1])
|
||||
corrector.replace(node.source_range, "\"#{reason}\"")
|
||||
end
|
||||
end
|
||||
|
||||
def_node_search :deprecate_reason, <<~EOS
|
||||
(pair (sym :because) $str)
|
||||
EOS
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -21,7 +21,7 @@ RSpec/InstanceVariable:
|
||||
- 'utils/git_spec.rb'
|
||||
- 'version_spec.rb'
|
||||
|
||||
# Offense count: 75
|
||||
# Offense count: 76
|
||||
RSpec/MultipleDescribes:
|
||||
Exclude:
|
||||
- 'ENV_spec.rb'
|
||||
@ -93,6 +93,7 @@ RSpec/MultipleDescribes:
|
||||
- 'patch_spec.rb'
|
||||
- 'rubocops/checksum_spec.rb'
|
||||
- 'rubocops/class_spec.rb'
|
||||
- 'rubocops/deprecate_spec.rb'
|
||||
- 'rubocops/formula_desc_spec.rb'
|
||||
- 'rubocops/lines_spec.rb'
|
||||
- 'rubocops/text_spec.rb'
|
||||
|
||||
@ -101,3 +101,145 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDate do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe RuboCop::Cop::FormulaAudit::DeprecateReason do
|
||||
subject(:cop) { described_class.new }
|
||||
|
||||
context "When auditing formula for deprecate! because:" do
|
||||
it "deprecation reason is acceptable" do
|
||||
expect_no_offenses(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken"
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason is acceptable with date" do
|
||||
expect_no_offenses(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! date: "2020-08-28", because: "is broken"
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason is absent" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate!
|
||||
^^^^^^^^^^ Add a reason for deprecation: `deprecate! because: "..."`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason is absent with date" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! date: "2020-08-28"
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add a reason for deprecation: `deprecate! because: "..."`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason starts with `it`" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "it is broken"
|
||||
^^^^^^^^^^^^^^ Do not start the reason with `it`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason starts with `it` with date" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! date: "2020-08-28", because: "it is broken"
|
||||
^^^^^^^^^^^^^^ Do not start the reason with `it`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason ends with a period" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken."
|
||||
^^^^^^^^^^^^ Do not end the reason with a punctuation mark
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason ends with an exclamation point" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken!"
|
||||
^^^^^^^^^^^^ Do not end the reason with a punctuation mark
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason ends with a question mark" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken?"
|
||||
^^^^^^^^^^^^ Do not end the reason with a punctuation mark
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "deprecation reason ends with a period with date" do
|
||||
expect_offense(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! date: "2020-08-28", because: "is broken."
|
||||
^^^^^^^^^^^^ Do not end the reason with a punctuation mark
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "auto corrects to remove `it`" do
|
||||
source = <<~RUBY
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "it is broken"
|
||||
end
|
||||
RUBY
|
||||
|
||||
corrected_source = <<~RUBY
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken"
|
||||
end
|
||||
RUBY
|
||||
|
||||
new_source = autocorrect_source(source)
|
||||
expect(new_source).to eq(corrected_source)
|
||||
end
|
||||
|
||||
it "auto corrects to remove punctuation" do
|
||||
source = <<~RUBY
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken."
|
||||
end
|
||||
RUBY
|
||||
|
||||
corrected_source = <<~RUBY
|
||||
class Foo < Formula
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
deprecate! because: "is broken"
|
||||
end
|
||||
RUBY
|
||||
|
||||
new_source = autocorrect_source(source)
|
||||
expect(new_source).to eq(corrected_source)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user