Class: RuboCop::Cop::Style::CharacterLiteral

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RuboCop::Cop::StringHelp
Defined in:
lib/rubocop/cop/style/character_literal.rb

Overview

Checks for uses of the character literal ?x. Starting with Ruby 1.9 character literals are essentially one-character strings, so this syntax is mostly redundant at this point.

? character literal can be used to express meta and control character. That’s a good use case of ? literal so it doesn’t count it as an offense.

Examples:

# bad
?x

# good
'x'

# good - control & meta escapes
?\C-\M-d
"\C-\M-d" # same as above

Constant Summary collapse

MSG =
'Do not use the character literal - use string literal instead.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from RuboCop::Cop::StringHelp

#on_regexp, #on_str

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#autocorrect(corrector, node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/style/character_literal.rb', line 35

def autocorrect(corrector, node)
  string = node.source[1..]

  # special character like \n
  # or ' which needs to use "" or be escaped.
  if string.length == 2 || string == "'"
    corrector.replace(node, %("#{string}"))
  elsif string.length == 1 # normal character
    corrector.replace(node, "'#{string}'")
  end
end

#correct_style_detectedObject

Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.



53
# File 'lib/rubocop/cop/style/character_literal.rb', line 53

def correct_style_detected; end

#offense?(node) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/rubocop/cop/style/character_literal.rb', line 30

def offense?(node)
  # we don't register an offense for things like ?\C-\M-d
  node.character_literal? && node.source.size.between?(2, 3)
end

#opposite_style_detectedObject

Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.



49
# File 'lib/rubocop/cop/style/character_literal.rb', line 49

def opposite_style_detected; end