From 04c7300964d9855b16b8d1cb48afcf1fb28a6403 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sat, 23 Jun 2018 08:47:18 +0300 Subject: [PATCH] Handle type declarations in scopes closes #66 --- .../rule/lint/shadowing_local_outer_var_spec.cr | 14 ++++++++++++++ spec/ameba/rule/lint/useless_assign_spec.cr | 11 +++++++++++ src/ameba/ast/visitors/scope_visitor.cr | 7 +++++++ 3 files changed, 32 insertions(+) diff --git a/spec/ameba/rule/lint/shadowing_local_outer_var_spec.cr b/spec/ameba/rule/lint/shadowing_local_outer_var_spec.cr index 5a19061d..ddd58142 100644 --- a/spec/ameba/rule/lint/shadowing_local_outer_var_spec.cr +++ b/spec/ameba/rule/lint/shadowing_local_outer_var_spec.cr @@ -124,6 +124,20 @@ module Ameba::Rule::Lint subject.catch(source).should be_valid end + it "doesn't report if it shadows record type declaration" do + source = Source.new %( + class FooBar + record Foo, index : String + + def bar + 3.times do |index| + end + end + end + ) + subject.catch(source).should be_valid + end + it "reports rule, location and message" do source = Source.new %( foo = 1 diff --git a/spec/ameba/rule/lint/useless_assign_spec.cr b/spec/ameba/rule/lint/useless_assign_spec.cr index 5c98993f..e6e1ec6a 100644 --- a/spec/ameba/rule/lint/useless_assign_spec.cr +++ b/spec/ameba/rule/lint/useless_assign_spec.cr @@ -408,6 +408,17 @@ module Ameba::Rule::Lint ) subject.catch(s).should be_valid end + + it "doesn't report if assignment initialized and captured by block" do + s = Source.new %( + a : String? = nil + + 1.times do + a = "Fotis" + end + ) + subject.catch(s).should be_valid + end end context "branching" do diff --git a/src/ameba/ast/visitors/scope_visitor.cr b/src/ameba/ast/visitors/scope_visitor.cr index aa8014d0..075a9219 100644 --- a/src/ameba/ast/visitors/scope_visitor.cr +++ b/src/ameba/ast/visitors/scope_visitor.cr @@ -110,6 +110,13 @@ module Ameba::AST on_scope_end(node) if @current_scope.eql?(node) end + # :nodoc: + def visit(node : Crystal::TypeDeclaration) + if !@current_scope.type_definition? && (var = node.var).is_a?(Crystal::Var) + @current_scope.add_variable var + end + end + # :nodoc: def visit(node : Crystal::Arg) @current_scope.add_argument node