diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 55f22bbc61..8ef5e9ace3 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -58,7 +58,7 @@ module RuboCop # Returns all string nodes among the descendants of given node def find_strings(node) return [] if node.nil? - return node if node.str_type? + return [node] if node.str_type? node.each_descendant(:str) end diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 4e7aa1adb2..141ab6fdf3 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -228,12 +228,15 @@ module RuboCop end find_every_method_call_by_name(body_node, :depends_on).each do |method| - next if modifier?(method.parent) param = parameters(method).first - dep, option = hash_dep(param) - next if dep.nil? || option.nil? - offending_node(param) - problem "Dependency #{string_content(dep)} should not use option #{string_content(option)}" + dep, option_child_nodes = hash_dep(param) + next if dep.nil? || option_child_nodes.empty? + option_child_nodes.each do |option| + find_strings(option).each do |dependency| + next unless match = regex_match_group(dependency, /(with(out)?-\w+|c\+\+11)/) + problem "Dependency #{string_content(dep)} should not use option #{match[0]}" + end + end end find_instance_method_call(body_node, :version, :==) do |method| @@ -363,10 +366,8 @@ module RuboCop (send nil? :depends_on $({str sym} _)))} EOS - # Match depends_on with hash as argument def_node_matcher :hash_dep, <<~EOS - {(hash (pair $(str _) $(str _))) - (hash (pair $(str _) (array $(str _) ...)))} + (hash (pair $(str _) $...)) EOS def_node_matcher :destructure_hash, <<~EOS diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index 19492c252b..5048b118d1 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -693,13 +693,27 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do RUBY end - it "dependencies with invalid options" do + it "dependencies with invalid options which lead to force rebuild" do expect_offense(<<~RUBY) class Foo < Formula desc "foo" url 'http://example.com/foo-1.0.tgz' depends_on "foo" => "with-bar" - ^^^^^^^^^^^^^^^^^^^ Dependency foo should not use option with-bar + ^^^^^^^^ Dependency foo should not use option with-bar + end + RUBY + end + + it "dependencies with invalid options in array value which lead to force rebuild" do + expect_offense(<<~RUBY) + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + depends_on "httpd" => [:build, :test] + depends_on "foo" => [:optional, "with-bar"] + ^^^^^^^^ Dependency foo should not use option with-bar + depends_on "icu4c" => [:optional, "c++11"] + ^^^^^ Dependency icu4c should not use option c++11 end RUBY end