Class: ImproveYourCode::AST::Node
- Inherits:
-
Parser::AST::Node
- Object
- Parser::AST::Node
- ImproveYourCode::AST::Node
- Defined in:
- lib/improve_your_code/ast/node.rb
Instance Method Summary collapse
- #contains_nested_node?(target_type) ⇒ Boolean
- #each_node(target_type, ignoring = [], &blk) ⇒ Object
- #find_nodes(target_types, ignoring = []) ⇒ Object
- #format_to_ruby ⇒ Object
- #full_comment ⇒ Object
-
#initialize(type, children = [], options = {}) ⇒ Node
constructor
A new instance of Node.
- #leading_comment ⇒ Object
- #length ⇒ Object
- #line ⇒ Object
- #source ⇒ Object
- #statements ⇒ Object
Constructor Details
#initialize(type, children = [], options = {}) ⇒ Node
12 13 14 15 |
# File 'lib/improve_your_code/ast/node.rb', line 12 def initialize(type, children = [], = {}) @comments = .fetch(:comments, []) super end |
Instance Method Details
#contains_nested_node?(target_type) ⇒ 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_ruby ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/improve_your_code/ast/node.rb', line 54 def format_to_ruby if location lines = location.expression.source.split("\n").map(&:strip) case lines.length when 1 then lines.first when 2 then lines.join('; ') else [lines.first, lines.last].join(' ... ') end else to_s end end |
#full_comment ⇒ Object
17 18 19 |
# File 'lib/improve_your_code/ast/node.rb', line 17 def full_comment comments.map(&:text).join("\n") end |
#leading_comment ⇒ Object
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 |
#length ⇒ Object
67 68 69 |
# File 'lib/improve_your_code/ast/node.rb', line 67 def length 1 end |
#line ⇒ Object
29 30 31 |
# File 'lib/improve_your_code/ast/node.rb', line 29 def line loc&.line end |
#source ⇒ Object
75 76 77 |
# File 'lib/improve_your_code/ast/node.rb', line 75 def source loc.expression.source_buffer.name end |
#statements ⇒ Object
71 72 73 |
# File 'lib/improve_your_code/ast/node.rb', line 71 def statements [self] end |