mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Crystal 0.24.1 support
This commit is contained in:
		
							parent
							
								
									eca0d28692
								
							
						
					
					
						commit
						7e8703dda0
					
				
					 6 changed files with 12 additions and 181 deletions
				
			
		| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
require "../../spec/spec_helper"
 | 
					require "../../spec_helper"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Ameba
 | 
					module Ameba
 | 
				
			||||||
  describe Rule::Base do
 | 
					  describe Rule::Base do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,118 +0,0 @@
 | 
				
			||||||
require "../../spec_helper"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module Ameba::Rule
 | 
					 | 
				
			||||||
  describe DuplicatedWhen do
 | 
					 | 
				
			||||||
    subject = DuplicatedWhen.new
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "passes if there are no duplicated when conditions in the case" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when "first"
 | 
					 | 
				
			||||||
          do_something
 | 
					 | 
				
			||||||
        when "second"
 | 
					 | 
				
			||||||
          do_something_else
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when Integer then :one
 | 
					 | 
				
			||||||
        when Int32, String then :two
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case {value1, value2}
 | 
					 | 
				
			||||||
        when {0, _}
 | 
					 | 
				
			||||||
        when {_, 0}
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when .odd?
 | 
					 | 
				
			||||||
        when .even?
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case
 | 
					 | 
				
			||||||
        when Integer then :one
 | 
					 | 
				
			||||||
        when Integer | String then :two
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when :one
 | 
					 | 
				
			||||||
        when "one"
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        case {value1, value2}
 | 
					 | 
				
			||||||
        when {String, String}
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "fails if there is a duplicated when condition in the case" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when .starts_with?("pattern1")
 | 
					 | 
				
			||||||
          do_something1
 | 
					 | 
				
			||||||
        when .starts_with?("pattern2")
 | 
					 | 
				
			||||||
          do_something2
 | 
					 | 
				
			||||||
        when .starts_with?("pattern1")
 | 
					 | 
				
			||||||
          do_something3
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "fails if there are multiple when conditions with nil" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when nil
 | 
					 | 
				
			||||||
          say_hello
 | 
					 | 
				
			||||||
        when nil
 | 
					 | 
				
			||||||
          say_nothing
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "fails if there are multiple whens with is_a? conditions" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when String then say_hello
 | 
					 | 
				
			||||||
        when Integer then say_nothing
 | 
					 | 
				
			||||||
        when String then blah
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "fails if there are duplicated whens but in difference order" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when String, Integer then something
 | 
					 | 
				
			||||||
        when Integer then blah
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "fails if there are duplicated conditions in one when" do
 | 
					 | 
				
			||||||
      s = Source.new %(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when String, String then something
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it "reports rule, location and message" do
 | 
					 | 
				
			||||||
      s = Source.new %q(
 | 
					 | 
				
			||||||
        case x
 | 
					 | 
				
			||||||
        when "first"
 | 
					 | 
				
			||||||
        when "first"
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      ), "source.cr"
 | 
					 | 
				
			||||||
      subject.catch(s).should_not be_valid
 | 
					 | 
				
			||||||
      error = s.errors.first
 | 
					 | 
				
			||||||
      error.rule.should_not be_nil
 | 
					 | 
				
			||||||
      error.location.to_s.should eq "source.cr:2:9"
 | 
					 | 
				
			||||||
      error.message.should eq "Duplicated when conditions in case"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
| 
						 | 
					@ -104,7 +104,7 @@ module Ameba
 | 
				
			||||||
      error = s.errors.first
 | 
					      error = s.errors.first
 | 
				
			||||||
      error.rule.should_not be_nil
 | 
					      error.rule.should_not be_nil
 | 
				
			||||||
      error.location.to_s.should eq "source.cr:2:12"
 | 
					      error.location.to_s.should eq "source.cr:2:12"
 | 
				
			||||||
      error.message.should eq "Avoid empty expression '()'"
 | 
					      error.message.should eq "Avoid empty expressions"
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -158,6 +158,7 @@ module Ameba::Rule
 | 
				
			||||||
    it "reports rule, location and a message" do
 | 
					    it "reports rule, location and a message" do
 | 
				
			||||||
      s = Source.new %q(
 | 
					      s = Source.new %q(
 | 
				
			||||||
        begin
 | 
					        begin
 | 
				
			||||||
 | 
					          do_something
 | 
				
			||||||
        rescue Exception | IndexError
 | 
					        rescue Exception | IndexError
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      ), "source.cr"
 | 
					      ), "source.cr"
 | 
				
			||||||
| 
						 | 
					@ -165,7 +166,7 @@ module Ameba::Rule
 | 
				
			||||||
      error = s.errors.first
 | 
					      error = s.errors.first
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      error.rule.should_not be_nil
 | 
					      error.rule.should_not be_nil
 | 
				
			||||||
      error.location.to_s.should eq "source.cr:2:9"
 | 
					      error.location.to_s.should eq "source.cr:3:11"
 | 
				
			||||||
      error.message.should eq(
 | 
					      error.message.should eq(
 | 
				
			||||||
        "Exception handler has shadowed exceptions: IndexError"
 | 
					        "Exception handler has shadowed exceptions: IndexError"
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,11 +53,14 @@ module Ameba::Formatter
 | 
				
			||||||
              .compact
 | 
					              .compact
 | 
				
			||||||
              .uniq!
 | 
					              .uniq!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      YAML.build do |yaml|
 | 
					      nodes_builder = YAML::Nodes::Builder.new
 | 
				
			||||||
        yaml.mapping do
 | 
					      nodes_builder.mapping do
 | 
				
			||||||
          rule.name.to_yaml(yaml)
 | 
					        rule.name.to_yaml nodes_builder
 | 
				
			||||||
          rule.to_yaml(yaml)
 | 
					        rule.to_yaml nodes_builder
 | 
				
			||||||
        end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      YAML.build do |builder|
 | 
				
			||||||
 | 
					        nodes_builder.document.to_yaml(builder)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,55 +0,0 @@
 | 
				
			||||||
module Ameba::Rule
 | 
					 | 
				
			||||||
  # A rule that disallows duplicated when conditions in case.
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # This is considered invalid:
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  # case a
 | 
					 | 
				
			||||||
  # when "first"
 | 
					 | 
				
			||||||
  #   do_something
 | 
					 | 
				
			||||||
  # when "first"
 | 
					 | 
				
			||||||
  #   do_somehting_else
 | 
					 | 
				
			||||||
  # end
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # And it should be written as follows:
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  # case a
 | 
					 | 
				
			||||||
  # when "first"
 | 
					 | 
				
			||||||
  #   do_something
 | 
					 | 
				
			||||||
  # when "second"
 | 
					 | 
				
			||||||
  #   do_somehting_else
 | 
					 | 
				
			||||||
  # end
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # YAML configuration example:
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  # DuplicatedWhen:
 | 
					 | 
				
			||||||
  #   Enabled: true
 | 
					 | 
				
			||||||
  # ```
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  struct DuplicatedWhen < Base
 | 
					 | 
				
			||||||
    properties do
 | 
					 | 
				
			||||||
      description = "Disallows duplicated when conditions in case"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def test(source)
 | 
					 | 
				
			||||||
      AST::Visitor.new self, source
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def test(source, node : Crystal::Case)
 | 
					 | 
				
			||||||
      return unless duplicated_whens?(node.whens)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      source.error self, node.location, "Duplicated when conditions in case"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private def duplicated_whens?(whens)
 | 
					 | 
				
			||||||
      whens.map(&.conds.map &.to_s)
 | 
					 | 
				
			||||||
           .flatten
 | 
					 | 
				
			||||||
           .group_by(&.itself)
 | 
					 | 
				
			||||||
           .any? { |_, v| v.size > 1 }
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue