diff --git a/Library/Homebrew/rubocops/checksum.rb b/Library/Homebrew/rubocops/checksum.rb index daeab70415..e8035b8ee3 100644 --- a/Library/Homebrew/rubocops/checksum.rb +++ b/Library/Homebrew/rubocops/checksum.rb @@ -38,7 +38,7 @@ module RuboCop return unless regex_match_group(checksum, /[^a-f0-9]+/i) - problem "sha256 contains invalid characters" + add_offense(@offensive_source_range, message: "sha256 contains invalid characters") end end @@ -46,6 +46,8 @@ module RuboCop # # @api private class ChecksumCase < FormulaCop + extend AutoCorrector + def audit_formula(_node, _class_node, _parent_class_node, body_node) return if body_node.nil? @@ -55,15 +57,11 @@ module RuboCop next if checksum.nil? next unless regex_match_group(checksum, /[A-F]+/) - problem "sha256 should be lowercase" - end - end - - def autocorrect(node) - lambda do |corrector| - correction = node.source.downcase - corrector.insert_before(node.source_range, correction) - corrector.remove(node.source_range) + add_offense(@offensive_source_range, message: "sha256 should be lowercase") do |corrector| + correction = @offensive_node.source.downcase + corrector.insert_before(@offensive_node.source_range, correction) + corrector.remove(@offensive_node.source_range) + end end end end diff --git a/Library/Homebrew/rubocops/shared/helper_functions.rb b/Library/Homebrew/rubocops/shared/helper_functions.rb index a2270efa47..c9330051d2 100644 --- a/Library/Homebrew/rubocops/shared/helper_functions.rb +++ b/Library/Homebrew/rubocops/shared/helper_functions.rb @@ -29,6 +29,7 @@ module RuboCop @line_no = line_number(node) @source_buf = source_buffer(node) @offensive_node = node + @offensive_source_range = source_range(@source_buf, @line_no, @column, @length) match_object end diff --git a/Library/Homebrew/test/rubocops/checksum_spec.rb b/Library/Homebrew/test/rubocops/checksum_spec.rb index 4546c4f37a..0372161d7e 100644 --- a/Library/Homebrew/test/rubocops/checksum_spec.rb +++ b/Library/Homebrew/test/rubocops/checksum_spec.rb @@ -33,12 +33,12 @@ describe RuboCop::Cop::FormulaAudit::Checksum do stable do url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" sha256 "5cf6e1ae0a645b426c0474cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9ad" - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters resource "foo-package" do url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" sha256 "5cf6e1ae0a645b426c047aaa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9" - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters end end end @@ -89,7 +89,7 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do RUBY end - it "reports an offense if a checksum outside a `stable` block contains uppercase letters" do + it "reports and corrects an offense if a checksum outside a `stable` block contains uppercase letters" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -109,27 +109,14 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do end end RUBY - end - it "auto-corrects checksums that contain uppercase letters" do - source = <<~RUBY + expect_correction(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' - stable do - url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" - sha256 "5cf6e1ae0A645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" - - resource "foo-package" do - url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" - sha256 "5cf6e1Ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" - end + resource "foo-outside" do + url "https://github.com/foo-lang/foo-outside/archive/0.18.0.tar.gz" + sha256 "a4cc7cd3f7d1605ffa1ac5755cf6e1ae0a645b426b047a6a39a8b2268ddc7ea9" end - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' stable do url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" @@ -141,9 +128,6 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do end end RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) end end end