diff --git a/spec/ameba/ast_traverse_spec.cr b/spec/ameba/ast/traverse_spec.cr similarity index 82% rename from spec/ameba/ast_traverse_spec.cr rename to spec/ameba/ast/traverse_spec.cr index 5cec2da3..3c14a9be 100644 --- a/spec/ameba/ast_traverse_spec.cr +++ b/spec/ameba/ast/traverse_spec.cr @@ -1,10 +1,10 @@ -require "../spec_helper" +require "../../spec_helper" -module Ameba +module Ameba::AST rule = DummyRule.new source = Source.new "" - describe "AST Traverse" do + describe "Traverse" do {% for name in NODE_VISITORS %} describe "{{name}}" do it "allow to visit {{name}} node" do diff --git a/spec/ameba/ast/util_spec.cr b/spec/ameba/ast/util_spec.cr new file mode 100644 index 00000000..08bd3497 --- /dev/null +++ b/spec/ameba/ast/util_spec.cr @@ -0,0 +1,39 @@ +require "../../spec_helper" + +module Ameba::AST + struct Test + include Util + end + + subject = Test.new + + describe Util do + describe "#literal?" do + [ + Crystal::ArrayLiteral.new, + Crystal::BoolLiteral.new(false), + Crystal::CharLiteral.new('a'), + Crystal::HashLiteral.new, + Crystal::NamedTupleLiteral.new, + Crystal::NilLiteral.new, + Crystal::NumberLiteral.new(42), + Crystal::RegexLiteral.new(Crystal::NilLiteral.new), + Crystal::StringLiteral.new(""), + Crystal::SymbolLiteral.new(""), + Crystal::TupleLiteral.new([] of Crystal::ASTNode), + Crystal::RangeLiteral.new( + Crystal::NilLiteral.new, + Crystal::NilLiteral.new, + true), + ].each do |literal| + it "returns true if node is #{literal}" do + subject.literal?(literal).should be_true + end + end + + it "returns false if node is not a literal" do + subject.literal?(Crystal::Nop).should be_false + end + end + end +end diff --git a/src/ameba.cr b/src/ameba.cr index 5caa690a..043b4846 100644 --- a/src/ameba.cr +++ b/src/ameba.cr @@ -1,4 +1,5 @@ require "./ameba/*" +require "./ameba/ast/*" require "./ameba/rules/*" module Ameba diff --git a/src/ameba/ast_traverse.cr b/src/ameba/ast/traverse.cr similarity index 96% rename from src/ameba/ast_traverse.cr rename to src/ameba/ast/traverse.cr index b4a06c74..6b3554fa 100644 --- a/src/ameba/ast_traverse.cr +++ b/src/ameba/ast/traverse.cr @@ -1,6 +1,6 @@ require "compiler/crystal/syntax/*" -module Ameba +module Ameba::AST NODE_VISITORS = [ Call, Case, diff --git a/src/ameba/ast/util.cr b/src/ameba/ast/util.cr new file mode 100644 index 00000000..6e6b4970 --- /dev/null +++ b/src/ameba/ast/util.cr @@ -0,0 +1,5 @@ +module Ameba::AST::Util + def literal?(node) + node.try &.class.name.ends_with? "Literal" + end +end diff --git a/src/ameba/rules/comparison_to_boolean.cr b/src/ameba/rules/comparison_to_boolean.cr index 3d4e61c0..e76df2cf 100644 --- a/src/ameba/rules/comparison_to_boolean.cr +++ b/src/ameba/rules/comparison_to_boolean.cr @@ -13,7 +13,7 @@ module Ameba::Rules # or negating the variable. struct ComparisonToBoolean < Rule def test(source) - CallVisitor.new self, source + AST::CallVisitor.new self, source end def test(source, node : Crystal::Call) diff --git a/src/ameba/rules/literal_in_condition.cr b/src/ameba/rules/literal_in_condition.cr index 7dfae7e7..033f79d8 100644 --- a/src/ameba/rules/literal_in_condition.cr +++ b/src/ameba/rules/literal_in_condition.cr @@ -13,10 +13,12 @@ module Ameba::Rules # end # ``` struct LiteralInCondition < Rule + include AST::Util + def test(source) - IfVisitor.new self, source - UnlessVisitor.new self, source - CaseVisitor.new self, source + AST::IfVisitor.new self, source + AST::UnlessVisitor.new self, source + AST::CaseVisitor.new self, source end def check_node(source, node) @@ -36,9 +38,5 @@ module Ameba::Rules def test(source, node : Crystal::Case) check_node source, node end - - private def literal?(node) - node.try &.class.name.ends_with? "Literal" - end end end diff --git a/src/ameba/rules/unless_else.cr b/src/ameba/rules/unless_else.cr index 3a60766f..148b3b12 100644 --- a/src/ameba/rules/unless_else.cr +++ b/src/ameba/rules/unless_else.cr @@ -37,7 +37,7 @@ module Ameba::Rules # ``` struct UnlessElse < Rule def test(source) - UnlessVisitor.new self, source + AST::UnlessVisitor.new self, source end def test(source, node : Crystal::Unless)