Skip to content

Commit

Permalink
Refactor ScopeVisitor to ignore accessor macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Dec 27, 2023
1 parent 509b56d commit 4f2e2c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/ameba/ast/scope.cr
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ module Ameba::AST
node.is_a?(Crystal::Def)
end

# Returns `true` if current scope is a class, `false` otherwise.
def class_def?
node.is_a?(Crystal::ClassDef)
end

# Returns `true` if current scope is a module, `false` otherwise.
def module_def?
node.is_a?(Crystal::ModuleDef)
end

# Returns `true` if this scope is a top level scope, `false` otherwise.
def top_level?
outer_scope.nil? || type_definition?
Expand Down
27 changes: 17 additions & 10 deletions src/ameba/ast/visitors/scope_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,30 @@ module Ameba::AST
# :nodoc:
def visit(node : Crystal::Call)
case
when @current_scope.def?
if node.name.in?(SPECIAL_NODE_NAMES) && node.args.empty?
@current_scope.arguments.each do |arg|
variable = arg.variable

ref = variable.reference(variable.node, @current_scope)
ref.explicit = false
end
when @current_scope.top_level? && record_macro?(node) then false
when @current_scope.module_def? && accessor_macro?(node) then false
when @current_scope.class_def? && accessor_macro?(node) then false
when @current_scope.def? && special_node?(node)
@current_scope.arguments.each do |arg|
variable = arg.variable

ref = variable.reference(variable.node, @current_scope)
ref.explicit = false
end
true
when @current_scope.top_level? && record_macro?(node)
false
else
true
end
end

private def special_node?(node)
node.name.in?(SPECIAL_NODE_NAMES) && node.args.empty?
end

private def accessor_macro?(node)
node.name.matches? /^(class_)?(getter[?!]?|setter|property[?!]?)$/
end

private def record_macro?(node)
node.name == "record" && node.args.first?.is_a?(Crystal::Path)
end
Expand Down

0 comments on commit 4f2e2c0

Please sign in to comment.