123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Util

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: lib/rubocop/cop/util.rb

Overview

This module contains a collection of useful utility methods.

Constant Summary

::RuboCop::PathUtil - Included

HIDDEN_FILE_PATTERN, SMART_PATH_CACHE

Class Method Summary

Instance Method Summary

::RuboCop::PathUtil - Included

#absolute?

Returns true for an absolute Unix or Windows path.

#glob?

Returns true for a glob.

#hidden_dir?, #hidden_file?, #hidden_file_in_not_hidden_dir?,
#match_path?

Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.

#maybe_hidden_file?

Loose check to reduce memory allocations.

#relative_path, #smart_path

Class Method Details

.add_parentheses(node, corrector) (mod_func)

Metrics/MethodLength

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 39

def add_parentheses(node, corrector)
  if node.args_type?
    arguments_range = node.source_range
    args_with_space = range_with_surrounding_space(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

.any_descendant?(node, *types) ⇒ Boolean (mod_func)

Metrics/MethodLength

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 60

def any_descendant?(node, *types)
  if block_given?
    node.each_descendant(*types) do |descendant|
      return true if yield(descendant)
    end
  else
    # Use a block version to avoid allocating enumerators.
    node.each_descendant do # rubocop:disable Lint/UnreachableLoop
      return true
    end
  end

  false
end

.args_begin(node) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 75

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) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 87

def args_end(node)
  node.source_range.end
end

.begins_its_line?(range) ⇒ Boolean (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 108

def begins_its_line?(range)
  if (regex = LINE_BEGINS_REGEX_CACHE[range.column])
    range.source_line.match?(regex)
  else
    range.source_line.index(/\S/) == range.column
  end
end

.comment_line?(line_source) ⇒ Boolean (mod_func)

This is a bad API

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 17

def comment_line?(line_source)
  /^\s*#/.match?(line_source)
end

.comment_lines?(node) ⇒ Boolean (mod_func)

Deprecated.

Use ProcessedSource#line_with_comment?, contains_comment? or similar

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 22

def comment_lines?(node)
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `comment_lines?` is deprecated. Use `ProcessedSource#line_with_comment?`, `contains_comment?` or similar instead.
  WARNING

  processed_source[line_range(node)].any? { |line| comment_line?(line) }
end

.compatible_external_encoding_for?(src) ⇒ Boolean (private, mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 198

def compatible_external_encoding_for?(src)
  src = src.dup if RUBY_ENGINE == 'jruby'
  src.force_encoding(Encoding.default_external).valid_encoding?
end

.double_quotes_required?(string) ⇒ Boolean (mod_func)

If converting a string to Ruby string literal source code, must double quotes be used?

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 134

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 "\\\\".
  /'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string)
end

.escape_string(string) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 148

def escape_string(string)
  string.inspect[1..-2].tap { |s| s.gsub!('\\"', '"') }
end

.first_part_of_call_chain(node) (mod_func)

Returns, for example, a bare if node if the given node is an if with calls chained to the end of it.

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 118

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

.include_or_equal?(source, target) ⇒ Boolean (private, mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 203

def include_or_equal?(source, target)
  source == target || (source.is_a?(Array) && source.include?(target))
end

.indent(node, offset: 0) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 185

def indent(node, offset: 0)
  ' ' * (node.loc.column + offset)
end

.interpret_string_escapes(string) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 165

def interpret_string_escapes(string)
  StringInterpreter.interpret(string)
end

.line(node_or_range) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 169

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) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 30

def line_range(node)
  node.first_line..node.last_line
end

.needs_escaping?(string) ⇒ Boolean (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 144

def needs_escaping?(string)
  double_quotes_required?(escape_string(string))
end

.on_node(syms, sexp, excludes = []) {|sexp| ... } (mod_func)

Yields:

  • (sexp)
[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 91

def on_node(syms, sexp, excludes = [], &block)
  return to_enum(:on_node, syms, sexp, excludes) unless block

  yield sexp if include_or_equal?(syms, sexp.type)
  return if include_or_equal?(excludes, sexp.type)

  sexp.each_child_node { |elem| on_node(syms, elem, excludes, &block) }
end

.parentheses?(node) ⇒ Boolean (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 34

def parentheses?(node)
  node.loc.respond_to?(:end) && node.loc.end&.is?(')')
end

.same_line?(node1, node2) ⇒ Boolean (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 177

def same_line?(node1, node2)
  line1 = line(node1)
  line2 = line(node2)
  return false unless line1 && line2

  line1 == line2
end

.to_string_literal(string) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 152

def to_string_literal(string)
  if needs_escaping?(string) && compatible_external_encoding_for?(string)
    string.inspect
  else
    # In a single-quoted strings, double quotes don't need to be escaped
    "'#{string.gsub('\\') { '\\\\' }.gsub('\"', '"')}'"
  end
end

.to_supported_styles(enforced_style) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 191

def to_supported_styles(enforced_style)
  @to_supported_styles_cache[enforced_style] ||=
    enforced_style.sub(/^Enforced/, 'Supported').sub('Style', 'Styles')
end

.trim_string_interpolation_escape_character(str) (mod_func)

[ GitHub ]

  
# File 'lib/rubocop/cop/util.rb', line 161

def trim_string_interpolation_escape_character(str)
  str.gsub(/\\\#\{(.*?)\}/) { "\#{#{Regexp.last_match(1)}}" }
end