Misc refactors

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-23 03:22:27 +01:00
parent ffd63ef028
commit 8b43a40a65
4 changed files with 7 additions and 6 deletions

View file

@ -37,7 +37,7 @@ module Ameba::AST
# Returns true if this node or one of the parent branchables is a loop, false otherwise. # Returns true if this node or one of the parent branchables is a loop, false otherwise.
def loop? def loop?
loop?(node) || parent.try(&.loop?) || false loop?(node) || !!parent.try(&.loop?)
end end
end end
end end

View file

@ -30,6 +30,7 @@ module Ameba::AST
# ``` # ```
def initialize(@node, @variable, @scope) def initialize(@node, @variable, @scope)
return unless scope = @variable.scope return unless scope = @variable.scope
@branch = Branch.of(@node, scope) @branch = Branch.of(@node, scope)
@referenced = true if @variable.special? || @referenced = true if @variable.special? ||
@variable.scope.type_definition? || @variable.scope.type_definition? ||
@ -37,7 +38,7 @@ module Ameba::AST
end end
def referenced_in_loop? def referenced_in_loop?
@variable.referenced? && @branch.try &.in_loop? @variable.referenced? && !!@branch.try(&.in_loop?)
end end
# Returns true if this assignment is an op assign, false if not. # Returns true if this assignment is an op assign, false if not.

View file

@ -161,7 +161,7 @@ module Ameba::AST
def declared_before?(node) def declared_before?(node)
var_location, node_location = location, node.location 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) ||
(var_location.line_number == node_location.line_number && (var_location.line_number == node_location.line_number &&

View file

@ -3,7 +3,7 @@ module Ameba::AST
class CountingVisitor < Crystal::Visitor class CountingVisitor < Crystal::Visitor
DEFAULT_COMPLEXITY = 1 DEFAULT_COMPLEXITY = 1
getter macro_condition = false getter? macro_condition = false
# Creates a new counting visitor # Creates a new counting visitor
def initialize(@scope : Crystal::ASTNode) def initialize(@scope : Crystal::ASTNode)
@ -27,13 +27,13 @@ module Ameba::AST
{% for node in %i(if while until rescue or and) %} {% for node in %i(if while until rescue or and) %}
# :nodoc: # :nodoc:
def visit(node : Crystal::{{ node.id.capitalize }}) def visit(node : Crystal::{{ node.id.capitalize }})
@complexity += 1 unless macro_condition @complexity += 1 unless macro_condition?
end end
{% end %} {% end %}
# :nodoc: # :nodoc:
def visit(node : Crystal::Case) def visit(node : Crystal::Case)
return true if macro_condition return true if macro_condition?
# Count the complexity of an exhaustive `Case` as 1 # Count the complexity of an exhaustive `Case` as 1
# Otherwise count the number of `When`s # Otherwise count the number of `When`s