diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 94952d1f53..862dabfda4 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -297,6 +297,11 @@ module RuboCop true end + # Check if negation is present in the given node + def negated?(node) + method_called?(node, :!) + end + # Return all the caveats' string nodes in an array def caveats_strings find_strings(find_method_def(@body, :caveats)) diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 4c82b42cf1..6c0c3ec736 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -288,15 +288,15 @@ module RuboCop end find_instance_method_call(body_node, :build, :with?) do |m| - next unless method_called?(m.parent, :!) + next unless negated?(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?'" - # end - # + find_instance_method_call(body_node, :build, :without?) do |m| + next unless negated?(m.parent) + problem "Don't negate 'build.without?': use 'build.with?'" + end + # find_instance_method_call(body_node, :build, :without?) do |m| # arg = parameters(m).first # next unless match = regex_match_group(arg, %r{-?-?without-(.*)}) diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index 8e3f42adf6..e008349635 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -691,6 +691,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.without? "bar" + end + end + EOS + + expected_offenses = [{ message: "Don't negate 'build.without?': use 'build.with?'", + 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])