class RuboCop::Cop::Style::StringLiterals

Checks if uses of quotes match the configured preference.

Constants

MSG_INCONSISTENT

Public Instance Methods

on_dstr(node) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 13
def on_dstr(node)
  # Strings which are continued across multiple lines using \
  # are parsed as a `dstr` node with `str` children
  # If one part of that continued string contains interpolations,
  # then it will be parsed as a nested `dstr` node
  return unless consistent_multiline?
  return if node.loc.is_a?(Parser::Source::Map::Heredoc)

  children = node.children
  return unless children.all? { |c| c.str_type? || c.dstr_type? }

  quote_styles = children.map { |c| c.loc.begin.source }.uniq
  if quote_styles.size > 1
    add_offense(node, :expression, MSG_INCONSISTENT)
  else
    check_multiline_quote_style(node, quote_styles[0])
  end

  ignore_node(node)
end

Private Instance Methods

check_multiline_quote_style(node, quote) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 58
def check_multiline_quote_style(node, quote)
  range = node.source_range
  children = node.children
  if quote == "'" && style == :double_quotes
    add_offense(node, range) if children.all? { |c| wrong_quotes?(c) }
  elsif quote == '"' && style == :single_quotes
    if children.none?(&:dstr_type?) &&
       children.none? { |c| double_quotes_acceptable?(c.str_content) }
      add_offense(node, range)
    end
  end
end
consistent_multiline?() click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 54
def consistent_multiline?
  cop_config['ConsistentQuotesInMultiline']
end
message(*) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 36
def message(*)
  if style == :single_quotes
    "Prefer single-quoted strings when you don't need string "              'interpolation or special symbols.'
  else
    'Prefer double-quoted strings unless you need single quotes to '              'avoid extra backslashes for escaping.'
  end
end
offense?(node) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 46
def offense?(node)
  # If it's a string within an interpolation, then it's not an offense
  # for this cop.
  return false if inside_interpolation?(node)

  wrong_quotes?(node)
end