mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: empty assignment
This commit is contained in:
parent
a9421ee79b
commit
9036a7ca71
3 changed files with 126 additions and 0 deletions
85
spec/ameba/rules/empty_expression_spec.cr
Normal file
85
spec/ameba/rules/empty_expression_spec.cr
Normal file
|
@ -0,0 +1,85 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
module Ameba
|
||||
subject = Rules::EmptyExpression.new
|
||||
|
||||
def it_detects_empty_expression(code)
|
||||
it "detects empty expression" do
|
||||
s = Source.new code
|
||||
rule = Rules::EmptyExpression.new
|
||||
rule.catch(s).should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe Rules::EmptyExpression do
|
||||
it "passes if there is no empty expression" do
|
||||
s = Source.new %(
|
||||
def method()
|
||||
end
|
||||
|
||||
method()
|
||||
method(1, 2, 3)
|
||||
method(nil)
|
||||
|
||||
a = nil
|
||||
a = ""
|
||||
a = 0
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it_detects_empty_expression %(())
|
||||
it_detects_empty_expression %(((())))
|
||||
it_detects_empty_expression %(a = ())
|
||||
it_detects_empty_expression %((();()))
|
||||
it_detects_empty_expression %(if (); end)
|
||||
it_detects_empty_expression %(
|
||||
if foo
|
||||
1
|
||||
elsif ()
|
||||
2
|
||||
end
|
||||
)
|
||||
it_detects_empty_expression %(
|
||||
case foo
|
||||
when :foo then ()
|
||||
end
|
||||
)
|
||||
it_detects_empty_expression %(
|
||||
case foo
|
||||
when :foo then 1
|
||||
else
|
||||
()
|
||||
end
|
||||
)
|
||||
it_detects_empty_expression %(
|
||||
case foo
|
||||
when () then 1
|
||||
end
|
||||
)
|
||||
it_detects_empty_expression %(
|
||||
def method
|
||||
a = 1
|
||||
()
|
||||
end
|
||||
)
|
||||
it_detects_empty_expression %(
|
||||
def method
|
||||
rescue
|
||||
()
|
||||
end
|
||||
)
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
if ()
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.message.should eq "Avoid empty expression '()'"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,6 +14,7 @@ module Ameba::AST
|
|||
InstanceVar,
|
||||
LibDef,
|
||||
ModuleDef,
|
||||
NilLiteral,
|
||||
StringInterpolation,
|
||||
Unless,
|
||||
Var,
|
||||
|
|
40
src/ameba/rules/empty_expression.cr
Normal file
40
src/ameba/rules/empty_expression.cr
Normal file
|
@ -0,0 +1,40 @@
|
|||
module Ameba::Rules
|
||||
# A rule that disallows empty expressions.
|
||||
#
|
||||
# This is considered invalid:
|
||||
#
|
||||
# ```
|
||||
# foo = ()
|
||||
#
|
||||
# if ()
|
||||
# bar
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
# And this is valid:
|
||||
#
|
||||
# ```
|
||||
# foo = (some_expression)
|
||||
#
|
||||
# if (some_expression)
|
||||
# bar
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
struct EmptyExpression < Rule
|
||||
include AST::Util
|
||||
|
||||
def test(source)
|
||||
AST::Visitor.new self, source
|
||||
end
|
||||
|
||||
def test(source, node : Crystal::NilLiteral)
|
||||
exp = node_source(node, source.lines).try &.join
|
||||
|
||||
return if exp.nil? || exp == "nil"
|
||||
|
||||
source.error self, node.location.try &.line_number,
|
||||
"Avoid empty expression '#{exp}'"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue