Included Modules

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Util

This module contains a collection of useful utility methods.

Public Instance Methods

block_length(block_node) click to toggle source
# File lib/rubocop/cop/util.rb, line 40
def block_length(block_node)
  block_node.loc.end.line - block_node.loc.begin.line
end
command?(name, node) click to toggle source
# File lib/rubocop/cop/util.rb, line 79
def command?(name, node)
  return unless node.type == :send

  receiver, method_name, _args = *node

  # commands have no explicit receiver
  !receiver && method_name == name
end
comment_line?(line_source) click to toggle source
# File lib/rubocop/cop/util.rb, line 44
def comment_line?(line_source)
  line_source =~ /^\s*#/
end
const_name(node) click to toggle source
# File lib/rubocop/cop/util.rb, line 61
def const_name(node)
  return nil if node.nil? || node.type != :const

  const_names = []
  const_node = node

  loop do
    namespace_node, name = *const_node
    const_names << name
    break unless namespace_node
    break unless namespace_node.is_a?(Parser::AST::Node)
    break if namespace_node.type == :cbase
    const_node = namespace_node
  end

  const_names.reverse.join('::')
end
first_part_of_call_chain(node) click to toggle source

Returns, for example, a bare `if` node if the given node is an `if` with calls chained to the end of it.

# File lib/rubocop/cop/util.rb, line 147
def first_part_of_call_chain(node)
  while node
    case node.type
    when :send
      receiver, _method_name, _args = *node
      node = receiver
    when :block
      method, _args, _body = *node
      node = method
    else
      break
    end
  end
  node
end
lambda?(node) click to toggle source
# File lib/rubocop/cop/util.rb, line 88
def lambda?(node)
  fail 'Not a block node' unless node.type == :block

  send_node, _block_args, _block_body = *node

  command?(:lambda, send_node)
end
lambda_or_proc?(node) click to toggle source
# File lib/rubocop/cop/util.rb, line 104
def lambda_or_proc?(node)
  lambda?(node) || proc?(node)
end
line_range(arg) click to toggle source
# File lib/rubocop/cop/util.rb, line 48
def line_range(arg)
  source_range = case arg
                 when Parser::Source::Range
                   arg
                 when Parser::AST::Node
                   arg.loc.expression
                 else
                   fail ArgumentError, "Invalid argument #{arg}"
                 end

  source_range.begin.line..source_range.end.line
end
on_node(syms, sexp, excludes = []) click to toggle source
# File lib/rubocop/cop/util.rb, line 112
def on_node(syms, sexp, excludes = [])
  yield sexp if Array(syms).include?(sexp.type)

  return if Array(excludes).include?(sexp.type)

  sexp.children.each do |elem|
    next unless elem.is_a?(Parser::AST::Node)
    on_node(syms, elem, excludes) { |s| yield s }
  end
end
operator?(symbol) click to toggle source
# File lib/rubocop/cop/util.rb, line 23
def operator?(symbol)
  OPERATOR_METHODS.include?(symbol)
end
parentheses?(node) click to toggle source
# File lib/rubocop/cop/util.rb, line 108
def parentheses?(node)
  node.loc.respond_to?(:end) && node.loc.end
end
proc?(node) click to toggle source
# File lib/rubocop/cop/util.rb, line 96
def proc?(node)
  fail 'Not a block node' unless node.type == :block

  send_node, _block_args, _block_body = *node

  command?(:proc, send_node) || send_node == PROC_NEW_NODE
end
range_with_surrounding_space(range, side = :both) click to toggle source
# File lib/rubocop/cop/util.rb, line 133
def range_with_surrounding_space(range, side = :both)
  src = @processed_source.buffer.source
  go_left = side == :left || side == :both
  go_right = side == :right || side == :both
  begin_pos = range.begin_pos
  begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
  end_pos = range.end_pos
  end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/
  end_pos += 1 if go_right && src[end_pos] == "\n"
  Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos)
end
source_range(source_buffer, preceding_lines, begin_column, column_count) click to toggle source
# File lib/rubocop/cop/util.rb, line 123
def source_range(source_buffer, preceding_lines, begin_column,
                 column_count)
  newline_length = 1
  begin_pos = preceding_lines.reduce(0) do |a, e|
    a + e.length + newline_length
  end + begin_column
  Parser::Source::Range.new(source_buffer, begin_pos,
                            begin_pos + column_count)
end
strip_quotes(str) click to toggle source
# File lib/rubocop/cop/util.rb, line 27
def strip_quotes(str)
  if str[0] == '"' || str[0] == "'"
    str[0] = ''
    str[-1] = ''
  else
    # we're dealing with %q or %Q
    str[0, 3] = ''
    str[-1] = ''
  end

  str
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.