style: enforce ISO-8601 in deprecation date
This commit is contained in:
		
							parent
							
								
									db998860f1
								
							
						
					
					
						commit
						9520eabc35
					
				@ -22,5 +22,6 @@ require "rubocops/uses_from_macos"
 | 
			
		||||
require "rubocops/files"
 | 
			
		||||
require "rubocops/keg_only"
 | 
			
		||||
require "rubocops/version"
 | 
			
		||||
require "rubocops/deprecate"
 | 
			
		||||
 | 
			
		||||
require "rubocops/rubocop-cask"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										35
									
								
								Library/Homebrew/rubocops/deprecate.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								Library/Homebrew/rubocops/deprecate.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require "rubocops/extend/formula"
 | 
			
		||||
 | 
			
		||||
module RuboCop
 | 
			
		||||
  module Cop
 | 
			
		||||
    module FormulaAudit
 | 
			
		||||
      # This cop audits deprecate!
 | 
			
		||||
      class Deprecate < FormulaCop
 | 
			
		||||
        def audit_formula(_node, _class_node, _parent_class_node, body_node)
 | 
			
		||||
          deprecate_node = find_node_method_by_name(body_node, :deprecate!)
 | 
			
		||||
 | 
			
		||||
          return if deprecate_node.nil? || deprecate_node.children.length < 3
 | 
			
		||||
 | 
			
		||||
          date_node = find_strings(deprecate_node).first
 | 
			
		||||
 | 
			
		||||
          begin
 | 
			
		||||
            Date.iso8601(string_content(date_node))
 | 
			
		||||
          rescue ArgumentError
 | 
			
		||||
            fixed_date_string = Date.parse(string_content(date_node)).iso8601
 | 
			
		||||
            offending_node(date_node)
 | 
			
		||||
            problem "Use `#{fixed_date_string}` to comply with ISO 8601"
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def autocorrect(node)
 | 
			
		||||
          lambda do |corrector|
 | 
			
		||||
            fixed_fixed_date_string = Date.parse(string_content(node)).iso8601
 | 
			
		||||
            corrector.replace(node.source_range, "\"#{fixed_fixed_date_string}\"")
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										56
									
								
								Library/Homebrew/test/rubocops/deprecate_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Library/Homebrew/test/rubocops/deprecate_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,56 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require "rubocops/deprecate"
 | 
			
		||||
 | 
			
		||||
describe RuboCop::Cop::FormulaAudit::Deprecate do
 | 
			
		||||
  subject(:cop) { described_class.new }
 | 
			
		||||
 | 
			
		||||
  context "When auditing formula for deprecate!" do
 | 
			
		||||
    it "deprecation date is not ISO-8601 compliant" do
 | 
			
		||||
      expect_offense(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          deprecate! :date => "June 25, 2020"
 | 
			
		||||
                              ^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "deprecation date is ISO-8601 compliant" do
 | 
			
		||||
      expect_no_offenses(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          deprecate! :date => "2020-06-25"
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "no deprecation date" do
 | 
			
		||||
      expect_no_offenses(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          deprecate!
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "auto corrects to ISO-8601 format" do
 | 
			
		||||
      source = <<~RUBY
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          deprecate! :date => "June 25, 2020"
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
 | 
			
		||||
      corrected_source = <<~RUBY
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          deprecate! :date => "2020-06-25"
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
 | 
			
		||||
      new_source = autocorrect_source(source)
 | 
			
		||||
      expect(new_source).to eq(corrected_source)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user