From 134963ece7ba59a78fa31acfd706d14f42505c2d Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sun, 27 Nov 2022 05:30:13 +0100 Subject: [PATCH] Minor details --- src/ameba/ast/visitors/top_level_nodes_visitor.cr | 6 ++++-- src/ameba/rule/layout/trailing_blank_lines.cr | 4 +++- src/ameba/rule/lint/ambiguous_assignment.cr | 1 + src/ameba/rule/lint/hash_duplicated_key.cr | 2 +- src/ameba/rule/lint/shadowed_exception.cr | 9 ++++++--- src/ameba/rule/metrics/cyclomatic_complexity.cr | 8 +++++--- src/ameba/rule/style/is_a_filter.cr | 1 + 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/ameba/ast/visitors/top_level_nodes_visitor.cr b/src/ameba/ast/visitors/top_level_nodes_visitor.cr index 6e606cae..5e5ad913 100644 --- a/src/ameba/ast/visitors/top_level_nodes_visitor.cr +++ b/src/ameba/ast/visitors/top_level_nodes_visitor.cr @@ -12,15 +12,17 @@ module Ameba::AST # :nodoc: def visit(node : Crystal::Require) require_nodes << node + true end - # If a top level node is Crystal::Expressions traverse the children. + # If a top level node is `Crystal::Expressions`, + # then always traverse the children. def visit(node : Crystal::Expressions) true end # A general visit method for rest of the nodes. - # Returns false meaning all child nodes will not be traversed. + # Returns `false`, meaning all child nodes will not be traversed. def visit(node : Crystal::ASTNode) false end diff --git a/src/ameba/rule/layout/trailing_blank_lines.cr b/src/ameba/rule/layout/trailing_blank_lines.cr index a9767286..dbfa3aa6 100644 --- a/src/ameba/rule/layout/trailing_blank_lines.cr +++ b/src/ameba/rule/layout/trailing_blank_lines.cr @@ -24,7 +24,9 @@ module Ameba::Rule::Layout return if source_lines_size == 1 && last_source_line.empty? last_line_empty = last_source_line.empty? - return if source_lines_size.zero? || (source_lines.last(2).join.presence && last_line_empty) + return if source_lines_size.zero? || + (source_lines.last(2).join.presence && last_line_empty) + if last_line_empty issue_for({source_lines_size, 1}, MSG) else diff --git a/src/ameba/rule/lint/ambiguous_assignment.cr b/src/ameba/rule/lint/ambiguous_assignment.cr index ad7cc9c5..e07dc467 100644 --- a/src/ameba/rule/lint/ambiguous_assignment.cr +++ b/src/ameba/rule/lint/ambiguous_assignment.cr @@ -41,6 +41,7 @@ module Ameba::Rule::Lint op_end_location.column_number - 1 ) op_text = source_between(op_location, op_end_location, source.lines) + return unless op_text return unless MISTAKES.has_key?(op_text) diff --git a/src/ameba/rule/lint/hash_duplicated_key.cr b/src/ameba/rule/lint/hash_duplicated_key.cr index c8ffcb4e..5b335edc 100644 --- a/src/ameba/rule/lint/hash_duplicated_key.cr +++ b/src/ameba/rule/lint/hash_duplicated_key.cr @@ -35,7 +35,7 @@ module Ameba::Rule::Lint private def duplicated_keys(entries) entries.map(&.key) .group_by(&.itself) - .tap(&.select! { |_, v| v.size > 1 }) + .select! { |_, v| v.size > 1 } .map { |k, _| k } end end diff --git a/src/ameba/rule/lint/shadowed_exception.cr b/src/ameba/rule/lint/shadowed_exception.cr index 6c779601..045117bb 100644 --- a/src/ameba/rule/lint/shadowed_exception.cr +++ b/src/ameba/rule/lint/shadowed_exception.cr @@ -45,13 +45,16 @@ module Ameba::Rule::Lint return if rescues.nil? - shadowed(rescues).each { |tp| issue_for tp, MSG % tp.names.join("::") } + shadowed(rescues).each do |path| + issue_for path, MSG % path.names.join("::") + end end private def shadowed(rescues, catch_all = false) traversed_types = Set(String).new - filter_rescues(rescues).each_with_object([] of Crystal::Path) do |types, shadowed| + rescues = filter_rescues(rescues) + rescues.each_with_object([] of Crystal::Path) do |types, shadowed| case when catch_all shadowed.concat(types) @@ -62,7 +65,7 @@ module Ameba::Rule::Lint catch_all = true next else - nodes = types.select { |tp| traverse(tp.to_s, traversed_types) } + nodes = types.select { |path| traverse(path.to_s, traversed_types) } shadowed.concat(nodes) unless nodes.empty? end end diff --git a/src/ameba/rule/metrics/cyclomatic_complexity.cr b/src/ameba/rule/metrics/cyclomatic_complexity.cr index 9b40c2ec..376c188f 100644 --- a/src/ameba/rule/metrics/cyclomatic_complexity.cr +++ b/src/ameba/rule/metrics/cyclomatic_complexity.cr @@ -20,10 +20,12 @@ module Ameba::Rule::Metrics def test(source, node : Crystal::Def) complexity = AST::CountingVisitor.new(node).count + return unless complexity > max_complexity - return unless complexity > max_complexity && (location = node.name_location) - issue_for location, name_end_location(node), - MSG % {complexity, max_complexity} + return unless location = node.name_location + end_location = name_end_location(node) + + issue_for location, end_location, MSG % {complexity, max_complexity} end end end diff --git a/src/ameba/rule/style/is_a_filter.cr b/src/ameba/rule/style/is_a_filter.cr index 6337040d..c2a27df8 100644 --- a/src/ameba/rule/style/is_a_filter.cr +++ b/src/ameba/rule/style/is_a_filter.cr @@ -79,6 +79,7 @@ module Ameba::Rule::Style 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)