mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge pull request #378 from crystal-ameba/node-visitor-category
This commit is contained in:
commit
b4244d4c61
10 changed files with 34 additions and 58 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue