Sha256: dac731a18032a31accc38e55d47889cfd3bc147f9fb4f72795161bbc62548b6f
Contents?: true
Size: 1.28 KB
Versions: 1
Compression:
Stored size: 1.28 KB
Contents
# encoding: utf-8 module Rubocop module Cop module Style # Checks for uses of the character literal ?x. class CharacterLiteral < Cop MSG = 'Do not use the character literal - use string literal instead.' def on_str(node) # Constants like __FILE__ are handled as strings, # but don't respond to begin. return unless node.loc.respond_to?(:begin) return if part_of_ignored_node?(node) # we don't register an offence for things like ?\C-\M-d if node.loc.begin.is?('?') && node.loc.expression.source.size.between?(2, 3) add_offence(:convention, node.loc.expression, MSG) do_autocorrect(node) end end alias_method :on_dstr, :ignore_node alias_method :on_regexp, :ignore_node def autocorrect_action(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 end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.10.0 | lib/rubocop/cop/style/character_literal.rb |