mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	
							parent
							
								
									bd68e8c3b3
								
							
						
					
					
						commit
						ddbcf5cb3f
					
				
					 3 changed files with 37 additions and 3 deletions
				
			
		| 
						 | 
					@ -475,6 +475,32 @@ module Ameba::Rule::Lint
 | 
				
			||||||
        issue.location.to_s.should eq "source.cr:1:1"
 | 
					        issue.location.to_s.should eq "source.cr:1:1"
 | 
				
			||||||
        issue.message.should eq "Useless assignment to variable `foo`"
 | 
					        issue.message.should eq "Useless assignment to variable `foo`"
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "doesn't report if top level variable defined inside class is referenced" do
 | 
				
			||||||
 | 
					        s = Source.new %(
 | 
				
			||||||
 | 
					          class A
 | 
				
			||||||
 | 
					            foo : String? = "foo"
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          puts foo
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        subject.catch(s).should be_valid
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "doesn't report if top level variable assigned inside class and referenced" do
 | 
				
			||||||
 | 
					        s = Source.new %(
 | 
				
			||||||
 | 
					          class A
 | 
				
			||||||
 | 
					            foo : String? = "foo"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            bar do
 | 
				
			||||||
 | 
					              foo = "bar"
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          puts foo
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        subject.catch(s).should be_valid
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context "branching" do
 | 
					    context "branching" do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -161,7 +161,7 @@ module Ameba::AST
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Returns `true` if this scope is a top level scope, `false` otherwise.
 | 
					    # Returns `true` if this scope is a top level scope, `false` otherwise.
 | 
				
			||||||
    def top_level?
 | 
					    def top_level?
 | 
				
			||||||
      outer_scope.nil?
 | 
					      outer_scope.nil? || type_definition?
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Returns `true` if var is an argument in current scope, `false` otherwise.
 | 
					    # Returns `true` if var is an argument in current scope, `false` otherwise.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -102,10 +102,18 @@ module Ameba::AST
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # :nodoc:
 | 
					    # :nodoc:
 | 
				
			||||||
    def visit(node : Crystal::TypeDeclaration)
 | 
					    def visit(node : Crystal::TypeDeclaration)
 | 
				
			||||||
      return if @current_scope.type_definition?
 | 
					      return unless (var = node.var).is_a?(Crystal::Var)
 | 
				
			||||||
      return if !(var = node.var).is_a?(Crystal::Var)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @current_scope.add_variable(var)
 | 
					      @current_scope.add_variable(var)
 | 
				
			||||||
 | 
					      @current_assign = node.value unless node.value.nil?
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def end_visit(node : Crystal::TypeDeclaration)
 | 
				
			||||||
 | 
					      return unless (var = node.var).is_a?(Crystal::Var)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      on_assign_end(node.var, node)
 | 
				
			||||||
 | 
					      @current_assign = nil
 | 
				
			||||||
 | 
					      on_scope_end(node) if @current_scope.eql?(node)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # :nodoc:
 | 
					    # :nodoc:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue