mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
AST Util
This commit is contained in:
parent
dcc0be892a
commit
6a81a648e3
8 changed files with 56 additions and 13 deletions
|
@ -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
|
39
spec/ameba/ast/util_spec.cr
Normal file
39
spec/ameba/ast/util_spec.cr
Normal 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
|
|
@ -1,4 +1,5 @@
|
||||||
require "./ameba/*"
|
require "./ameba/*"
|
||||||
|
require "./ameba/ast/*"
|
||||||
require "./ameba/rules/*"
|
require "./ameba/rules/*"
|
||||||
|
|
||||||
module Ameba
|
module Ameba
|
||||||
|
|
|
@ -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
5
src/ameba/ast/util.cr
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module Ameba::AST::Util
|
||||||
|
def literal?(node)
|
||||||
|
node.try &.class.name.ends_with? "Literal"
|
||||||
|
end
|
||||||
|
end
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue