mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add Lint/AmbiguousAssignment rule (#244)
This commit is contained in:
parent
9a91e42bcc
commit
48b15b9bf8
6 changed files with 234 additions and 10 deletions
|
@ -44,7 +44,7 @@ module Ameba::AST
|
|||
)
|
||||
node = Crystal::Parser.new(s).parse
|
||||
source = subject.node_source node, s.split("\n")
|
||||
source.should eq ["a = 1"]
|
||||
source.should eq "a = 1"
|
||||
end
|
||||
|
||||
it "returns original source of multiline node" do
|
||||
|
@ -55,11 +55,11 @@ module Ameba::AST
|
|||
)
|
||||
node = Crystal::Parser.new(s).parse
|
||||
source = subject.node_source node, s.split("\n")
|
||||
source.should eq([
|
||||
"if ()",
|
||||
" :ok",
|
||||
" end",
|
||||
])
|
||||
source.should eq <<-CRYSTAL
|
||||
if ()
|
||||
:ok
|
||||
end
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report source of node which has incorrect location" do
|
||||
|
@ -81,7 +81,7 @@ module Ameba::AST
|
|||
if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("0.35.1")
|
||||
source.should be_nil
|
||||
else
|
||||
source.should eq %w(nil)
|
||||
source.should eq "nil"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
169
spec/ameba/rule/lint/ambiguous_assignment_spec.cr
Normal file
169
spec/ameba/rule/lint/ambiguous_assignment_spec.cr
Normal file
|
@ -0,0 +1,169 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Lint
|
||||
describe AmbiguousAssignment do
|
||||
subject = AmbiguousAssignment.new
|
||||
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
x = 1
|
||||
x -= y
|
||||
x = -y
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
x = 1
|
||||
x += y
|
||||
x = +y
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
end
|
||||
|
||||
it "does not register an offense when no mistype assignments" do
|
||||
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
|
||||
x = false
|
||||
x != y
|
||||
x = !y
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue