Support of Crystal::UnitializedVar

This commit is contained in:
Vitalii Elenhaupt 2018-05-12 18:24:56 +03:00
parent 415432713a
commit efe67212b0
No known key found for this signature in database
GPG Key ID: 7558EF3A4056C706
2 changed files with 38 additions and 1 deletions

View File

@ -852,5 +852,33 @@ module Ameba::Rule
subject.catch(s).should_not be_valid
end
end
context "uninitialized" do
it "reports if uninitialized assignment is not referenced at a top level" do
s = Source.new %(
a = uninitialized U
)
subject.catch(s).should_not be_valid
end
it "reports if uninitialized assignment is not referenced in a method" do
s = Source.new %(
def foo
a = uninitialized U
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if uninitialized assignment is referenced" do
s = Source.new %(
def foo
a = uninitialized U
a
end
)
subject.catch(s).should be_valid
end
end
end
end

View File

@ -81,7 +81,7 @@ module Ameba::AST
@current_assign : Crystal::ASTNode?
# :nodoc:
def visit(node : Crystal::Assign | Crystal::OpAssign | Crystal::MultiAssign)
def visit(node : Crystal::Assign | Crystal::OpAssign | Crystal::MultiAssign | Crystal::UninitializedVar)
@current_assign = node
end
@ -89,12 +89,21 @@ module Ameba::AST
def end_visit(node : Crystal::Assign | Crystal::OpAssign)
@current_scope.assign_variable(node.target)
@current_assign = nil
on_scope_end(node) if @current_scope.eql?(node)
end
# :nodoc:
def end_visit(node : Crystal::MultiAssign)
node.targets.each { |target| @current_scope.assign_variable(target) }
@current_assign = nil
on_scope_end(node) if @current_scope.eql?(node)
end
# :nodoc:
def end_visit(node : Crystal::UninitializedVar)
@current_scope.assign_variable(node.var)
@current_assign = nil
on_scope_end(node) if @current_scope.eql?(node)
end
# :nodoc: