diff --git a/src/ameba/rule/lint/not_nil_after_no_bang.cr b/src/ameba/rule/lint/not_nil_after_no_bang.cr index b69a4927..c215d6da 100644 --- a/src/ameba/rule/lint/not_nil_after_no_bang.cr +++ b/src/ameba/rule/lint/not_nil_after_no_bang.cr @@ -41,7 +41,7 @@ module Ameba::Rule::Lint return unless (obj = node.obj).is_a?(Crystal::Call) return unless obj.name.in?(obj.block ? BLOCK_CALL_NAMES : CALL_NAMES) - return unless name_location = obj.name_location + return unless name_location = name_location(obj) return unless name_location_end = name_end_location(obj) return unless end_location = name_end_location(node) diff --git a/src/ameba/rule/lint/redundant_string_coercion.cr b/src/ameba/rule/lint/redundant_string_coercion.cr index befb3a9b..0a486084 100644 --- a/src/ameba/rule/lint/redundant_string_coercion.cr +++ b/src/ameba/rule/lint/redundant_string_coercion.cr @@ -31,7 +31,7 @@ module Ameba::Rule::Lint def test(source, node : Crystal::StringInterpolation) string_coercion_nodes(node).each do |expr| - issue_for expr.name_location, expr.end_location, MSG + issue_for name_location(expr), expr.end_location, MSG end end diff --git a/src/ameba/rule/performance/any_after_filter.cr b/src/ameba/rule/performance/any_after_filter.cr index c16d9412..e4f7bcaf 100644 --- a/src/ameba/rule/performance/any_after_filter.cr +++ b/src/ameba/rule/performance/any_after_filter.cr @@ -27,6 +27,8 @@ module Ameba::Rule::Performance # - reject # ``` class AnyAfterFilter < Base + include AST::Util + properties do description "Identifies usage of `any?` calls that follow filters" filter_names %w(select reject) @@ -39,7 +41,7 @@ module Ameba::Rule::Performance return unless obj.is_a?(Crystal::Call) && obj.block && node.block.nil? return unless obj.name.in?(filter_names) - issue_for obj.name_location, node.name_end_location, MSG % obj.name + issue_for name_location(obj), name_end_location(node), MSG % obj.name end end end diff --git a/src/ameba/rule/performance/compact_after_map.cr b/src/ameba/rule/performance/compact_after_map.cr index e7807a1b..7ec44a1d 100644 --- a/src/ameba/rule/performance/compact_after_map.cr +++ b/src/ameba/rule/performance/compact_after_map.cr @@ -22,6 +22,8 @@ module Ameba::Rule::Performance # Enabled: true # ``` class CompactAfterMap < Base + include AST::Util + properties do description "Identifies usage of `compact` calls that follow `map`" end @@ -37,7 +39,7 @@ module Ameba::Rule::Performance return unless obj.is_a?(Crystal::Call) && obj.block return unless obj.name == "map" - issue_for obj.name_location, node.name_end_location, MSG + issue_for name_location(obj), name_end_location(node), MSG end end end diff --git a/src/ameba/rule/performance/excessive_allocations.cr b/src/ameba/rule/performance/excessive_allocations.cr index eef646a4..41250dfd 100644 --- a/src/ameba/rule/performance/excessive_allocations.cr +++ b/src/ameba/rule/performance/excessive_allocations.cr @@ -57,7 +57,7 @@ module Ameba::Rule::Performance return unless obj.args.empty? && obj.block.nil? return unless method = call_names[obj.name]? - return unless name_location = obj.name_location + return unless name_location = name_location(obj) return unless end_location = name_end_location(node) msg = MSG % {method, obj.name} diff --git a/src/ameba/rule/performance/first_last_after_filter.cr b/src/ameba/rule/performance/first_last_after_filter.cr index 9127ccd5..68836a02 100644 --- a/src/ameba/rule/performance/first_last_after_filter.cr +++ b/src/ameba/rule/performance/first_last_after_filter.cr @@ -26,6 +26,8 @@ module Ameba::Rule::Performance # - select # ``` class FirstLastAfterFilter < Base + include AST::Util + properties do description "Identifies usage of `first/last/first?/last?` calls that follow filters" filter_names %w(select) @@ -47,7 +49,7 @@ module Ameba::Rule::Performance message = node.name.includes?(CALL_NAMES.first) ? MSG : MSG_REVERSE - issue_for obj.name_location, node.name_end_location, + issue_for name_location(obj), name_end_location(node), message % {obj.name, node.name} end end diff --git a/src/ameba/rule/performance/flatten_after_map.cr b/src/ameba/rule/performance/flatten_after_map.cr index 34576240..cbdd4f1f 100644 --- a/src/ameba/rule/performance/flatten_after_map.cr +++ b/src/ameba/rule/performance/flatten_after_map.cr @@ -22,6 +22,8 @@ module Ameba::Rule::Performance # Enabled: true # ``` class FlattenAfterMap < Base + include AST::Util + properties do description "Identifies usage of `flatten` calls that follow `map`" end @@ -37,7 +39,7 @@ module Ameba::Rule::Performance return unless obj.is_a?(Crystal::Call) && obj.block return unless obj.name == "map" - issue_for obj.name_location, node.name_end_location, MSG + issue_for name_location(obj), name_end_location(node), MSG end end end diff --git a/src/ameba/rule/performance/map_instead_of_block.cr b/src/ameba/rule/performance/map_instead_of_block.cr index 74cbce8f..5831710b 100644 --- a/src/ameba/rule/performance/map_instead_of_block.cr +++ b/src/ameba/rule/performance/map_instead_of_block.cr @@ -23,6 +23,8 @@ module Ameba::Rule::Performance # Enabled: true # ``` class MapInsteadOfBlock < Base + include AST::Util + properties do description "Identifies usage of `sum/product` calls that follow `map`" end @@ -40,7 +42,7 @@ module Ameba::Rule::Performance return unless obj.is_a?(Crystal::Call) && obj.block return unless obj.name == MAP_NAME - issue_for obj.name_location, node.name_end_location, + issue_for name_location(obj), name_end_location(node), MSG % {node.name, node.name} end end diff --git a/src/ameba/rule/performance/minmax_after_map.cr b/src/ameba/rule/performance/minmax_after_map.cr index 248c3421..803c84e1 100644 --- a/src/ameba/rule/performance/minmax_after_map.cr +++ b/src/ameba/rule/performance/minmax_after_map.cr @@ -26,6 +26,8 @@ module Ameba::Rule::Performance # Enabled: true # ``` class MinMaxAfterMap < Base + include AST::Util + properties do description "Identifies usage of `min/max/minmax` calls that follow `map`" end @@ -42,14 +44,14 @@ module Ameba::Rule::Performance return unless (obj = node.obj) && obj.is_a?(Crystal::Call) return unless obj.name == "map" && obj.block && obj.args.empty? - return unless name_location = obj.name_location - return unless end_location = node.name_end_location + return unless name_location = name_location(obj) + return unless end_location = name_end_location(node) of_name = node.name.sub(/(.+?)(\?)?$/, "\\1_of\\2") message = MSG % {of_name, node.name} issue_for name_location, end_location, message do |corrector| - next unless node_name_location = node.name_location + next unless node_name_location = name_location(node) # TODO: switching the order of the below calls breaks the corrector corrector.replace( diff --git a/src/ameba/rule/performance/size_after_filter.cr b/src/ameba/rule/performance/size_after_filter.cr index 1f1e1857..ea58112f 100644 --- a/src/ameba/rule/performance/size_after_filter.cr +++ b/src/ameba/rule/performance/size_after_filter.cr @@ -33,6 +33,8 @@ module Ameba::Rule::Performance # - reject # ``` class SizeAfterFilter < Base + include AST::Util + properties do description "Identifies usage of `size` calls that follow filter" filter_names %w(select reject) @@ -49,7 +51,7 @@ module Ameba::Rule::Performance return unless obj.is_a?(Crystal::Call) && obj.block return unless obj.name.in?(filter_names) - issue_for obj.name_location, node.name_end_location, MSG % obj.name + issue_for name_location(obj), name_end_location(node), MSG % obj.name end end end diff --git a/src/ameba/rule/style/is_a_filter.cr b/src/ameba/rule/style/is_a_filter.cr index 97c131fa..b2847fce 100644 --- a/src/ameba/rule/style/is_a_filter.cr +++ b/src/ameba/rule/style/is_a_filter.cr @@ -39,6 +39,8 @@ module Ameba::Rule::Style # - one? # ``` class IsAFilter < Base + include AST::Util + properties do description "Identifies usage of `is_a?/nil?` calls within filters" filter_names %w(select reject any? all? none? one?) @@ -54,7 +56,7 @@ module Ameba::Rule::Style def test(source, node : Crystal::Call) return unless node.name.in?(filter_names) - return unless filter_location = node.name_location + return unless filter_location = name_location(node) return unless block = node.block return unless (body = block.body).is_a?(Crystal::IsA) return unless (path = body.const).is_a?(Crystal::Path) diff --git a/src/ameba/rule/style/verbose_block.cr b/src/ameba/rule/style/verbose_block.cr index bb8b2cf7..aec0ea38 100644 --- a/src/ameba/rule/style/verbose_block.cr +++ b/src/ameba/rule/style/verbose_block.cr @@ -47,7 +47,7 @@ module Ameba::Rule::Style CALL_PATTERN = "%s(%s&.%s)" protected def same_location_lines?(a, b) - return unless a_location = a.name_location + return unless a_location = name_location(a) return unless b_location = b.location a_location.line_number == b_location.line_number @@ -78,7 +78,7 @@ module Ameba::Rule::Style protected def valid_line_length?(node, code) if max_line_length = self.max_line_length - if location = node.name_location + if location = name_location(node) final_line_length = location.column_number + code.size return final_line_length <= max_line_length end @@ -203,7 +203,7 @@ module Ameba::Rule::Style return unless valid_line_length?(call, call_code) return unless valid_length?(call_code) - return unless location = call.name_location + return unless location = name_location(call) return unless end_location = block.end_location if call_code.includes?("{...}")