123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::AST::Ext::Range

Relationships & Source Files
Defined in: lib/rubocop/ast/ext/range.rb

Overview

Extensions to Parser::AST::Range

Constant Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#begins_its_line?Boolean (readonly)

Returns:

  • (Boolean)

    whether this range begins its line, i.e. is preceded only by whitespace

[ GitHub ]

  
# File 'lib/rubocop/ast/ext/range.rb', line 47

def begins_its_line?
  if (regex = LINE_BEGINS_REGEX_CACHE[column])
    source_line.match?(regex)
  else
    source_line.index(/\S/) == column
  end
end

Instance Method Details

#line_span(exclude_end: false) ⇒ Range

If exclude_end is true, then the range will be exclusive.

Assume that node corresponds to the following array literal:

[
  :foo,
  :bar
]
node.loc.begin.line_span                       # => 1..1
node.source_range.line_span(exclude_end: true) # => 1...4

Returns:

  • (Range)

    the range of line numbers for the node

[ GitHub ]

  
# File 'lib/rubocop/ast/ext/range.rb', line 20

def line_span(exclude_end: false)
  ::Range.new(first_line, last_line, exclude_end)
end

#same_line?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

    whether this range starts on the same line as other

[ GitHub ]

  
# File 'lib/rubocop/ast/ext/range.rb', line 27

def same_line?(other)
  other_line = if other.respond_to?(:line)
                 other.line
               elsif other.respond_to?(:loc)
                 other.loc.line
               end

  !other_line.nil? && line == other_line
end