mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Correctly process record declaration at a top level (#78)
This commit is contained in:
		
							parent
							
								
									18ac04d992
								
							
						
					
					
						commit
						cb5f802012
					
				
					 3 changed files with 52 additions and 5 deletions
				
			
		| 
						 | 
					@ -419,6 +419,35 @@ module Ameba::Rule::Lint
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        subject.catch(s).should be_valid
 | 
					        subject.catch(s).should be_valid
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "doesn't report if this is a record declaration" do
 | 
				
			||||||
 | 
					        s = Source.new %(
 | 
				
			||||||
 | 
					          record Foo, foo = "foo"
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        subject.catch(s).should be_valid
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "does not report if assignment is referenced after the record declaration" do
 | 
				
			||||||
 | 
					        s = Source.new %(
 | 
				
			||||||
 | 
					          foo = 2
 | 
				
			||||||
 | 
					          record Bar, foo = 3 # foo = 3 is not parsed as assignment
 | 
				
			||||||
 | 
					          puts foo
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        subject.catch(s).should be_valid
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "reports if assignment is not referenced after the record declaration" do
 | 
				
			||||||
 | 
					        s = Source.new %(
 | 
				
			||||||
 | 
					          foo = 2
 | 
				
			||||||
 | 
					          record Bar, foo = 3
 | 
				
			||||||
 | 
					        ), "source.cr"
 | 
				
			||||||
 | 
					        subject.catch(s).should_not be_valid
 | 
				
			||||||
 | 
					        s.issues.size.should eq 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        issue = s.issues.first
 | 
				
			||||||
 | 
					        issue.location.to_s.should eq "source.cr:2:11"
 | 
				
			||||||
 | 
					        issue.message.should eq "Useless assignment to variable `foo`"
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context "branching" do
 | 
					    context "branching" do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -98,6 +98,11 @@ module Ameba::AST
 | 
				
			||||||
      node.is_a? Crystal::Def
 | 
					      node.is_a? Crystal::Def
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Returns true if this scope is a top level scope, false if not.
 | 
				
			||||||
 | 
					    def top_level?
 | 
				
			||||||
 | 
					      outer_scope.nil?
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Returns true if var is an argument in current scope, false if not.
 | 
					    # Returns true if var is an argument in current scope, false if not.
 | 
				
			||||||
    def arg?(var)
 | 
					    def arg?(var)
 | 
				
			||||||
      case current_node = node
 | 
					      case current_node = node
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,6 +3,9 @@ require "./base_visitor"
 | 
				
			||||||
module Ameba::AST
 | 
					module Ameba::AST
 | 
				
			||||||
  # AST Visitor that traverses the source and constructs scopes.
 | 
					  # AST Visitor that traverses the source and constructs scopes.
 | 
				
			||||||
  class ScopeVisitor < BaseVisitor
 | 
					  class ScopeVisitor < BaseVisitor
 | 
				
			||||||
 | 
					    SUPER_NODE_NAME  = "super"
 | 
				
			||||||
 | 
					    RECORD_NODE_NAME = "record"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @current_scope : Scope
 | 
					    @current_scope : Scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def initialize(@rule, @source)
 | 
					    def initialize(@rule, @source)
 | 
				
			||||||
| 
						 | 
					@ -140,13 +143,23 @@ module Ameba::AST
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # :nodoc:
 | 
					    # :nodoc:
 | 
				
			||||||
    def visit(node : Crystal::Call)
 | 
					    def visit(node : Crystal::Call)
 | 
				
			||||||
      return true unless node.name == "super" && node.args.empty?
 | 
					      case @current_scope
 | 
				
			||||||
      return true unless (scope = @current_scope).def?
 | 
					      when .def?
 | 
				
			||||||
      scope.arguments.each do |arg|
 | 
					        if node.name == SUPER_NODE_NAME && node.args.empty?
 | 
				
			||||||
 | 
					          @current_scope.arguments.each do |arg|
 | 
				
			||||||
            variable = arg.variable
 | 
					            variable = arg.variable
 | 
				
			||||||
        variable.reference(variable.node, scope).explicit = false
 | 
					            variable.reference(variable.node, @current_scope).explicit = false
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      when .top_level?
 | 
				
			||||||
 | 
					        return false if record_macro?(node)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      true
 | 
					      true
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private def record_macro?(node)
 | 
				
			||||||
 | 
					      node.name == RECORD_NODE_NAME && node.args.first?.is_a?(Crystal::Path)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue