123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Parser::WithMeta::Builder

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::AST::NodePattern::Builder
Defined in: lib/rubocop/ast/node_pattern/with_meta.rb

Overview

Overrides Builder to emit nodes with locations

Instance Method Summary

Instance Method Details

#emit_atom(type, token)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 40

def emit_atom(type, token)
  value, loc = token
  begin_l = loc.resize(1)
  end_l = loc.end.adjust(begin_pos: -1)
  begin_l = nil if begin_l.source.match?(/\w/)
  end_l = nil if end_l.source.match?(/\w/)
  n(type, [value], source_map(token, begin_t: begin_l, end_t: end_l))
end

#emit_call(type, selector_t, args = nil)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 61

def emit_call(type, selector_t, args = nil)
  selector, = selector_t
  begin_t, arg_nodes, end_t = args

  map = source_map(selector_t, begin_t: begin_t, end_t: end_t, selector_t: selector_t)
  n(type, [selector, *arg_nodes], map)
end

#emit_list(type, begin_t, children, end_t)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 55

def emit_list(type, begin_t, children, end_t)
  expr = children.first.source_range.join(children.last.source_range)
  map = source_map(expr, begin_t: begin_t, end_t: end_t)
  n(type, children, map)
end

#emit_unary_op(type, operator_t = nil, *children)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 49

def emit_unary_op(type, operator_t = nil, *children)
  children[-1] = children[-1].first if children[-1].is_a?(Array) # token?
  map = source_map(children.first.source_range, operator_t: operator_t)
  n(type, children, map)
end

#join_exprs(left_expr, right_expr) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 81

def join_exprs(left_expr, right_expr)
  left_expr.source_range.join(right_expr.source_range)
end

#loc(token_or_range) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 75

def loc(token_or_range)
  return token_or_range[1] if token_or_range.is_a?(Array)

  token_or_range
end

#n(type, children, source_map) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 71

def n(type, children, source_map)
  super(type, children, { location: source_map })
end

#source_map(token_or_range, begin_t: nil, end_t: nil, operator_t: nil, selector_t: nil) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/with_meta.rb', line 85

def source_map(token_or_range, begin_t: nil, end_t: nil, operator_t: nil, selector_t: nil)
  expression_l = loc(token_or_range)
  expression_l = expression_l.expression if expression_l.respond_to?(:expression)
  locs = [begin_t, end_t, operator_t, selector_t].map { |token| loc(token) }
  begin_l, end_l, operator_l, selector_l = locs

  expression_l = locs.compact.inject(expression_l, :join)

  ::Parser::Source::Map::Send.new(_dot_l = nil, selector_l, begin_l, end_l, expression_l)
                             .with_operator(operator_l)
end