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) # visitor = Ameba::AST::NodeVisitor.new(rule, source)
# ``` # ```
class NodeVisitor < BaseVisitor class NodeVisitor < BaseVisitor
@[Flags]
enum Category
Macro
end
# List of nodes to be visited by Ameba's rules. # List of nodes to be visited by Ameba's rules.
NODES = { NODES = {
Alias, Alias,
IsA,
Assign, Assign,
Call,
Block, Block,
Call,
Case, Case,
ClassDef, ClassDef,
ClassVar, ClassVar,
@ -25,20 +29,37 @@ module Ameba::AST
HashLiteral, HashLiteral,
If, If,
InstanceVar, InstanceVar,
IsA,
LibDef, LibDef,
ModuleDef, ModuleDef,
NilLiteral, NilLiteral,
StringInterpolation, StringInterpolation,
Unless, Unless,
Until,
Var, Var,
When, When,
While, While,
Until,
} }
@skip : Array(Crystal::ASTNode.class)? @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)) @skip = skip.try &.map(&.as(Crystal::ASTNode.class))
super @rule, @source super @rule, @source
end end

View file

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

View file

@ -34,12 +34,7 @@ module Ameba::Rule::Lint
MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`" MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`"
def test(source) def test(source)
AST::NodeVisitor.new self, source, skip: [ AST::NodeVisitor.new self, source, skip: :macro
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
end end
def test(source, node : Crystal::Call) 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" MSG = "Use bang method variant `%s!` after chained `%s` call"
def test(source) def test(source)
AST::NodeVisitor.new self, source, skip: [ AST::NodeVisitor.new self, source, skip: :macro
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
end end
def test(source, node : Crystal::Call) def test(source, node : Crystal::Call)

View file

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

View file

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

View file

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

View file

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

View file

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