Style improvements

This commit is contained in:
Vitalii Elenhaupt 2017-11-01 19:30:08 +02:00
parent 628077066e
commit 996dc962db
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
8 changed files with 10 additions and 7 deletions

View file

@ -11,6 +11,7 @@ module Ameba::Rules
# This is because these expressions evaluate to `true` or `false`, so you # This is because these expressions evaluate to `true` or `false`, so you
# could get the same result by using either the variable directly, # could get the same result by using either the variable directly,
# or negating the variable. # or negating the variable.
#
struct ComparisonToBoolean < Rule struct ComparisonToBoolean < Rule
def test(source) def test(source)
AST::CallVisitor.new self, source AST::CallVisitor.new self, source

View file

@ -3,6 +3,7 @@ module Ameba::Rules
# #
# This is because we don't want debugger breakpoints accidentally being # This is because we don't want debugger breakpoints accidentally being
# committed into our codebase. # committed into our codebase.
#
struct DebuggerStatement < Rule struct DebuggerStatement < Rule
def test(source) def test(source)
AST::CallVisitor.new self, source AST::CallVisitor.new self, source

View file

@ -1,5 +1,6 @@
module Ameba::Rules module Ameba::Rules
# A rule that disallows lines longer than 79 symbols. # A rule that disallows lines longer than 79 symbols.
#
struct LineLength < Rule struct LineLength < Rule
def test(source) def test(source)
source.lines.each_with_index do |line, index| source.lines.each_with_index do |line, index|

View file

@ -12,6 +12,7 @@ module Ameba::Rules
# :ok # :ok
# end # end
# ``` # ```
#
struct LiteralInCondition < Rule struct LiteralInCondition < Rule
include AST::Util include AST::Util

View file

@ -6,7 +6,7 @@ module Ameba::Rules
# #
# ``` # ```
# "Hello, #{:Ary}" # "Hello, #{:Ary}"
# "The are #{4} cats" # "There are #{4} cats"
# ``` # ```
# #
struct LiteralInInterpolation < Rule struct LiteralInInterpolation < Rule
@ -17,12 +17,8 @@ module Ameba::Rules
end end
def test(source, node : Crystal::StringInterpolation) def test(source, node : Crystal::StringInterpolation)
has_literal = node.expressions.any? do |e| found = node.expressions.any? { |e| !string_literal?(e) && literal?(e) }
!string_literal?(e) && literal?(e) return unless found
end
return unless has_literal
source.error self, node.location.try &.line_number, source.error self, node.location.try &.line_number,
"Literal value found in interpolation" "Literal value found in interpolation"
end end

View file

@ -1,5 +1,6 @@
module Ameba::Rules module Ameba::Rules
# A rule that disallows trailing blank lines at the end of the source file. # A rule that disallows trailing blank lines at the end of the source file.
#
struct TrailingBlankLines < Rule struct TrailingBlankLines < Rule
def test(source) def test(source)
if source.lines.size > 1 && source.lines[-2, 2].join.strip.empty? if source.lines.size > 1 && source.lines[-2, 2].join.strip.empty?

View file

@ -1,5 +1,6 @@
module Ameba::Rules module Ameba::Rules
# A rule that disallows trailing whitespaces. # A rule that disallows trailing whitespaces.
#
struct TrailingWhitespace < Rule struct TrailingWhitespace < Rule
def test(source) def test(source)
source.lines.each_with_index do |line, index| source.lines.each_with_index do |line, index|

View file

@ -35,6 +35,7 @@ module Ameba::Rules
# :one # :one
# end # end
# ``` # ```
#
struct UnlessElse < Rule struct UnlessElse < Rule
def test(source) def test(source)
AST::UnlessVisitor.new self, source AST::UnlessVisitor.new self, source