Sha256: 322781dcb2789fa66b0c51f1a5a674637978a9b997a3bff0576936169a47bdad

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for the use of a method, the result of which
      # would be a literal, like an empty array, hash or string.
      class EmptyLiteral < Cop
        ARR_MSG = 'Use array literal [] instead of Array.new.'
        HASH_MSG = 'Use hash literal {} instead of Hash.new.'
        STR_MSG = "Use string literal '' instead of String.new."

        # Empty array node
        #
        # (send
        #   (const nil :Array) :new)
        ARRAY_NODE = s(:send, s(:const, nil, :Array), :new)

        # Empty hash node
        #
        # (send
        #   (const nil :Hash) :new)
        HASH_NODE = s(:send, s(:const, nil, :Hash), :new)

        # Empty string node
        #
        # (send
        #   (const nil :String) :new)
        STR_NODE = s(:send, s(:const, nil, :String), :new)

        def on_send(node)
          return if part_of_ignored_node?(node)

          case node
          when ARRAY_NODE
            add_offense(node, :expression, ARR_MSG)
          when HASH_NODE
            add_offense(node, :expression, HASH_MSG)
          when STR_NODE
            add_offense(node, :expression, STR_MSG)
          end
        end

        # TODO: Check block contents as well.
        alias_method :on_block, :ignore_node

        def autocorrect(node)
          @corrections << lambda do |corrector|
            name = case node
                   when ARRAY_NODE then '[]'
                   when HASH_NODE then '{}'
                   when STR_NODE then "''"
                   end
            corrector.replace(node.loc.expression, name)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/empty_literal.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/empty_literal.rb
rubocop-0.27.0 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.26.1 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.26.0 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.25.0 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.24.1 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.24.0 lib/rubocop/cop/style/empty_literal.rb
rubocop-0.23.0 lib/rubocop/cop/style/empty_literal.rb