Module: RuboCop::Cop::Util
Overview
This module contains a collection of useful utility methods.
Constant Summary
collapse
- PROC_NEW_NODE =
s(:send, s(:const, nil, :Proc), :new)
- EQUALS_ASGN_NODES =
[:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn, :masgn]
- SHORTHAND_ASGN_NODES =
[:op_asgn, :or_asgn, :and_asgn]
- ASGN_NODES =
EQUALS_ASGN_NODES + SHORTHAND_ASGN_NODES
- OPERATOR_METHODS =
%w(
| ^ & <=> == === =~ > >= < <= << >>
+ - * / % ** ~ +@ -@ [] []= ! != !~
).map(&:to_sym) + [:'`']
Class Method Summary
collapse
Methods included from PathUtil
match_path?, relative_path
Class Method Details
.block_length(block_node) ⇒ Object
41
42
43
|
# File 'lib/rubocop/cop/util.rb', line 41
def block_length(block_node)
block_node.loc.end.line - block_node.loc.begin.line
end
|
.command?(name, node) ⇒ Boolean
80
81
82
83
84
85
86
87
|
# File 'lib/rubocop/cop/util.rb', line 80
def command?(name, node)
return unless node.type == :send
receiver, method_name, _args = *node
!receiver && method_name == name
end
|
45
46
47
|
# File 'lib/rubocop/cop/util.rb', line 45
def (line_source)
line_source =~ /^\s*#/
end
|
.const_name(node) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/rubocop/cop/util.rb', line 62
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) ⇒ Object
Returns, for example, a bare if
node if the given node is an if
with calls chained to the end of it.
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/rubocop/cop/util.rb', line 158
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) ⇒ Boolean
89
90
91
92
93
94
95
|
# File 'lib/rubocop/cop/util.rb', line 89
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) ⇒ Boolean
105
106
107
|
# File 'lib/rubocop/cop/util.rb', line 105
def lambda_or_proc?(node)
lambda?(node) || proc?(node)
end
|
.line_range(arg) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rubocop/cop/util.rb', line 49
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
|
.numeric_range_size(range) ⇒ Object
Range#size is not avaialable prior to Ruby 2.0.
175
176
177
178
179
180
|
# File 'lib/rubocop/cop/util.rb', line 175
def numeric_range_size(range)
size = range.end - range.begin
size += 1 unless range.exclude_end?
size = 0 if size < 0
size
end
|
.on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/rubocop/cop/util.rb', line 113
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) ⇒ Boolean
24
25
26
|
# File 'lib/rubocop/cop/util.rb', line 24
def operator?(symbol)
OPERATOR_METHODS.include?(symbol)
end
|
.parentheses?(node) ⇒ Boolean
109
110
111
|
# File 'lib/rubocop/cop/util.rb', line 109
def parentheses?(node)
node.loc.respond_to?(:end) && node.loc.end
end
|
.proc?(node) ⇒ Boolean
97
98
99
100
101
102
103
|
# File 'lib/rubocop/cop/util.rb', line 97
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) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/rubocop/cop/util.rb', line 144
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, line_number, column, length = 1) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rubocop/cop/util.rb', line 124
def source_range(source_buffer, line_number, column, length = 1)
if column.is_a?(Range)
column_index = column.begin
length = numeric_range_size(column)
else
column_index = column
end
preceding_line_numbers = (1...line_number)
line_begin_pos = preceding_line_numbers.reduce(0) do |pos, line|
pos + source_buffer.source_line(line).length + 1
end
begin_pos = line_begin_pos + column_index
end_pos = begin_pos + length
Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
end
|
.strip_quotes(str) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/rubocop/cop/util.rb', line 28
def strip_quotes(str)
if str[0] == '"' || str[0] == "'"
str[0] = ''
str[-1] = ''
else
str[0, 3] = ''
str[-1] = ''
end
str
end
|