Detect shadowing outer local vars

This commit is contained in:
Vitalii Elenhaupt 2018-05-28 11:48:07 +03:00 committed by V. Elenhaupt
parent 3887da1438
commit 15bb8f5331
6 changed files with 216 additions and 14 deletions

View file

@ -0,0 +1,140 @@
require "../../spec_helper"
module Ameba::Rule
describe ShadowingOuterLocalVar do
subject = ShadowingOuterLocalVar.new
it "doesn't report if there is no shadowing" do
source = Source.new %(
def some_method
foo = 1
3.times do |bar|
bar
end
-> (baz : Int32) {}
-> (bar : String) {}
end
)
subject.catch(source).should be_valid
end
it "reports if there is a shadowing in a block" do
source = Source.new %(
def some_method
foo = 1
3.times do |foo|
end
end
)
subject.catch(source).should_not be_valid
end
it "reports if there is a shadowing in a proc" do
source = Source.new %(
def some_method
foo = 1
-> (foo : Int32) {}
end
)
subject.catch(source).should_not be_valid
end
it "reports if there is a shadowing in an inner scope" do
source = Source.new %(
def foo
foo = 1
3.times do |i|
3.times { |foo| foo }
end
end
)
subject.catch(source).should_not be_valid
end
it "reports if variable is shadowed twice" do
source = Source.new %(
foo = 1
3.times do |foo|
-> (foo : Int32) { foo + 1 }
end
)
subject.catch(source).should_not be_valid
source.errors.size.should eq 2
end
it "reports if a splat block argument shadows local var" do
source = Source.new %(
foo = 1
3.times do |*foo|
end
)
subject.catch(source).should_not be_valid
end
it "reports if a &block argument is shadowed" do
source = Source.new %(
def method_with_block(a, &block)
3.times do |block|
end
end
)
subject.catch(source).should_not be_valid
source.errors.first.message.should eq "Shadowing outer local variable `block`"
end
it "reports if there are multiple args and one shadows local var" do
source = Source.new %(
foo = 1
[1, 2, 3].each_with_index do |i, foo|
i + foo
end
)
subject.catch(source).should_not be_valid
source.errors.first.message.should eq "Shadowing outer local variable `foo`"
end
it "doesn't report if an outer var is reassigned in a block" do
source = Source.new %(
def foo
foo = 1
3.times do |i|
foo = 2
end
end
)
subject.catch(source).should be_valid
end
it "doesn't report if an argument is a black hole '_'" do
source = Source.new %(
_ = 1
3.times do |_|
end
)
subject.catch(source).should be_valid
end
it "reports rule, location and message" do
source = Source.new %(
foo = 1
3.times { |foo| foo + 1 }
), "source.cr"
subject.catch(source).should_not be_valid
error = source.errors.first
error.rule.should_not be_nil
error.location.to_s.should eq "source.cr:3:20"
error.message.should eq "Shadowing outer local variable `foo`"
end
end
end