Add test for negated build.with?
This commit is contained in:
parent
ec2b0df10e
commit
e14fedd1b3
@ -252,11 +252,15 @@ module RuboCop
|
||||
end
|
||||
|
||||
# Check if method_name is called among the direct children nodes in the given node
|
||||
# Check if the node itself is the method
|
||||
def method_called?(node, method_name)
|
||||
if node.send_type? && node.method_name == method_name
|
||||
offending_node(node)
|
||||
return true
|
||||
end
|
||||
node.each_child_node(:send) do |call_node|
|
||||
next unless call_node.method_name == method_name
|
||||
@offensive_node = call_node
|
||||
@offense_source_range = call_node.source_range
|
||||
offending_node(call_node)
|
||||
return true
|
||||
end
|
||||
false
|
||||
|
||||
@ -287,11 +287,11 @@ module RuboCop
|
||||
problem "Use if #{correct} instead of unless #{m.source}"
|
||||
end
|
||||
|
||||
# find_instance_method_call(body_node, :build, :with?) do |m|
|
||||
# next unless negation?(m)
|
||||
# problem "Don't negate 'build.with?': use 'build.without?'"
|
||||
# end
|
||||
#
|
||||
find_instance_method_call(body_node, :build, :with?) do |m|
|
||||
next unless method_called?(m.parent, :!)
|
||||
problem "Don't negate 'build.with?': use 'build.without?'"
|
||||
end
|
||||
|
||||
# find_instance_method_call(body_node, :build, :without?) do |m|
|
||||
# next unless negation?(m)
|
||||
# problem "Don't negate 'build.without?': use 'build.with?'"
|
||||
@ -324,6 +324,7 @@ module RuboCop
|
||||
end
|
||||
|
||||
def unless_modifier?(node)
|
||||
return false unless node.if_type?
|
||||
node.modifier_form? && node.unless?
|
||||
end
|
||||
|
||||
|
||||
@ -667,6 +667,30 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
|
||||
expect_offense(expected, actual)
|
||||
end
|
||||
end
|
||||
|
||||
it "with negated build.with?" do
|
||||
source = <<-EOS.undent
|
||||
class Foo < Formula
|
||||
desc "foo"
|
||||
url 'http://example.com/foo-1.0.tgz'
|
||||
def post_install
|
||||
return if !build.with? "bar"
|
||||
end
|
||||
end
|
||||
EOS
|
||||
|
||||
expected_offenses = [{ message: "Don't negate 'build.with?': use 'build.without?'",
|
||||
severity: :convention,
|
||||
line: 5,
|
||||
column: 14,
|
||||
source: source }]
|
||||
|
||||
inspect_source(cop, source)
|
||||
|
||||
expected_offenses.zip(cop.offenses).each do |expected, actual|
|
||||
expect_offense(expected, actual)
|
||||
end
|
||||
end
|
||||
end
|
||||
def expect_offense(expected, actual)
|
||||
expect(actual.message).to eq(expected[:message])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user