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
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
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/rubocop/cop/util.rb', line 35
def add_parentheses(node, corrector)
if !node.respond_to?(:arguments)
corrector.wrap(node, '(', ')')
elsif node.arguments.empty?
corrector.insert_after(node, '()')
else
corrector.replace(args_begin(node), '(')
corrector.insert_after(args_end(node), ')')
end
end
|
.args_begin(node) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/rubocop/cop/util.rb', line 46
def args_begin(node)
loc = node.loc
selector =
node.super_type? || node.yield_type? ? loc.keyword : loc.selector
selector.end.resize(1)
end
|
.args_end(node) ⇒ Object
53
54
55
|
# File 'lib/rubocop/cop/util.rb', line 53
def args_end(node)
node.loc.expression.end
end
|
.begins_its_line?(range) ⇒ Boolean
66
67
68
|
# File 'lib/rubocop/cop/util.rb', line 66
def begins_its_line?(range)
range.source_line.index(/\S/) == range.column
end
|
17
18
19
|
# File 'lib/rubocop/cop/util.rb', line 17
def (line_source)
/^\s*#/.match?(line_source)
end
|
Deprecated.
Use ‘ProcessedSource#line_with_comment?`, `contains_comment?` or similar
22
23
24
|
# File 'lib/rubocop/cop/util.rb', line 22
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?
88
89
90
91
92
93
94
95
96
|
# File 'lib/rubocop/cop/util.rb', line 88
def double_quotes_required?(string)
/'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string)
end
|
.escape_string(string) ⇒ Object
102
103
104
|
# File 'lib/rubocop/cop/util.rb', line 102
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.
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/rubocop/cop/util.rb', line 72
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) ⇒ Object
128
129
130
|
# File 'lib/rubocop/cop/util.rb', line 128
def indent(node)
' ' * node.loc.column
end
|
.interpret_string_escapes(string) ⇒ Object
118
119
120
|
# File 'lib/rubocop/cop/util.rb', line 118
def interpret_string_escapes(string)
StringInterpreter.interpret(string)
end
|
.line_range(node) ⇒ Object
26
27
28
|
# File 'lib/rubocop/cop/util.rb', line 26
def line_range(node)
node.first_line..node.last_line
end
|
.needs_escaping?(string) ⇒ Boolean
98
99
100
|
# File 'lib/rubocop/cop/util.rb', line 98
def needs_escaping?(string)
double_quotes_required?(escape_string(string))
end
|
.on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/rubocop/cop/util.rb', line 57
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
30
31
32
33
|
# File 'lib/rubocop/cop/util.rb', line 30
def parentheses?(node)
node.loc.respond_to?(:end) && node.loc.end &&
node.loc.end.is?(')')
end
|
.same_line?(node1, node2) ⇒ Boolean
122
123
124
125
126
|
# File 'lib/rubocop/cop/util.rb', line 122
def same_line?(node1, node2)
node1.respond_to?(:loc) &&
node2.respond_to?(:loc) &&
node1.loc.line == node2.loc.line
end
|
.to_string_literal(string) ⇒ Object
106
107
108
109
110
111
112
|
# File 'lib/rubocop/cop/util.rb', line 106
def to_string_literal(string)
if needs_escaping?(string) && compatible_external_encoding_for?(string)
string.inspect
else
"'#{string.gsub('\\') { '\\\\' }}'"
end
end
|
.to_supported_styles(enforced_style) ⇒ Object
132
133
134
135
136
|
# File 'lib/rubocop/cop/util.rb', line 132
def to_supported_styles(enforced_style)
enforced_style
.sub(/^Enforced/, 'Supported')
.sub('Style', 'Styles')
end
|
.trim_string_interporation_escape_character(str) ⇒ Object
114
115
116
|
# File 'lib/rubocop/cop/util.rb', line 114
def trim_string_interporation_escape_character(str)
str.gsub(/\\\#\{(.*?)\}/) { "\#{#{Regexp.last_match(1)}}" }
end
|