Remove treating paths as literals in AST::Util#literal? & friends

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-08 14:24:51 +01:00
parent 0bd4ed0c45
commit 2dc21a00d9

View file

@ -4,7 +4,7 @@ module Ameba::AST::Util
# #
# 1. is *node* a literal? # 1. is *node* a literal?
# 2. can *node* be proven static? # 2. can *node* be proven static?
protected def literal_kind?(node, include_paths = false) : {Bool, Bool} protected def literal_kind?(node) : {Bool, Bool}
case node case node
when Crystal::NilLiteral, when Crystal::NilLiteral,
Crystal::BoolLiteral, Crystal::BoolLiteral,
@ -17,44 +17,42 @@ module Ameba::AST::Util
Crystal::MacroLiteral Crystal::MacroLiteral
{true, true} {true, true}
when Crystal::RangeLiteral when Crystal::RangeLiteral
{true, static_literal?(node.from, include_paths) && {true, static_literal?(node.from) &&
static_literal?(node.to, include_paths)} static_literal?(node.to)}
when Crystal::ArrayLiteral, when Crystal::ArrayLiteral,
Crystal::TupleLiteral Crystal::TupleLiteral
{true, node.elements.all? do |el| {true, node.elements.all? do |el|
static_literal?(el, include_paths) static_literal?(el)
end} end}
when Crystal::HashLiteral when Crystal::HashLiteral
{true, node.entries.all? do |entry| {true, node.entries.all? do |entry|
static_literal?(entry.key, include_paths) && static_literal?(entry.key) &&
static_literal?(entry.value, include_paths) static_literal?(entry.value)
end} end}
when Crystal::NamedTupleLiteral when Crystal::NamedTupleLiteral
{true, node.entries.all? do |entry| {true, node.entries.all? do |entry|
static_literal?(entry.value, include_paths) static_literal?(entry.value)
end} end}
when Crystal::Path
{include_paths, true}
else else
{false, false} {false, false}
end end
end end
# Returns `true` if current `node` is a static literal, `false` otherwise. # Returns `true` if current `node` is a static literal, `false` otherwise.
def static_literal?(node, include_paths = false) : Bool def static_literal?(node) : Bool
is_literal, is_static = literal_kind?(node, include_paths) is_literal, is_static = literal_kind?(node)
is_literal && is_static is_literal && is_static
end end
# Returns `true` if current `node` is a dynamic literal, `false` otherwise. # Returns `true` if current `node` is a dynamic literal, `false` otherwise.
def dynamic_literal?(node, include_paths = false) : Bool def dynamic_literal?(node) : Bool
is_literal, is_static = literal_kind?(node, include_paths) is_literal, is_static = literal_kind?(node)
is_literal && !is_static is_literal && !is_static
end end
# Returns `true` if current `node` is a literal, `false` otherwise. # Returns `true` if current `node` is a literal, `false` otherwise.
def literal?(node, include_paths = false) : Bool def literal?(node) : Bool
is_literal, _ = literal_kind?(node, include_paths) is_literal, _ = literal_kind?(node)
is_literal is_literal
end end