Merge pull request #378 from crystal-ameba/node-visitor-category

This commit is contained in:
Sijawusz Pur Rahnama 2023-06-13 11:36:05 +02:00 committed by GitHub
commit b4244d4c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 58 deletions

View File

@ -8,13 +8,17 @@ module Ameba::AST
# visitor = Ameba::AST::NodeVisitor.new(rule, source)
# ```
class NodeVisitor < BaseVisitor
@[Flags]
enum Category
Macro
end
# List of nodes to be visited by Ameba's rules.
NODES = {
Alias,
IsA,
Assign,
Call,
Block,
Call,
Case,
ClassDef,
ClassVar,
@ -25,20 +29,37 @@ module Ameba::AST
HashLiteral,
If,
InstanceVar,
IsA,
LibDef,
ModuleDef,
NilLiteral,
StringInterpolation,
Unless,
Until,
Var,
When,
While,
Until,
}
@skip : Array(Crystal::ASTNode.class)?
def initialize(@rule, @source, skip = nil)
def self.category_to_node_classes(category : Category)
([] of Crystal::ASTNode.class).tap do |classes|
classes.push(
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
) if category.macro?
end
end
def initialize(@rule, @source, *, skip : Category)
initialize @rule, @source,
skip: NodeVisitor.category_to_node_classes(skip)
end
def initialize(@rule, @source, *, skip : Array? = nil)
@skip = skip.try &.map(&.as(Crystal::ASTNode.class))
super @rule, @source
end

View File

@ -36,12 +36,7 @@ module Ameba::Rule::Lint
MSG = "Avoid using `not_nil!`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -34,12 +34,7 @@ module Ameba::Rule::Lint
MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -59,12 +59,7 @@ module Ameba::Rule::Performance
MSG = "Use bang method variant `%s!` after chained `%s` call"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -31,12 +31,7 @@ module Ameba::Rule::Performance
MSG = "Use `compact_map {...}` instead of `map {...}.compact`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -36,12 +36,7 @@ module Ameba::Rule::Performance
MSG_REVERSE = "Use `reverse_each.find {...}` instead of `%s {...}.%s`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -31,12 +31,7 @@ module Ameba::Rule::Performance
MSG = "Use `flat_map {...}` instead of `map {...}.flatten`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -32,12 +32,7 @@ module Ameba::Rule::Performance
MSG = "Use `%s {...}` instead of `map {...}.%s`"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -42,12 +42,7 @@ module Ameba::Rule::Performance
MSG = "Use `count {...}` instead of `%s {...}.size`."
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)

View File

@ -49,12 +49,7 @@ module Ameba::Rule::Style
OLD = "%s {...}"
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)