This commit is contained in:
Vitalii Elenhaupt 2017-11-01 13:10:34 +02:00
parent dcc0be892a
commit 6a81a648e3
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
8 changed files with 56 additions and 13 deletions

View file

@ -1,10 +1,10 @@
require "../spec_helper" require "../../spec_helper"
module Ameba module Ameba::AST
rule = DummyRule.new rule = DummyRule.new
source = Source.new "" source = Source.new ""
describe "AST Traverse" do describe "Traverse" do
{% for name in NODE_VISITORS %} {% for name in NODE_VISITORS %}
describe "{{name}}" do describe "{{name}}" do
it "allow to visit {{name}} node" do it "allow to visit {{name}} node" do

View file

@ -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

View file

@ -1,4 +1,5 @@
require "./ameba/*" require "./ameba/*"
require "./ameba/ast/*"
require "./ameba/rules/*" require "./ameba/rules/*"
module Ameba module Ameba

View file

@ -1,6 +1,6 @@
require "compiler/crystal/syntax/*" require "compiler/crystal/syntax/*"
module Ameba module Ameba::AST
NODE_VISITORS = [ NODE_VISITORS = [
Call, Call,
Case, Case,

5
src/ameba/ast/util.cr Normal file
View file

@ -0,0 +1,5 @@
module Ameba::AST::Util
def literal?(node)
node.try &.class.name.ends_with? "Literal"
end
end

View file

@ -13,7 +13,7 @@ module Ameba::Rules
# or negating the variable. # or negating the variable.
struct ComparisonToBoolean < Rule struct ComparisonToBoolean < Rule
def test(source) def test(source)
CallVisitor.new self, source AST::CallVisitor.new self, source
end end
def test(source, node : Crystal::Call) def test(source, node : Crystal::Call)

View file

@ -13,10 +13,12 @@ module Ameba::Rules
# end # end
# ``` # ```
struct LiteralInCondition < Rule struct LiteralInCondition < Rule
include AST::Util
def test(source) def test(source)
IfVisitor.new self, source AST::IfVisitor.new self, source
UnlessVisitor.new self, source AST::UnlessVisitor.new self, source
CaseVisitor.new self, source AST::CaseVisitor.new self, source
end end
def check_node(source, node) def check_node(source, node)
@ -36,9 +38,5 @@ module Ameba::Rules
def test(source, node : Crystal::Case) def test(source, node : Crystal::Case)
check_node source, node check_node source, node
end end
private def literal?(node)
node.try &.class.name.ends_with? "Literal"
end
end end
end end

View file

@ -37,7 +37,7 @@ module Ameba::Rules
# ``` # ```
struct UnlessElse < Rule struct UnlessElse < Rule
def test(source) def test(source)
UnlessVisitor.new self, source AST::UnlessVisitor.new self, source
end end
def test(source, node : Crystal::Unless) def test(source, node : Crystal::Unless)