-
Notifications
You must be signed in to change notification settings - Fork 8
/
range_help.rb
52 lines (44 loc) · 1.4 KB
/
range_help.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
module RuboCop
module Cop
# Methods that calculate and return Parser::Source::Ranges
module RangeHelp
private
def range_with_receiver(node)
receiver = node.receiver
loc_begin = if receiver.send_type?
receiver.loc.selector.begin_pos
else
receiver.loc.expression.begin_pos
end
loc_end = node.loc.selector.end_pos
range_between(loc_begin, loc_end)
end
def string_contents_range(node)
begin_pos = node.loc.begin.end_pos
end_pos = node.loc.end.begin_pos
range_between(begin_pos, end_pos)
end
def arguments_range(node)
begin_pos = node.arguments.first.loc.expression.begin_pos
end_pos = node.arguments.last.loc.expression.end_pos
range_between(begin_pos, end_pos)
end
def conditional_range(node)
if node.modifier_form?
range_between(node.loc.keyword.begin_pos,
node.loc.expression.end_pos)
else
:expression
end
end
def file_ext_range(argument_node)
filename = argument_node.str_content
ext_size = File.extname(filename).size
end_pos = argument_node.loc.end.begin_pos
begin_pos = end_pos - ext_size
range_between(begin_pos, end_pos)
end
end
end
end