mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Properly report literals (#96)
This commit is contained in:
parent
43bb29d3fd
commit
3a71b86193
2 changed files with 139 additions and 11 deletions
|
@ -44,7 +44,26 @@ module Ameba::Rule::Lint
|
|||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "fails if there is a predicate in case conditional" do
|
||||
describe "range" do
|
||||
it "reports range with literals" do
|
||||
s = Source.new %(
|
||||
case 1..2
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report range with non-literals" do
|
||||
s = Source.new %(
|
||||
case (1..a)
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "array" do
|
||||
it "reports array with literals" do
|
||||
s = Source.new %(
|
||||
case [1, 2, 3]
|
||||
when :array
|
||||
|
@ -56,6 +75,93 @@ module Ameba::Rule::Lint
|
|||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report array with non-literals" do
|
||||
s = Source.new %(
|
||||
a, b = 1, 2
|
||||
case [1, 2, a]
|
||||
when :array
|
||||
:ok
|
||||
when :not_array
|
||||
:also_ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "hash" do
|
||||
it "reports hash with literals" do
|
||||
s = Source.new %(
|
||||
case { "name" => 1, 33 => 'b' }
|
||||
when :hash
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report hash with non-literals in keys" do
|
||||
s = Source.new %(
|
||||
case { a => 1, 33 => 'b' }
|
||||
when :hash
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't report hash with non-literals in values" do
|
||||
s = Source.new %(
|
||||
case { "name" => a, 33 => 'b' }
|
||||
when :hash
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "tuple" do
|
||||
it "reports tuple with literals" do
|
||||
s = Source.new %(
|
||||
case {1, false}
|
||||
when {1, _}
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report tuple with non-literals" do
|
||||
s = Source.new %(
|
||||
a, b = 1, 2
|
||||
case {1, b}
|
||||
when {1, 2}
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "named tuple" do
|
||||
it "reports named tuple with literals" do
|
||||
s = Source.new %(
|
||||
case { name: 1, foo: :bar}
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report named tuple with non-literals" do
|
||||
s = Source.new %(
|
||||
case { name: a, foo: :bar}
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
puts "hello" if true
|
||||
|
|
|
@ -2,7 +2,29 @@
|
|||
module Ameba::AST::Util
|
||||
# Returns true if current `node` is a literal, false otherwise.
|
||||
def literal?(node)
|
||||
node.try &.class.name.ends_with? "Literal"
|
||||
case node
|
||||
when Crystal::NilLiteral,
|
||||
Crystal::BoolLiteral,
|
||||
Crystal::NumberLiteral,
|
||||
Crystal::CharLiteral,
|
||||
Crystal::StringLiteral,
|
||||
Crystal::SymbolLiteral,
|
||||
Crystal::RegexLiteral,
|
||||
Crystal::ProcLiteral,
|
||||
Crystal::MacroLiteral
|
||||
true
|
||||
when Crystal::RangeLiteral
|
||||
literal?(node.from) && literal?(node.to)
|
||||
when Crystal::ArrayLiteral,
|
||||
Crystal::TupleLiteral
|
||||
node.elements.all? { |el| literal?(el) }
|
||||
when Crystal::HashLiteral
|
||||
node.entries.all? { |entry| literal?(entry.key) && literal?(entry.value) }
|
||||
when Crystal::NamedTupleLiteral
|
||||
node.entries.all? { |entry| literal?(entry.value) }
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if current `node` is a string literal, false otherwise.
|
||||
|
|
Loading…
Reference in a new issue