From 8b43a40a65d4d7028e4fb2108e2e844e075ff87f Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Wed, 23 Nov 2022 03:22:27 +0100 Subject: [PATCH] Misc refactors --- src/ameba/ast/branchable.cr | 2 +- src/ameba/ast/variabling/assignment.cr | 3 ++- src/ameba/ast/variabling/variable.cr | 2 +- src/ameba/ast/visitors/counting_visitor.cr | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ameba/ast/branchable.cr b/src/ameba/ast/branchable.cr index 909f8a09..47eb5972 100644 --- a/src/ameba/ast/branchable.cr +++ b/src/ameba/ast/branchable.cr @@ -37,7 +37,7 @@ module Ameba::AST # Returns true if this node or one of the parent branchables is a loop, false otherwise. def loop? - loop?(node) || parent.try(&.loop?) || false + loop?(node) || !!parent.try(&.loop?) end end end diff --git a/src/ameba/ast/variabling/assignment.cr b/src/ameba/ast/variabling/assignment.cr index 11a7fbbc..f0d80c6f 100644 --- a/src/ameba/ast/variabling/assignment.cr +++ b/src/ameba/ast/variabling/assignment.cr @@ -30,6 +30,7 @@ module Ameba::AST # ``` def initialize(@node, @variable, @scope) return unless scope = @variable.scope + @branch = Branch.of(@node, scope) @referenced = true if @variable.special? || @variable.scope.type_definition? || @@ -37,7 +38,7 @@ module Ameba::AST end def referenced_in_loop? - @variable.referenced? && @branch.try &.in_loop? + @variable.referenced? && !!@branch.try(&.in_loop?) end # Returns true if this assignment is an op assign, false if not. diff --git a/src/ameba/ast/variabling/variable.cr b/src/ameba/ast/variabling/variable.cr index 19bad442..13ba570c 100644 --- a/src/ameba/ast/variabling/variable.cr +++ b/src/ameba/ast/variabling/variable.cr @@ -161,7 +161,7 @@ module Ameba::AST def declared_before?(node) var_location, node_location = location, node.location - return if var_location.nil? || node_location.nil? + return unless var_location && node_location (var_location.line_number < node_location.line_number) || (var_location.line_number == node_location.line_number && diff --git a/src/ameba/ast/visitors/counting_visitor.cr b/src/ameba/ast/visitors/counting_visitor.cr index a9e7dc72..7bb0bb7c 100644 --- a/src/ameba/ast/visitors/counting_visitor.cr +++ b/src/ameba/ast/visitors/counting_visitor.cr @@ -3,7 +3,7 @@ module Ameba::AST class CountingVisitor < Crystal::Visitor DEFAULT_COMPLEXITY = 1 - getter macro_condition = false + getter? macro_condition = false # Creates a new counting visitor def initialize(@scope : Crystal::ASTNode) @@ -27,13 +27,13 @@ module Ameba::AST {% for node in %i(if while until rescue or and) %} # :nodoc: def visit(node : Crystal::{{ node.id.capitalize }}) - @complexity += 1 unless macro_condition + @complexity += 1 unless macro_condition? end {% end %} # :nodoc: def visit(node : Crystal::Case) - return true if macro_condition + return true if macro_condition? # Count the complexity of an exhaustive `Case` as 1 # Otherwise count the number of `When`s