Class: RuboCop::Cop::Style::CharacterLiteral
- 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.
Constant Summary collapse
- MSG =
'Do not use the character literal - use string literal instead.'
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #autocorrect(corrector, node) ⇒ Object
-
#correct_style_detected ⇒ Object
Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.
- #offense?(node) ⇒ Boolean
-
#opposite_style_detected ⇒ Object
Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.
Methods included from AutoCorrector
Methods included from RuboCop::Cop::StringHelp
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
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
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_detected ⇒ Object
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
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_detected ⇒ Object
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 |