Class: ImproveYourCode::AST::Node

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



12
13
14
15
# File 'lib/improve_your_code/ast/node.rb', line 12

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

Instance Method Details

#contains_nested_node?(target_type) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/improve_your_code/ast/node.rb', line 49

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

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



33
34
35
36
37
38
39
40
41
# File 'lib/improve_your_code/ast/node.rb', line 33

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



43
44
45
46
47
# File 'lib/improve_your_code/ast/node.rb', line 43

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

#format_to_rubyObject



54
55
56
57
58
59
60
61
# File 'lib/improve_your_code/ast/node.rb', line 54

def format_to_ruby
  if location
    lines = location.expression.source.split("\n").map(&:strip)
    lines.first
  else
    to_s
  end
end

#full_commentObject



17
18
19
# File 'lib/improve_your_code/ast/node.rb', line 17

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

#leading_commentObject



21
22
23
24
25
26
27
# File 'lib/improve_your_code/ast/node.rb', line 21

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

#lengthObject



63
64
65
# File 'lib/improve_your_code/ast/node.rb', line 63

def length
  1
end

#lineObject



29
30
31
# File 'lib/improve_your_code/ast/node.rb', line 29

def line
  loc&.line
end

#sourceObject



71
72
73
# File 'lib/improve_your_code/ast/node.rb', line 71

def source
  loc.expression.source_buffer.name
end

#statementsObject



67
68
69
# File 'lib/improve_your_code/ast/node.rb', line 67

def statements
  [self]
end