Module: RuboCop::AST::Ext::Range
| Relationships & Source Files | |
| Defined in: | lib/rubocop/ast/ext/range.rb |
Overview
Extensions to Parser::AST::Range
Constant Summary
-
LINE_BEGINS_REGEX_CACHE =
private
# File 'lib/rubocop/ast/ext/range.rb', line 40Hash.new do |hash, index| hash[index] = /^\s{#{index}}\S/ if index <= MAX_LINE_BEGINS_REGEX_INDEX end
-
MAX_LINE_BEGINS_REGEX_INDEX =
private
# File 'lib/rubocop/ast/ext/range.rb', line 39
Arbitrarily chosen value, should be enough to cover the most nested source code in real world projects.
50
Instance Attribute Summary
- #begins_its_line? ⇒ Boolean readonly
Instance Method Summary
-
#line_span(exclude_end: false) ⇒ Range
If
exclude_endistrue, then the range will be exclusive. - #same_line?(other) ⇒ Boolean
Instance Attribute Details
#begins_its_line? ⇒ Boolean (readonly)
# 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,
:
]
node.loc.begin.line_span # => 1..1
node.source_range.line_span(exclude_end: true) # => 1...4
# 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
# 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