Class: Reek::AST::Node Private

Inherits:
Parser::AST::Node
  • Object
show all
Defined in:
lib/reek/ast/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for AST nodes extended with utility methods. Contains some methods to ease the transition from Sexp to AST::Node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, children = [], options = {}) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Node.



13
14
15
16
17
# File 'lib/reek/ast/node.rb', line 13

def initialize(type, children = [], options = {})
  @comments = options.fetch(:comments, [])
  @parent   = options.fetch(:parent, nil)
  super
end

Instance Attribute Details

#parentObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/reek/ast/node.rb', line 11

def parent
  @parent
end

Instance Method Details

#[](index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.


32
33
34
# File 'lib/reek/ast/node.rb', line 32

def [](index)
  elements[index]
end

#contains_nested_node?(target_type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/reek/ast/node.rb', line 90

def contains_nested_node?(target_type)
  look_for_type(target_type) { |_elem| return true }
  false
end

#each_node(target_type, ignoring = [], &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Carries out a depth-first traversal of this syntax tree, yielding every Sexp of type ‘target_type`. The traversal ignores any node whose type is listed in the Array `ignoring`. Takes a block as well.

target_type - the type to look for, e.g. :send, :block ignoring - types to ignore, e.g. [:casgn, :class, :module] blk - block to execute for every hit

Examples:

context.each_node(:send, [:mlhs]) do |call_node| .... end
context.each_node(:lvar).any? { |it| it.var_name == 'something' }

Returns an array with all matching nodes.



60
61
62
63
64
65
66
67
68
# File 'lib/reek/ast/node.rb', line 60

def each_node(target_type, ignoring = [], &blk)
  if block_given?
    look_for_type(target_type, ignoring, &blk)
  else
    result = []
    look_for_type(target_type, ignoring) { |exp| result << exp }
    result
  end
end

#find_nodes(target_types, ignoring = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Carries out a depth-first traversal of this syntax tree, yielding every Sexp of type ‘target_type`. The traversal ignores any node whose type is listed in the Array `ignoring`, including the top node. Takes a block as well.

target_types - the types to look for, e.g. [:send, :block] ignoring - types to ignore, e.g. [:casgn, :class, :module] blk - block to execute for every hit

Examples:

exp.find_nodes([:block]).flat_map do |elem| ... end

Returns an array with all matching nodes.



84
85
86
87
88
# File 'lib/reek/ast/node.rb', line 84

def find_nodes(target_types, ignoring = [])
  result = []
  look_for_types(target_types, ignoring) { |exp| result << exp }
  result
end

#firstObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.


41
42
43
# File 'lib/reek/ast/node.rb', line 41

def first
  type
end

#format_to_rubyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/reek/ast/node.rb', line 95

def format_to_ruby
  SexpFormatter.format(self)
end

#full_commentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/reek/ast/node.rb', line 19

def full_comment
  @comments.map(&:text).join("\n")
end

#leading_commentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
# File 'lib/reek/ast/node.rb', line 23

def leading_comment
  line = location.line
  comment_lines = @comments.select do |comment|
    comment.location.line < line
  end
  comment_lines.map(&:text).join("\n")
end

#lineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
# File 'lib/reek/ast/node.rb', line 36

def line
  loc.line
end