diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 48aea66162..f0ddbd2965 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -838,15 +838,10 @@ module Homebrew def line_problems(line, _lineno) # Check for string interpolation of single values. - if line =~ /(system|inreplace|gsub!|change_make_var!).*[ ,]"#\{([\w.]+)\}"/ - # TODO: check could be in RuboCop - problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}" - end - - return unless @strict + return unless line =~ /(system|inreplace|gsub!|change_make_var!).*[ ,]"#\{([\w.]+)\}"/ # TODO: check could be in RuboCop - problem "`#{Regexp.last_match(1)}` is now unnecessary" if line =~ /(require ["']formula["'])/ + problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}" end def audit_reverse_migration diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 28456e8e93..ff86ddd1c4 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -6,7 +6,19 @@ module RuboCop module Cop module FormulaAudit class Text < FormulaCop - def audit_formula(_node, _class_node, _parent_class_node, body_node) + def audit_formula(node, _class_node, _parent_class_node, body_node) + @full_source_content = source_buffer(node).source + + if match = @full_source_content.match(/^require ['"]formula['"]$/) + @offensive_node = node + @source_buf = source_buffer(node) + @line_no = match.pre_match.count("\n") + 1 + @column = 0 + @length = match[0].length + @offense_source_range = source_range(@source_buf, @line_no, @column, @length) + problem "`#{match}` is now unnecessary" + end + if !find_node_method_by_name(body_node, :plist_options) && find_method_def(body_node, :plist) problem "Please set plist_options when using a formula-defined plist." diff --git a/Library/Homebrew/test/rubocops/text_spec.rb b/Library/Homebrew/test/rubocops/text_spec.rb index c357375ae4..4ccb551145 100644 --- a/Library/Homebrew/test/rubocops/text_spec.rb +++ b/Library/Homebrew/test/rubocops/text_spec.rb @@ -6,6 +6,17 @@ describe RuboCop::Cop::FormulaAudit::Text do subject(:cop) { described_class.new } context "When auditing formula text" do + it "with `require \"formula\"` is present" do + expect_offense(<<~RUBY) + require "formula" + ^^^^^^^^^^^^^^^^^ `require "formula"` is now unnecessary + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + homepage "https://brew.sh" + end + RUBY + end + it "with both openssl and libressl optional dependencies" do expect_offense(<<~RUBY) class Foo < Formula