Module: Tweezer::ASTHelper
Class Method Summary collapse
- .append_block_child(block, node) ⇒ Object
- .block?(node) ⇒ Boolean
- .block_children(node) ⇒ Object
- .group_block?(node) ⇒ Boolean
- .groups_from_group_block(node) ⇒ Object
- .s(type, *children) ⇒ Object
- .source_block?(node) ⇒ Boolean
- .unparse_hash_node(node) ⇒ Object
Instance Method Summary collapse
Class Method Details
.append_block_child(block, node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tweezer/ast_helper.rb', line 46 def append_block_child(block, node) new_children = block.children[0..1] old_child = block.children[2] if old_child.type == :begin new_children << old_child.append(node) else new_children << s(:begin, old_child, node) end block.updated(nil, new_children) end |
.block?(node) ⇒ Boolean
11 12 13 |
# File 'lib/tweezer/ast_helper.rb', line 11 def block?(node) node.type == :block end |
.block_children(node) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/tweezer/ast_helper.rb', line 27 def block_children(node) child = node.children[2] children = child.type == :begin ? child.children : [child] return children unless block_given? children.each { |c| yield c } end |
.group_block?(node) ⇒ Boolean
21 22 23 24 25 |
# File 'lib/tweezer/ast_helper.rb', line 21 def group_block?(node) block?(node) && node.children[0].type == :send && node.children[0].children[1] == :group end |
.groups_from_group_block(node) ⇒ Object
34 35 36 |
# File 'lib/tweezer/ast_helper.rb', line 34 def groups_from_group_block(node) node.children[0].children[2..-1].flat_map(&:children) end |
.s(type, *children) ⇒ Object
3 4 5 |
# File 'lib/tweezer/ast_helper.rb', line 3 def s(type, *children) Parser::AST::Node.new(type, children) end |
.source_block?(node) ⇒ Boolean
15 16 17 18 19 |
# File 'lib/tweezer/ast_helper.rb', line 15 def source_block?(node) block?(node) && node.children[0].type == :send && node.children[0].children[1] == :source end |
.unparse_hash_node(node) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/tweezer/ast_helper.rb', line 38 def unparse_hash_node(node) return {} unless node fail ArgumentError unless node.type == :hash Hash[node.children.map do |child| [child.children[0].children[0], child.children[1]] end] end |
Instance Method Details
#blank_line ⇒ Object
7 8 9 |
# File 'lib/tweezer/ast_helper.rb', line 7 def blank_line Parser::AST::Node.new(:blank_line) end |