Correctly process record declaration at a top level (#78)

This commit is contained in:
V. Elenhaupt 2018-09-07 00:47:02 +03:00 committed by GitHub
parent 18ac04d992
commit cb5f802012
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 5 deletions

View file

@ -419,6 +419,35 @@ module Ameba::Rule::Lint
)
subject.catch(s).should be_valid
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
context "branching" do

View file

@ -98,6 +98,11 @@ module Ameba::AST
node.is_a? Crystal::Def
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.
def arg?(var)
case current_node = node

View file

@ -3,6 +3,9 @@ require "./base_visitor"
module Ameba::AST
# AST Visitor that traverses the source and constructs scopes.
class ScopeVisitor < BaseVisitor
SUPER_NODE_NAME = "super"
RECORD_NODE_NAME = "record"
@current_scope : Scope
def initialize(@rule, @source)
@ -140,13 +143,23 @@ module Ameba::AST
# :nodoc:
def visit(node : Crystal::Call)
return true unless node.name == "super" && node.args.empty?
return true unless (scope = @current_scope).def?
scope.arguments.each do |arg|
variable = arg.variable
variable.reference(variable.node, scope).explicit = false
case @current_scope
when .def?
if node.name == SUPER_NODE_NAME && node.args.empty?
@current_scope.arguments.each do |arg|
variable = arg.variable
variable.reference(variable.node, @current_scope).explicit = false
end
end
when .top_level?
return false if record_macro?(node)
end
true
end
private def record_macro?(node)
node.name == RECORD_NODE_NAME && node.args.first?.is_a?(Crystal::Path)
end
end
end