-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
module Ext | ||
# Extensions to Parser::AST::Range | ||
module Range | ||
# @return [Range] the range of line numbers for the node | ||
# 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.loc.expression.line_span(exclude_end: true) # => 1...4 | ||
def line_span(exclude_end: false) | ||
::Range.new(first_line, last_line, exclude_end) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
::Parser::Source::Range.include ::RuboCop::AST::Ext::Range |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::Ext::Range do | ||
let(:source) { <<~RUBY } | ||
[ | ||
1, | ||
2 | ||
] | ||
RUBY | ||
|
||
let(:node) { parse_source(source).ast } | ||
|
||
describe '#line_span' do | ||
it 'returns the range of lines a range occupies' do | ||
expect(node.loc.begin.line_span).to eq 1..1 | ||
end | ||
|
||
it 'accepts an `exclude_end` keyword argument' do | ||
expect(node.loc.expression.line_span(exclude_end: true)).to eq 1...4 | ||
end | ||
end | ||
end |