Skip to content

Add AST::NodeVisitor::Category simplifying code a bit #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/ameba/ast/visitors/node_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/lint/not_nil.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/lint/not_nil_after_no_bang.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/chained_call_with_no_bang.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/compact_after_map.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/first_last_after_filter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/flatten_after_map.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/map_instead_of_block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/performance/size_after_filter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions src/ameba/rule/style/is_a_filter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down