mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Incorrectly reporting shadowingOuterLocalVar within macro included
closes #129
This commit is contained in:
		
							parent
							
								
									43b566a852
								
							
						
					
					
						commit
						aff723b682
					
				
					 5 changed files with 45 additions and 2 deletions
				
			
		| 
						 | 
					@ -84,4 +84,20 @@ module Ameba::AST
 | 
				
			||||||
      scope.block?.should be_false
 | 
					      scope.block?.should be_false
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  describe "#macro?" do
 | 
				
			||||||
 | 
					    it "returns true if Crystal::Macro" do
 | 
				
			||||||
 | 
					      nodes = as_nodes %(
 | 
				
			||||||
 | 
					        macro included
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					      scope = Scope.new nodes.macro_nodes.first
 | 
				
			||||||
 | 
					      scope.macro?.should be_true
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "returns false otherwise" do
 | 
				
			||||||
 | 
					      scope = Scope.new as_node "a = 1"
 | 
				
			||||||
 | 
					      scope.macro?.should be_false
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -174,5 +174,24 @@ module Ameba::Rule::Lint
 | 
				
			||||||
      issue.end_location.should be_nil
 | 
					      issue.end_location.should be_nil
 | 
				
			||||||
      issue.message.should eq "Shadowing outer local variable `foo`"
 | 
					      issue.message.should eq "Shadowing outer local variable `foo`"
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context "macro" do
 | 
				
			||||||
 | 
					      it "does not report shadowed vars A" do
 | 
				
			||||||
 | 
					        source = Source.new %(
 | 
				
			||||||
 | 
					          macro included
 | 
				
			||||||
 | 
					            def foo
 | 
				
			||||||
 | 
					              {% for ivar in instance_vars %}
 | 
				
			||||||
 | 
					                {% ann = ivar.annotation(Name) %}
 | 
				
			||||||
 | 
					              {% end %}
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            def bar
 | 
				
			||||||
 | 
					              {% instance_vars.reject { |ivar| ivar } %}
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        subject.catch(source).should be_valid
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -139,6 +139,7 @@ module Ameba
 | 
				
			||||||
      Crystal::OpAssign,
 | 
					      Crystal::OpAssign,
 | 
				
			||||||
      Crystal::MultiAssign,
 | 
					      Crystal::MultiAssign,
 | 
				
			||||||
      Crystal::Block,
 | 
					      Crystal::Block,
 | 
				
			||||||
 | 
					      Crystal::Macro,
 | 
				
			||||||
      Crystal::Def,
 | 
					      Crystal::Def,
 | 
				
			||||||
      Crystal::If,
 | 
					      Crystal::If,
 | 
				
			||||||
      Crystal::While,
 | 
					      Crystal::While,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -94,6 +94,11 @@ module Ameba::AST
 | 
				
			||||||
      node.is_a?(Crystal::Block) || node.is_a?(Crystal::ProcLiteral)
 | 
					      node.is_a?(Crystal::Block) || node.is_a?(Crystal::ProcLiteral)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Returns true if currency scope represents a macro.
 | 
				
			||||||
 | 
					    def macro?
 | 
				
			||||||
 | 
					      node.is_a?(Crystal::Macro)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Returns true instance variable assinged in this scope.
 | 
					    # Returns true instance variable assinged in this scope.
 | 
				
			||||||
    def assigns_ivar?(name)
 | 
					    def assigns_ivar?(name)
 | 
				
			||||||
      arguments.find { |arg| arg.name == name } &&
 | 
					      arguments.find { |arg| arg.name == name } &&
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,9 +53,11 @@ module Ameba::Rule::Lint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private def find_shadowing(source, scope)
 | 
					    private def find_shadowing(source, scope)
 | 
				
			||||||
      scope.arguments.each do |arg|
 | 
					      scope.arguments.each do |arg|
 | 
				
			||||||
        next if arg.ignored?
 | 
					 | 
				
			||||||
        outer_scope = scope.outer_scope
 | 
					        outer_scope = scope.outer_scope
 | 
				
			||||||
        if outer_scope && outer_scope.find_variable(arg.name) && !outer_scope.assigns_ivar?(arg.name)
 | 
					
 | 
				
			||||||
 | 
					        next if arg.ignored? || outer_scope.nil?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if !outer_scope.macro? && outer_scope.find_variable(arg.name) && !outer_scope.assigns_ivar?(arg.name)
 | 
				
			||||||
          issue_for arg.node, MSG % arg.name
 | 
					          issue_for arg.node, MSG % arg.name
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue