Class: Rubocop::Cop::Style::CharacterLiteral
- Includes:
- StringHelp
- Defined in:
- lib/rubocop/cop/style/character_literal.rb
Overview
Checks for uses of the character literal ?x.
Constant Summary collapse
- MSG =
'Do not use the character literal - use string literal instead.'
Constants inherited from Cop
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offences, #processed_source
Instance Method Summary collapse
Methods included from StringHelp
Methods inherited from Cop
#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#autocorrect(node) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rubocop/cop/style/character_literal.rb', line 18 def autocorrect(node) @corrections << lambda do |corrector| string = node.loc.expression.source[1..-1] if string.length == 1 # normal character corrector.replace(node.loc.expression, "'#{string}'") elsif string.length == 2 # special character like \n corrector.replace(node.loc.expression, %Q("#{string}")) end end end |
#offence?(node) ⇒ Boolean
12 13 14 15 16 |
# File 'lib/rubocop/cop/style/character_literal.rb', line 12 def offence?(node) # we don't register an offence for things like ?\C-\M-d node.loc.begin.is?('?') && node.loc.expression.source.size.between?(2, 3) end |