Refactor Lint::Documentation rule to use a custom visitor

This commit is contained in:
Sijawusz Pur Rahnama 2023-05-29 16:33:45 +02:00
parent 1a9a58b3cd
commit 09fdac6be9

View file

@ -22,10 +22,10 @@ module Ameba::Rule::Lint
MSG = "Missing documentation" MSG = "Missing documentation"
def test(source) def test(source)
AST::ScopeVisitor.new self, source DocumentationVisitor.new self, source
end end
def test(source, node : Crystal::ClassDef | Crystal::ModuleDef | Crystal::EnumDef | Crystal::Def | Crystal::Macro, scope : AST::Scope) def test(source, node)
return unless node.visibility.public? return unless node.visibility.public?
case node case node
@ -38,5 +38,33 @@ module Ameba::Rule::Lint
issue_for(node, MSG) unless node.doc.presence issue_for(node, MSG) unless node.doc.presence
end end
# :nodoc:
private class DocumentationVisitor < AST::BaseVisitor
NODES = {
ClassDef,
ModuleDef,
EnumDef,
Def,
Macro,
}
@visibility : Crystal::Visibility = :public
def visit(node : Crystal::VisibilityModifier)
@visibility = node.modifier
true
end
{% for name in NODES %}
def visit(node : Crystal::{{ name }})
node.visibility = @visibility
@visibility = :public
@rule.test @source, node
true
end
{% end %}
end
end end
end end