mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge branch 'master' into fix/crystal-nightly
This commit is contained in:
commit
e2faffacfe
23 changed files with 352 additions and 518 deletions
|
@ -6,163 +6,115 @@ module Ameba::Rule::Lint
|
|||
|
||||
context "when using `-`" do
|
||||
it "registers an offense with `x`" do
|
||||
source = Source.new("x =- y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
x =- y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@x`" do
|
||||
source = Source.new("@x =- y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:4"
|
||||
issue.end_location.to_s.should eq "source.cr:1:5"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@x =- y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@@x`" do
|
||||
source = Source.new("@@x =- y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:5"
|
||||
issue.end_location.to_s.should eq "source.cr:1:6"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@@x =- y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `X`" do
|
||||
source = Source.new("X =- y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
X =- y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
x = 1
|
||||
x -= y
|
||||
x = -y
|
||||
CRYSTAL
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
|
||||
context "when using `+`" do
|
||||
it "registers an offense with `x`" do
|
||||
source = Source.new("x =+ y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
x =+ y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@x`" do
|
||||
source = Source.new("@x =+ y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:4"
|
||||
issue.end_location.to_s.should eq "source.cr:1:5"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@x =+ y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@@x`" do
|
||||
source = Source.new("@@x =+ y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:5"
|
||||
issue.end_location.to_s.should eq "source.cr:1:6"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@@x =+ y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `X`" do
|
||||
source = Source.new("X =+ y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
X =+ y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
x = 1
|
||||
x += y
|
||||
x = +y
|
||||
CRYSTAL
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
|
||||
context "when using `!`" do
|
||||
it "registers an offense with `x`" do
|
||||
source = Source.new("x =! y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
x =! y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@x`" do
|
||||
source = Source.new("@x =! y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:4"
|
||||
issue.end_location.to_s.should eq "source.cr:1:5"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@x =! y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `@@x`" do
|
||||
source = Source.new("@@x =! y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:5"
|
||||
issue.end_location.to_s.should eq "source.cr:1:6"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
@@x =! y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "registers an offense with `X`" do
|
||||
source = Source.new("X =! y", "source.cr")
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq 1
|
||||
|
||||
issue = source.issues.first
|
||||
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
|
||||
issue.location.to_s.should eq "source.cr:1:3"
|
||||
issue.end_location.to_s.should eq "source.cr:1:4"
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
X =! y
|
||||
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
x = false
|
||||
x != y
|
||||
x = !y
|
||||
CRYSTAL
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,62 +5,42 @@ module Ameba::Rule::Lint
|
|||
subject = BadDirective.new
|
||||
|
||||
it "does not report if rule is correct" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
# ameba:disable Lint/BadDirective
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is incorrect action" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
# ameba:foo Lint/BadDirective
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 1
|
||||
|
||||
issue = s.issues.first
|
||||
issue.message.should eq(
|
||||
"Bad action in comment directive: 'foo'. Possible values: disable, enable"
|
||||
)
|
||||
issue.location.to_s.should eq "source.cr:1:1"
|
||||
issue.end_location.to_s.should eq ""
|
||||
# ^{} error: Bad action in comment directive: 'foo'. Possible values: disable, enable
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there are incorrect rule names" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
# ameba:enable BadRule1, BadRule2
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 1
|
||||
|
||||
issue = s.issues.first
|
||||
issue.message.should eq(
|
||||
"Such rules do not exist: BadRule1, BadRule2"
|
||||
)
|
||||
issue.location.to_s.should eq "source.cr:1:1"
|
||||
issue.end_location.to_s.should eq ""
|
||||
# ^{} error: Such rules do not exist: BadRule1, BadRule2
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if there no action and rules at all" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
# ameba:
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if there are no rules" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
# ameba:enable
|
||||
# ameba:disable
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if there are group names in the directive" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
# ameba:disable Style Performance
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ module Ameba::Rule::Lint
|
|||
subject = EmptyLoop.new
|
||||
|
||||
it "does not report if there are not empty loops" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
a = 1
|
||||
|
||||
while a < 10
|
||||
|
@ -19,54 +19,50 @@ module Ameba::Rule::Lint
|
|||
loop do
|
||||
a += 1
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is an empty while loop" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
a = 1
|
||||
while true
|
||||
# ^^^^^^^^ error: Empty loop detected
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if while loop has non-literals in cond block" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
a = 1
|
||||
while a = gets.to_s
|
||||
# nothing here
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is an empty until loop" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
do_something
|
||||
until false
|
||||
# ^^^^^^^^^ error: Empty loop detected
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if until loop has non-literals in cond block" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
until socket_open?
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there an empty loop" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
a = 1
|
||||
loop do
|
||||
|
||||
# ^^^^^ error: Empty loop detected
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, message and location" do
|
||||
|
|
|
@ -5,26 +5,32 @@ module Ameba::Rule::Lint
|
|||
subject = HashDuplicatedKey.new
|
||||
|
||||
it "passes if there is no duplicated keys in a hash literals" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
h = {"a" => 1, :a => 2, "b" => 3}
|
||||
h = {"a" => 1, "b" => 2, "c" => {"a" => 3, "b" => 4}}
|
||||
h = {} of String => String
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is a duplicated key in a hash literal" do
|
||||
s = Source.new %q(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
h = {"a" => 1, "b" => 2, "a" => 3}
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is a duplicated key in the inner hash literal" do
|
||||
s = Source.new %q(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
h = {"a" => 1, "b" => {"a" => 3, "b" => 4, "a" => 5}}
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports multiple duplicated keys" do
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
|
||||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "key1", "key2"
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, location and message" do
|
||||
|
@ -38,14 +44,5 @@ module Ameba::Rule::Lint
|
|||
issue.end_location.to_s.should eq "source.cr:1:24"
|
||||
issue.message.should eq %(Duplicated keys in hash literal: "a")
|
||||
end
|
||||
|
||||
it "reports multiple duplicated keys" do
|
||||
s = Source.new %q(
|
||||
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
issue = s.issues.first
|
||||
issue.message.should eq %(Duplicated keys in hash literal: "key1", "key2")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,15 +5,14 @@ module Ameba::Rule::Lint
|
|||
|
||||
describe LiteralInInterpolation do
|
||||
it "passes with good interpolation examples" do
|
||||
s = Source.new %q(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
name = "Ary"
|
||||
"Hello, #{name}"
|
||||
|
||||
"#{name}"
|
||||
|
||||
"Name size: #{name.size}"
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is useless interpolation" do
|
||||
|
|
|
@ -5,22 +5,25 @@ module Ameba::Rule::Lint
|
|||
subject = RandZero.new
|
||||
|
||||
it "passes if it is not rand(1) or rand(0)" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
rand(1.0)
|
||||
rand(0.11)
|
||||
rand(2)
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if it is rand(0)" do
|
||||
s = Source.new "rand(0)"
|
||||
subject.catch(s).should_not be_valid
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
rand(0)
|
||||
# ^^^^^ error: rand(0) always returns 0
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if it is rand(1)" do
|
||||
s = Source.new "rand(1)"
|
||||
subject.catch(s).should_not be_valid
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
rand(1)
|
||||
# ^^^^^ error: rand(1) always returns 0
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, location and a message" do
|
||||
|
|
|
@ -5,7 +5,7 @@ module Ameba::Rule::Lint
|
|||
subject = ShadowingOuterLocalVar.new
|
||||
|
||||
it "doesn't report if there is no shadowing" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
def some_method
|
||||
foo = 1
|
||||
|
||||
|
@ -14,126 +14,117 @@ module Ameba::Rule::Lint
|
|||
end
|
||||
|
||||
-> (baz : Int32) {}
|
||||
|
||||
-> (bar : String) {}
|
||||
end
|
||||
)
|
||||
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is a shadowing in a block" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
def some_method
|
||||
foo = 1
|
||||
|
||||
3.times do |foo|
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report outer vars declared below shadowed block" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
methods = klass.methods.select { |m| m.annotation(MyAnn) }
|
||||
m = methods.last
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is a shadowing in a proc" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
def some_method
|
||||
foo = 1
|
||||
|
||||
-> (foo : Int32) {}
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is a shadowing in an inner scope" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
def foo
|
||||
foo = 1
|
||||
|
||||
3.times do |i|
|
||||
3.times { |foo| foo }
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if variable is shadowed twice" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
foo = 1
|
||||
|
||||
3.times do |foo|
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
-> (foo : Int32) { foo + 1 }
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
source.issues.size.should eq 2
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if a splat block argument shadows local var" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
foo = 1
|
||||
|
||||
3.times do |*foo|
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if a &block argument is shadowed" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
def method_with_block(a, &block)
|
||||
3.times do |block|
|
||||
# ^ error: Shadowing outer local variable `block`
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.first.message.should eq "Shadowing outer local variable `block`"
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there are multiple args and one shadows local var" do
|
||||
source = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
foo = 1
|
||||
[1, 2, 3].each_with_index do |i, foo|
|
||||
# ^ error: Shadowing outer local variable `foo`
|
||||
i + foo
|
||||
end
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.first.message.should eq "Shadowing outer local variable `foo`"
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if an outer var is reassigned in a block" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
def foo
|
||||
foo = 1
|
||||
3.times do |i|
|
||||
foo = 2
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if an argument is a black hole '_'" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
_ = 1
|
||||
3.times do |_|
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if it shadows record type declaration" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
class FooBar
|
||||
record Foo, index : String
|
||||
|
||||
|
@ -142,12 +133,11 @@ module Ameba::Rule::Lint
|
|||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if it shadows throwaway arguments" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
data = [{1, "a"}, {2, "b"}, {3, "c"}]
|
||||
|
||||
data.each do |_, string|
|
||||
|
@ -155,18 +145,16 @@ module Ameba::Rule::Lint
|
|||
puts string, number
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if argument shadows an ivar assignment" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
def bar(@foo)
|
||||
@foo.try do |foo|
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, location and message" do
|
||||
|
@ -185,7 +173,7 @@ module Ameba::Rule::Lint
|
|||
|
||||
context "macro" do
|
||||
it "does not report shadowed vars in outer scope" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
macro included
|
||||
def foo
|
||||
{% for ivar in instance_vars %}
|
||||
|
@ -197,12 +185,11 @@ module Ameba::Rule::Lint
|
|||
{% instance_vars.reject { |ivar| ivar } %}
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report shadowed vars in macro within the same scope" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
{% methods = klass.methods.select { |m| m.annotation(MyAnn) } %}
|
||||
|
||||
{% for m, m_idx in methods %}
|
||||
|
@ -210,12 +197,11 @@ module Ameba::Rule::Lint
|
|||
{% d %}
|
||||
{% end %}
|
||||
{% end %}
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report shadowed vars within nested macro" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
module Foo
|
||||
macro included
|
||||
def foo
|
||||
|
@ -233,12 +219,11 @@ module Ameba::Rule::Lint
|
|||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report scoped vars to MacroFor" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
struct Test
|
||||
def test
|
||||
{% for ivar in @type.instance_vars %}
|
||||
|
@ -248,8 +233,24 @@ module Ameba::Rule::Lint
|
|||
{% ["a", "b"].map { |ivar| puts ivar } %}
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
# https://github.com/crystal-ameba/ameba/issues/224#issuecomment-822245167
|
||||
it "does not report scoped vars to MacroFor (2)" do
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
struct Test
|
||||
def test
|
||||
{% begin %}
|
||||
{% for ivar in @type.instance_vars %}
|
||||
{% var_type = ivar %}
|
||||
{% end %}
|
||||
|
||||
{% ["a", "b"].map { |ivar| puts ivar } %}
|
||||
{% end %}
|
||||
end
|
||||
end
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,23 +5,22 @@ module Ameba::Rule::Lint
|
|||
subject = Syntax.new
|
||||
|
||||
it "passes if there is no invalid syntax" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
def hello
|
||||
puts "totally valid"
|
||||
rescue e: Exception
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is an invalid syntax" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
def hello
|
||||
puts "invalid"
|
||||
rescue Exception => e
|
||||
# ^ error: expecting any of these tokens: ;, NEWLINE (not '=>')
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, location and message" do
|
||||
|
|
|
@ -5,17 +5,15 @@ module Ameba::Rule::Lint
|
|||
subject = UnneededDisableDirective.new
|
||||
|
||||
it "passes if there are no comments" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
a = 1
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "passes if there is disable directive" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
a = 1 # my super var
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "doesn't report if there is disable directive and it is needed" do
|
||||
|
@ -48,33 +46,27 @@ module Ameba::Rule::Lint
|
|||
end
|
||||
|
||||
it "fails if there is unneeded directive" do
|
||||
s = Source.new %Q(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
# ameba:disable #{NamedRule.name}
|
||||
# ^{} error: Unnecessary disabling of #{NamedRule.name}
|
||||
a = 1
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.first.message.should eq(
|
||||
"Unnecessary disabling of #{NamedRule.name}"
|
||||
)
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is inline unneeded directive" do
|
||||
s = Source.new %Q(a = 1 # ameba:disable #{NamedRule.name})
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.first.message.should eq(
|
||||
"Unnecessary disabling of #{NamedRule.name}"
|
||||
)
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
a = 1 # ameba:disable #{NamedRule.name}
|
||||
# ^ error: Unnecessary disabling of #{NamedRule.name}
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "detects mixed inline directives" do
|
||||
s = Source.new %Q(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
# ameba:disable Rule1, Rule2
|
||||
# ^{} error: Unnecessary disabling of Rule1, Rule2
|
||||
a = 1 # ameba:disable Rule3
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 2
|
||||
s.issues.first.message.should contain "Rule1, Rule2"
|
||||
s.issues.last.message.should contain "Rule3"
|
||||
# ^ error: Unnecessary disabling of Rule3
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is disabled UnneededDisableDirective" do
|
||||
|
|
|
@ -5,25 +5,24 @@ module Ameba::Rule::Lint
|
|||
|
||||
describe UselessConditionInWhen do
|
||||
it "passes if there is not useless condition" do
|
||||
s = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
case
|
||||
when utc?
|
||||
io << " UTC"
|
||||
when local?
|
||||
Format.new(" %:z").format(self, io) if utc?
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is useless if condition" do
|
||||
s = Source.new %(
|
||||
expect_issue subject, <<-CRYSTAL
|
||||
case
|
||||
when utc?
|
||||
io << " UTC" if utc?
|
||||
# ^^^^ error: Useless condition in when detected
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports rule, location and message" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue