Sha256: 5c0eccb5a696890b8f274ec2f1a651729ac1bf75ebb58fdcc109c68ecce76979

Contents?: true

Size: 813 Bytes

Versions: 2

Compression:

Stored size: 813 Bytes

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks symbol literal syntax.
      #
      # @example
      #
      #   # bad
      #   :"symbol"
      #
      #   # good
      #   :symbol
      class SymbolLiteral < Cop
        MSG = 'Do not use strings for word-like symbol literals.'

        def on_sym(node)
          sym_name = node.loc.expression.source

          return unless sym_name =~ /\A:["'][A-Za-z_]\w*["']\z/

          add_offense(node, :expression)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            current_name = node.loc.expression.source
            corrector.replace(node.loc.expression,
                              current_name.gsub(/["']/, ''))
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/symbol_literal.rb
rubocop-0.30.0 lib/rubocop/cop/style/symbol_literal.rb