mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Shadowed argument
This commit is contained in:
		
							parent
							
								
									15bb8f5331
								
							
						
					
					
						commit
						c12b4f1aa5
					
				
					 7 changed files with 287 additions and 12 deletions
				
			
		
							
								
								
									
										165
									
								
								spec/ameba/rule/shadowed_argument_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										165
									
								
								spec/ameba/rule/shadowed_argument_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,165 @@
 | 
			
		|||
require "../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule
 | 
			
		||||
  describe ShadowedArgument do
 | 
			
		||||
    subject = ShadowedArgument.new
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if there is not a shadowed argument" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          baz = 1
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        3.times do |i|
 | 
			
		||||
          a = 1
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        proc = -> (a : Int32) {
 | 
			
		||||
          b = 2
 | 
			
		||||
        }
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports if there is a shadowed method argument" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar = 1
 | 
			
		||||
          bar
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports if there is a shadowed block argument" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        3.times do |i|
 | 
			
		||||
          i = 2
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports if there is a shadowed proc argument" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        ->(x : Int32) {
 | 
			
		||||
          x = 20
 | 
			
		||||
          x
 | 
			
		||||
        }
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if the argument is referenced before the assignment" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar
 | 
			
		||||
          bar = 1
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if the argument is conditionally reassigned" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar = nil)
 | 
			
		||||
          bar ||= true
 | 
			
		||||
          bar
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if the op assign is followed by another assignment" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar ||= 3
 | 
			
		||||
          bar = 43
 | 
			
		||||
          bar
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports if the shadowing assignment is followed by op assign" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar = 42
 | 
			
		||||
          bar ||= 43
 | 
			
		||||
          bar
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if the argument is unused" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports if the argument is shadowed before super" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar = 1
 | 
			
		||||
          super
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context "branch" do
 | 
			
		||||
      it "doesn't report if the argument is not shadowed in a condition" do
 | 
			
		||||
        s = Source.new %(
 | 
			
		||||
          def foo(bar, baz)
 | 
			
		||||
            bar = 1 if baz
 | 
			
		||||
            bar
 | 
			
		||||
          end
 | 
			
		||||
        )
 | 
			
		||||
        subject.catch(s).should be_valid
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "reports if the argument is shadowed after the condition" do
 | 
			
		||||
        s = Source.new %(
 | 
			
		||||
          def foo(foo)
 | 
			
		||||
            if something
 | 
			
		||||
              foo = 42
 | 
			
		||||
            end
 | 
			
		||||
            foo = 43
 | 
			
		||||
            foo
 | 
			
		||||
          end
 | 
			
		||||
        )
 | 
			
		||||
        subject.catch(s).should_not be_valid
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "doesn't report if the argument is conditionally assigned in a branch" do
 | 
			
		||||
        s = Source.new %(
 | 
			
		||||
          def foo(bar)
 | 
			
		||||
            if something
 | 
			
		||||
              bar ||= 22
 | 
			
		||||
            end
 | 
			
		||||
            bar
 | 
			
		||||
          end
 | 
			
		||||
        )
 | 
			
		||||
        subject.catch(s).should be_valid
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, location and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def foo(bar)
 | 
			
		||||
          bar = 22
 | 
			
		||||
          bar
 | 
			
		||||
        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:3:11"
 | 
			
		||||
      error.message.should eq "Argument `bar` is assigned before it is used"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue