Shadowed argument

This commit is contained in:
Vitalii Elenhaupt 2018-05-28 19:42:01 +03:00 committed by V. Elenhaupt
parent 15bb8f5331
commit c12b4f1aa5
7 changed files with 287 additions and 12 deletions

View file

@ -43,14 +43,14 @@ module Ameba::AST
it "creates a new assignment" do
scope = Scope.new as_node("foo = 1")
scope.add_variable Crystal::Var.new "foo"
scope.assign_variable(Crystal::Var.new "foo")
scope.assign_variable("foo", Crystal::Var.new "foo")
scope.find_variable("foo").not_nil!.assignments.size.should eq 1
end
it "does not create the assignment if variable is wrong" do
scope = Scope.new as_node("foo = 1")
scope.add_variable Crystal::Var.new "foo"
scope.assign_variable(Crystal::Var.new "bar")
scope.assign_variable("bar", Crystal::Var.new "bar")
scope.find_variable("foo").not_nil!.assignments.size.should eq 0
end
end

View file

@ -0,0 +1,165 @@
require "../../spec_helper"
module Ameba::Rule
describe ShadowedArgument do
subject = ShadowedArgument.new
it "doesn't report if there is not a shadowed argument" do
s = Source.new %(
def foo(bar)
baz = 1
end
3.times do |i|
a = 1
end
proc = -> (a : Int32) {
b = 2
}
)
subject.catch(s).should be_valid
end
it "reports if there is a shadowed method argument" do
s = Source.new %(
def foo(bar)
bar = 1
bar
end
)
subject.catch(s).should_not be_valid
end
it "reports if there is a shadowed block argument" do
s = Source.new %(
3.times do |i|
i = 2
end
)
subject.catch(s).should_not be_valid
end
it "reports if there is a shadowed proc argument" do
s = Source.new %(
->(x : Int32) {
x = 20
x
}
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is referenced before the assignment" do
s = Source.new %(
def foo(bar)
bar
bar = 1
end
)
subject.catch(s).should be_valid
end
it "doesn't report if the argument is conditionally reassigned" do
s = Source.new %(
def foo(bar = nil)
bar ||= true
bar
end
)
subject.catch(s).should be_valid
end
it "doesn't report if the op assign is followed by another assignment" do
s = Source.new %(
def foo(bar)
bar ||= 3
bar = 43
bar
end
)
subject.catch(s).should be_valid
end
it "reports if the shadowing assignment is followed by op assign" do
s = Source.new %(
def foo(bar)
bar = 42
bar ||= 43
bar
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is unused" do
s = Source.new %(
def foo(bar)
end
)
subject.catch(s).should be_valid
end
it "reports if the argument is shadowed before super" do
s = Source.new %(
def foo(bar)
bar = 1
super
end
)
subject.catch(s).should_not be_valid
end
context "branch" do
it "doesn't report if the argument is not shadowed in a condition" do
s = Source.new %(
def foo(bar, baz)
bar = 1 if baz
bar
end
)
subject.catch(s).should be_valid
end
it "reports if the argument is shadowed after the condition" do
s = Source.new %(
def foo(foo)
if something
foo = 42
end
foo = 43
foo
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if the argument is conditionally assigned in a branch" do
s = Source.new %(
def foo(bar)
if something
bar ||= 22
end
bar
end
)
subject.catch(s).should be_valid
end
end
it "reports rule, location and message" do
s = Source.new %(
def foo(bar)
bar = 22
bar
end
), "source.cr"
subject.catch(s).should_not be_valid
error = s.errors.first
error.rule.should_not be_nil
error.location.to_s.should eq "source.cr:3:11"
error.message.should eq "Argument `bar` is assigned before it is used"
end
end
end