Module: RuboCop::Cop::Util
Overview
This module contains a collection of useful utility methods.
Constant Summary
collapse
- LITERAL_REGEX =
Match literal regex characters, not including anchors, character classes, alternatives, groups, repetitions, references, etc
%r{[\w\s\-,"'!#%&<>=;:`~/]|\\[^AbBdDgGhHkpPRwWXsSzZ0-9]}.freeze
Class Method Summary
collapse
-
.add_parentheses(node, corrector) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
.args_begin(node) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
-
.args_end(node) ⇒ Object
-
.begins_its_line?(range) ⇒ Boolean
-
.comment_line?(line_source) ⇒ Boolean
-
.comment_lines?(node) ⇒ Boolean
deprecated
Deprecated.
Use ‘ProcessedSource#line_with_comment?`, `contains_comment?` or similar
-
.double_quotes_required?(string) ⇒ Boolean
If converting a string to Ruby string literal source code, must double quotes be used?.
-
.escape_string(string) ⇒ Object
-
.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.
-
.indent(node, offset: 0) ⇒ Object
-
.interpret_string_escapes(string) ⇒ Object
-
.line(node_or_range) ⇒ Object
-
.line_range(node) ⇒ Object
-
.needs_escaping?(string) ⇒ Boolean
-
.on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object
-
.parentheses?(node) ⇒ Boolean
-
.same_line?(node1, node2) ⇒ Boolean
-
.to_string_literal(string) ⇒ Object
-
.to_supported_styles(enforced_style) ⇒ Object
-
.trim_string_interporation_escape_character(str) ⇒ Object
Methods included from PathUtil
absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path
Class Method Details
.add_parentheses(node, corrector) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rubocop/cop/util.rb', line 34
def add_parentheses(node, corrector)
if node.args_type?
arguments_range = node.source_range
args_with_space = range_with_surrounding_space(range: arguments_range, side: :left)
leading_space = range_between(args_with_space.begin_pos, arguments_range.begin_pos)
corrector.replace(leading_space, '(')
corrector.insert_after(arguments_range, ')')
elsif !node.respond_to?(:arguments)
corrector.wrap(node, '(', ')')
elsif node.arguments.empty?
corrector.insert_after(node, '()')
else
args_begin = args_begin(node)
corrector.remove(args_begin)
corrector.insert_before(args_begin, '(')
corrector.insert_after(args_end(node), ')')
end
end
|
.args_begin(node) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/rubocop/cop/util.rb', line 55
def args_begin(node)
loc = node.loc
selector = if node.super_type? || node.yield_type?
loc.keyword
elsif node.def_type? || node.defs_type?
loc.name
else
loc.selector
end
selector.end.resize(1)
end
|
.args_end(node) ⇒ Object
67
68
69
|
# File 'lib/rubocop/cop/util.rb', line 67
def args_end(node)
node.loc.expression.end
end
|
.begins_its_line?(range) ⇒ Boolean
80
81
82
|
# File 'lib/rubocop/cop/util.rb', line 80
def begins_its_line?(range)
range.source_line.index(/\S/) == range.column
end
|
16
17
18
|
# File 'lib/rubocop/cop/util.rb', line 16
def (line_source)
/^\s*#/.match?(line_source)
end
|
Deprecated.
Use ‘ProcessedSource#line_with_comment?`, `contains_comment?` or similar
21
22
23
|
# File 'lib/rubocop/cop/util.rb', line 21
def (node)
processed_source[line_range(node)].any? { |line| (line) }
end
|
.double_quotes_required?(string) ⇒ Boolean
If converting a string to Ruby string literal source code, must double quotes be used?
102
103
104
105
106
107
108
109
110
|
# File 'lib/rubocop/cop/util.rb', line 102
def double_quotes_required?(string)
/'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string)
end
|
.escape_string(string) ⇒ Object
116
117
118
|
# File 'lib/rubocop/cop/util.rb', line 116
def escape_string(string)
string.inspect[1..-2].tap { |s| s.gsub!(/\\"/, '"') }
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.
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/rubocop/cop/util.rb', line 86
def first_part_of_call_chain(node)
while node
case node.type
when :send
node = node.receiver
when :block
node = node.send_node
else
break
end
end
node
end
|
.indent(node, offset: 0) ⇒ Object
151
152
153
|
# File 'lib/rubocop/cop/util.rb', line 151
def indent(node, offset: 0)
' ' * (node.loc.column + offset)
end
|
.interpret_string_escapes(string) ⇒ Object
133
134
135
|
# File 'lib/rubocop/cop/util.rb', line 133
def interpret_string_escapes(string)
StringInterpreter.interpret(string)
end
|
.line(node_or_range) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/rubocop/cop/util.rb', line 137
def line(node_or_range)
if node_or_range.respond_to?(:line)
node_or_range.line
elsif node_or_range.respond_to?(:loc)
node_or_range.loc.line
end
end
|
.line_range(node) ⇒ Object
25
26
27
|
# File 'lib/rubocop/cop/util.rb', line 25
def line_range(node)
node.first_line..node.last_line
end
|
.needs_escaping?(string) ⇒ Boolean
112
113
114
|
# File 'lib/rubocop/cop/util.rb', line 112
def needs_escaping?(string)
double_quotes_required?(escape_string(string))
end
|
.on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/rubocop/cop/util.rb', line 71
def on_node(syms, sexp, excludes = [], &block)
return to_enum(:on_node, syms, sexp, excludes) unless block
yield sexp if Array(syms).include?(sexp.type)
return if Array(excludes).include?(sexp.type)
sexp.each_child_node { |elem| on_node(syms, elem, excludes, &block) }
end
|
.parentheses?(node) ⇒ Boolean
29
30
31
|
# File 'lib/rubocop/cop/util.rb', line 29
def parentheses?(node)
node.loc.respond_to?(:end) && node.loc.end && node.loc.end.is?(')')
end
|
.same_line?(node1, node2) ⇒ Boolean
145
146
147
148
149
|
# File 'lib/rubocop/cop/util.rb', line 145
def same_line?(node1, node2)
line1 = line(node1)
line2 = line(node2)
line1 && line2 && line1 == line2
end
|
.to_string_literal(string) ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/rubocop/cop/util.rb', line 120
def to_string_literal(string)
if needs_escaping?(string) && compatible_external_encoding_for?(string)
string.inspect
else
"'#{string.gsub('\"', '"').gsub('\\') { '\\\\' }}'"
end
end
|
.to_supported_styles(enforced_style) ⇒ Object
155
156
157
|
# File 'lib/rubocop/cop/util.rb', line 155
def to_supported_styles(enforced_style)
enforced_style.sub(/^Enforced/, 'Supported').sub('Style', 'Styles')
end
|
.trim_string_interporation_escape_character(str) ⇒ Object
129
130
131
|
# File 'lib/rubocop/cop/util.rb', line 129
def trim_string_interporation_escape_character(str)
str.gsub(/\\\#\{(.*?)\}/) { "\#{#{Regexp.last_match(1)}}" }
end
|