Module: PrismHelpers

Included in:
Deprecool::Finder
Defined in:
lib/deprecool/helpers/prism_helpers.rb

Instance Method Summary collapse

Instance Method Details

#arguments_are_length(node, length) ⇒ Object



40
41
42
43
44
# File 'lib/deprecool/helpers/prism_helpers.rb', line 40

def arguments_are_length(node, length)
  return false unless node.is_a?(Prism::ArgumentsNode)

  unwrap_arguments(node.arguments).length == length
end

#arguments_contain(node, value, position: -1,, kwarg: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/deprecool/helpers/prism_helpers.rb', line 46

def arguments_contain(node, value, position: -1, kwarg: nil)
  return false unless node.is_a?(Prism::ArgumentsNode)

  arguments = unwrap_arguments(node)

  return (value_from_argument(arguments[position]) == value) if position >= 0

  arguments.any? do |arg|
    value_from_argument(arg) == value
  end
end

#unwrap_arguments(node) ⇒ Object

Method arguments are represented as: node.arguments returns a literal array of arguments https://docs.ruby-lang.org/en/master/Prism/ArgumentsNode.html



15
16
17
18
19
# File 'lib/deprecool/helpers/prism_helpers.rb', line 15

def unwrap_arguments(node)
  return node unless node.is_a?(Prism::ArgumentsNode)

  node.arguments
end

#unwrap_array(node) ⇒ Object



22
23
24
25
26
# File 'lib/deprecool/helpers/prism_helpers.rb', line 22

def unwrap_array(node)
  return node unless node.is_a?(Prism::ArrayNode)

  node.elements
end

#unwrap_children(node) ⇒ Object



34
35
36
37
38
# File 'lib/deprecool/helpers/prism_helpers.rb', line 34

def unwrap_children(node)
  return node unless node.respond_to?(:child_nodes)

  node.child_nodes
end

#unwrap_class(node) ⇒ Object



28
29
30
31
32
# File 'lib/deprecool/helpers/prism_helpers.rb', line 28

def unwrap_class(node)
  return node unless node.is_a?(Prism::ClassNode)

  node.body
end

#unwrap_parentheses(node) ⇒ Object

(1..10) parses as a ParenthesesNode wrapping the real expression.



5
6
7
8
9
10
# File 'lib/deprecool/helpers/prism_helpers.rb', line 5

def unwrap_parentheses(node)
  return node unless node.is_a?(Prism::ParenthesesNode)

  body = node.body&.body
  body&.length == 1 ? body.first : node
end

#value_from_argument(node) ⇒ Object



58
59
60
61
62
# File 'lib/deprecool/helpers/prism_helpers.rb', line 58

def value_from_argument(node)
  case node
  when Prism::SymbolNode, Prism::StringNode then node.unescaped
  end
end