Module: RuboCop::Cop::Util
- Extended by:
- Sexp
- Includes:
- PathUtil
- Included in:
- Cop, Lint::UnusedBlockArgument
- Defined in:
- lib/rubocop/cop/util.rb
Overview
This module contains a collection of useful utility methods.
Constant Summary collapse
- BYTE_ORDER_MARK =
The Unicode codepoint
0xfeff
- EQUALS_ASGN_NODES =
[:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn, :masgn].freeze
- SHORTHAND_ASGN_NODES =
[:op_asgn, :or_asgn, :and_asgn].freeze
- ASGN_NODES =
(EQUALS_ASGN_NODES + SHORTHAND_ASGN_NODES).freeze
- OPERATOR_METHODS =
http://phrogz.net/programmingruby/language.html#table_18.4 Backtick is added last just to help editors parse this code.
%w( | ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ +@ -@ [] []= ! != !~ ).map(&:to_sym).push(:'`').freeze
- STRING_ESCAPES =
{ '\a' => "\a", '\b' => "\b", '\e' => "\e", '\f' => "\f", '\n' => "\n", '\r' => "\r", '\s' => ' ', '\t' => "\t", '\v' => "\v", "\\\n" => '' }.freeze
- STRING_ESCAPE_REGEX =
/\\(?: [abefnrstv\n] | # simple escapes (above) \d{1,3} | # octal byte escape x\d{1,2} | # hex byte escape u[0-9a-fA-F]{4} | # unicode char escape u\{[^}]*\} | # extended unicode escape . # any other escaped char )/x
- LITERAL_REGEX =
Match literal regex characters, not including anchors, character classes, alternatives, groups, repetitions, references, etc
/[\w\s\-,"'!#%&<>=;:`~]|\\[^AbBdDgGkwWszZS0-9]/
Class Method Summary collapse
- .begins_its_line?(range) ⇒ Boolean
- .block_length(block_node) ⇒ Object
- .comment_line?(line_source) ⇒ Boolean
- .directions(side) ⇒ Object
-
.double_quotes_acceptable?(string) ⇒ Boolean
If double quoted string literals are found in Ruby code, and they are not the preferred style, should they be flagged?.
-
.double_quotes_required?(string) ⇒ Boolean
If converting a string to Ruby string literal source code, must double quotes be used?.
-
.effective_column(range) ⇒ Object
Returns the column attribute of the range, except if the range is on the first line and there's a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.
- .ends_its_line?(range) ⇒ Boolean
-
.first_part_of_call_chain(node) ⇒ Object
Returns, for example, a bare
if
node if the given node is anif
with calls chained to the end of it. -
.interpret_string_escapes(string) ⇒ Object
Take a string with embedded escapes, and convert the escapes as the Ruby interpreter would when reading a double-quoted string literal.
- .line_range(arg) ⇒ Object
- .move_pos(src, pos, step, condition, regexp) ⇒ Object
-
.numeric_range_size(range) ⇒ Object
Range#size is not available prior to Ruby 2.0.
- .on_node(syms, sexp, excludes = []) {|sexp| ... } ⇒ Object
- .operator?(symbol) ⇒ Boolean
- .parentheses?(node) ⇒ Boolean
- .range_with_surrounding_comma(range, side = :both, buffer = nil) ⇒ Object
- .range_with_surrounding_space(range, side = :both, buffer = nil, with_newline = true) ⇒ Object
- .source_range(source_buffer, line_number, column, length = 1) ⇒ Object
- .strip_quotes(str) ⇒ Object
- .to_string_literal(string) ⇒ Object
- .to_symbol_literal(string) ⇒ Object
- .within_node?(inner, outer) ⇒ Boolean
Methods included from Sexp
Methods included from PathUtil
absolute?, hidden?, issue_deprecation_warning, match_path?, relative_path
Class Method Details
.begins_its_line?(range) ⇒ Boolean
176 177 178 |
# File 'lib/rubocop/cop/util.rb', line 176 def begins_its_line?(range) (range.source_line =~ /\S/) == range.column end |
.block_length(block_node) ⇒ Object
61 62 63 |
# File 'lib/rubocop/cop/util.rb', line 61 def block_length(block_node) block_node.loc.end.line - block_node.loc.begin.line end |
.comment_line?(line_source) ⇒ Boolean
65 66 67 |
# File 'lib/rubocop/cop/util.rb', line 65 def comment_line?(line_source) line_source =~ /^\s*#/ end |
.directions(side) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/rubocop/cop/util.rb', line 168 def directions(side) if side == :both [true, true] else [side == :left, side == :right] end end |
.double_quotes_acceptable?(string) ⇒ Boolean
If double quoted string literals are found in Ruby code, and they are not the preferred style, should they be flagged?
231 232 233 234 235 236 |
# File 'lib/rubocop/cop/util.rb', line 231 def double_quotes_acceptable?(string) # If a string literal contains hard-to-type characters which would # not appear on a "normal" keyboard, then double-quotes are acceptable double_quotes_required?(string) || string.codepoints.any? { |cp| cp < 32 || cp > 126 } end |
.double_quotes_required?(string) ⇒ Boolean
If converting a string to Ruby string literal source code, must double quotes be used?
219 220 221 222 223 224 225 226 227 |
# File 'lib/rubocop/cop/util.rb', line 219 def double_quotes_required?(string) # Double quotes are required for strings which either: # - Contain single quotes # - Contain non-printable characters, which must use an escape # Regex matches IF there is a ' or there is a \\ in the string that is # not preceded/followed by another \\ (e.g. "\\x34") but not "\\\\". string.inspect =~ /'|(?<! \\) \\{2}* \\ (?![\\"])/x end |
.effective_column(range) ⇒ Object
Returns the column attribute of the range, except if the range is on the first line and there's a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. This gives the column as it appears when viewing the file in an editor.
123 124 125 126 127 128 129 130 |
# File 'lib/rubocop/cop/util.rb', line 123 def effective_column(range) if range.line == 1 && @processed_source.raw_source.codepoints.first == BYTE_ORDER_MARK range.column - 1 else range.column end end |
.ends_its_line?(range) ⇒ Boolean
180 181 182 183 |
# File 'lib/rubocop/cop/util.rb', line 180 def ends_its_line?(range) line = range.source_buffer.source_line(range.last_line) (line =~ /\s*\z/) == range.last_column 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.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rubocop/cop/util.rb', line 193 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 |
.interpret_string_escapes(string) ⇒ Object
Take a string with embedded escapes, and convert the escapes as the Ruby interpreter would when reading a double-quoted string literal. For example, "\n" will be converted to "\n".
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rubocop/cop/util.rb', line 257 def interpret_string_escapes(string) # We currently don't handle \cx, \C-x, and \M-x string.gsub(STRING_ESCAPE_REGEX) do |escape| STRING_ESCAPES[escape] || begin if escape[1] == 'x' [escape[2..-1].hex].pack('C') elsif escape[1] == 'u' if escape[2] == '{' escape[3..-1].split(/\s+/).map(&:hex).pack('U') else [escape[2..-1].hex].pack('U') end elsif escape[1] =~ /\d/ # octal escape [escape[1..-1].to_i(8)].pack('C') else escape[1] # literal escaped char, like \\ end end end end |
.line_range(arg) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/util.rb', line 69 def line_range(arg) source_range = case arg when Parser::Source::Range arg when Parser::AST::Node arg.source_range else raise ArgumentError, "Invalid argument #{arg}" end source_range.begin.line..source_range.end.line end |
.move_pos(src, pos, step, condition, regexp) ⇒ Object
162 163 164 165 166 |
# File 'lib/rubocop/cop/util.rb', line 162 def move_pos(src, pos, step, condition, regexp) offset = step == -1 ? -1 : 0 pos += step while condition && src[pos + offset] =~ regexp pos end |
.numeric_range_size(range) ⇒ Object
Range#size is not available prior to Ruby 2.0.
210 211 212 213 214 215 |
# File 'lib/rubocop/cop/util.rb', line 210 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
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/cop/util.rb', line 87 def on_node(syms, sexp, excludes = [], &block) return to_enum(:on_node, syms, sexp, excludes) unless block_given? 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, &block) end end |
.operator?(symbol) ⇒ Boolean
45 46 47 |
# File 'lib/rubocop/cop/util.rb', line 45 def operator?(symbol) OPERATOR_METHODS.include?(symbol) end |
.parentheses?(node) ⇒ Boolean
82 83 84 85 |
# File 'lib/rubocop/cop/util.rb', line 82 def parentheses?(node) node.loc.respond_to?(:end) && node.loc.end && node.loc.end.is?(')'.freeze) end |
.range_with_surrounding_comma(range, side = :both, buffer = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rubocop/cop/util.rb', line 132 def range_with_surrounding_comma(range, side = :both, buffer = nil) buffer ||= @processed_source.buffer src = buffer.source go_left, go_right = directions(side) begin_pos = range.begin_pos end_pos = range.end_pos begin_pos = move_pos(src, begin_pos, -1, go_left, /,/) end_pos = move_pos(src, end_pos, 1, go_right, /,/) Parser::Source::Range.new(buffer, begin_pos, end_pos) end |
.range_with_surrounding_space(range, side = :both, buffer = nil, with_newline = true) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rubocop/cop/util.rb', line 146 def range_with_surrounding_space(range, side = :both, buffer = nil, with_newline = true) buffer ||= @processed_source.buffer src = buffer.source go_left, go_right = directions(side) begin_pos = range.begin_pos end_pos = range.end_pos begin_pos = move_pos(src, begin_pos, -1, go_left, /[ \t]/) begin_pos = move_pos(src, begin_pos, -1, go_left && with_newline, /\n/) end_pos = move_pos(src, end_pos, 1, go_right, /[ \t]/) end_pos = move_pos(src, end_pos, 1, go_right && with_newline, /\n/) Parser::Source::Range.new(buffer, begin_pos, end_pos) end |
.source_range(source_buffer, line_number, column, length = 1) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubocop/cop/util.rb', line 99 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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rubocop/cop/util.rb', line 49 def strip_quotes(str) if str[0] == '"' || str[0] == "'" str[0] = '' else # we're dealing with %q or %Q str[0, 3] = '' end str[-1] = '' str end |
.to_string_literal(string) ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/rubocop/cop/util.rb', line 238 def to_string_literal(string) if double_quotes_required?(string) string.inspect else "'#{string.gsub('\\') { '\\\\' }}'" end end |
.to_symbol_literal(string) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/rubocop/cop/util.rb', line 246 def to_symbol_literal(string) if string =~ /\s/ || double_quotes_required?(string) ":#{to_string_literal(string)}" else ":#{string}" end end |