2017-11-07 21:50:25 +00:00
|
|
|
module Ameba::Rule
|
2017-11-07 18:44:38 +00:00
|
|
|
# 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
|
|
|
|
# ```
|
|
|
|
#
|
2017-11-07 21:50:25 +00:00
|
|
|
struct EmptyExpression < Base
|
2017-11-07 18:44:38 +00:00
|
|
|
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"
|
|
|
|
|
2017-11-07 20:02:51 +00:00
|
|
|
source.error self, node.location, "Avoid empty expression '#{exp}'"
|
2017-11-07 18:44:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|