diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 443eab7a58..82df0a41ab 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -10,17 +10,16 @@ module RuboCop # # @api private class Text < FormulaCop - def audit_formula(node, _class_node, _parent_class_node, body_node) - @full_source_content = source_buffer(node).source + extend AutoCorrector - 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" + 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['"]$/) + range = source_range(source_buffer(node), match.pre_match.count("\n") + 1, 0, match[0].length) + add_offense(range, message: "`#{match}` is now unnecessary") do |corrector| + corrector.remove(range_with_surrounding_space(range: range)) + end end if !find_node_method_by_name(body_node, :plist_options) && diff --git a/Library/Homebrew/test/rubocops/text_spec.rb b/Library/Homebrew/test/rubocops/text_spec.rb index 63c49df4f8..b7730e37db 100644 --- a/Library/Homebrew/test/rubocops/text_spec.rb +++ b/Library/Homebrew/test/rubocops/text_spec.rb @@ -16,6 +16,13 @@ describe RuboCop::Cop::FormulaAudit::Text do homepage "https://brew.sh" end RUBY + + expect_correction(<<~RUBY) + 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