2021-01-24 19:23:32 +00:00
|
|
|
module Ameba::Rule::Style
|
|
|
|
# This rule is used to identify usage of `is_a?/nil?` calls within filters.
|
|
|
|
#
|
|
|
|
# For example, this is considered invalid:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# matches = %w[Alice Bob].map(&.match(/^A./))
|
|
|
|
#
|
|
|
|
# matches.any?(&.is_a?(Regex::MatchData)) # => true
|
|
|
|
# matches.one?(&.nil?) # => true
|
|
|
|
#
|
|
|
|
# typeof(matches.reject(&.nil?)) # => Array(Regex::MatchData | Nil)
|
|
|
|
# typeof(matches.select(&.is_a?(Regex::MatchData))) # => Array(Regex::MatchData | Nil)
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# And it should be written as this:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# matches = %w[Alice Bob].map(&.match(/^A./))
|
|
|
|
#
|
|
|
|
# matches.any?(Regex::MatchData) # => true
|
|
|
|
# matches.one?(Nil) # => true
|
|
|
|
#
|
|
|
|
# typeof(matches.reject(Nil)) # => Array(Regex::MatchData)
|
|
|
|
# typeof(matches.select(Regex::MatchData)) # => Array(Regex::MatchData)
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# YAML configuration example:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# Style/IsAFilter:
|
|
|
|
# Enabled: true
|
|
|
|
# FilterNames:
|
|
|
|
# - select
|
|
|
|
# - reject
|
|
|
|
# - any?
|
|
|
|
# - all?
|
|
|
|
# - none?
|
|
|
|
# - one?
|
|
|
|
# ```
|
|
|
|
class IsAFilter < Base
|
|
|
|
properties do
|
|
|
|
description "Identifies usage of `is_a?/nil?` calls within filters."
|
|
|
|
filter_names : Array(String) = %w(select reject any? all? none? one?)
|
|
|
|
end
|
|
|
|
|
2021-11-06 13:15:19 +00:00
|
|
|
MSG = "Use `%s` instead of `%s`"
|
|
|
|
NEW = "%s(%s)"
|
|
|
|
OLD = "%s {...}"
|
2021-01-24 19:23:32 +00:00
|
|
|
|
2021-04-04 09:19:45 +00:00
|
|
|
def test(source)
|
|
|
|
AST::NodeVisitor.new self, source, skip: [
|
|
|
|
Crystal::Macro,
|
|
|
|
Crystal::MacroExpression,
|
|
|
|
Crystal::MacroIf,
|
|
|
|
Crystal::MacroFor,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-01-24 19:23:32 +00:00
|
|
|
def test(source, node : Crystal::Call)
|
|
|
|
return unless node.name.in?(filter_names)
|
2021-11-06 13:15:19 +00:00
|
|
|
return unless (filter_location = node.name_location)
|
2021-01-24 19:23:32 +00:00
|
|
|
return unless (block = node.block)
|
|
|
|
return unless (body = block.body).is_a?(Crystal::IsA)
|
|
|
|
return unless (path = body.const).is_a?(Crystal::Path)
|
|
|
|
return unless body.obj.is_a?(Crystal::Var)
|
2021-03-23 18:56:02 +00:00
|
|
|
return if block.args.size > 1
|
2021-01-24 19:23:32 +00:00
|
|
|
|
|
|
|
name = path.names.join("::")
|
|
|
|
name = "::#{name}" if path.global? && !body.nil_check?
|
|
|
|
|
|
|
|
end_location = node.end_location
|
|
|
|
if !end_location || end_location.try(&.column_number.zero?)
|
|
|
|
if end_location = path.end_location
|
|
|
|
end_location = Crystal::Location.new(
|
|
|
|
end_location.filename,
|
|
|
|
end_location.line_number,
|
|
|
|
end_location.column_number + 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-06 13:15:19 +00:00
|
|
|
old = OLD % node.name
|
|
|
|
new = NEW % {node.name, name}
|
|
|
|
msg = MSG % {new, old}
|
|
|
|
if end_location
|
|
|
|
issue_for(filter_location, end_location, msg) do |corrector|
|
|
|
|
corrector.replace(filter_location, end_location, new)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
issue_for(filter_location, nil, msg)
|
|
|
|
end
|
2021-01-24 19:23:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|