Sha256: 6918403dd63cb3ee5b67ff0a2f4a1195f3d78e7cd7464fb644acc3b28ae82239

Contents?: true

Size: 1.17 KB

Versions: 14

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for `:true` and `:false` symbols.
      # In most cases it would be a typo.
      #
      # @example
      #
      #   # bad
      #   :true
      #
      #   # good
      #   true
      #
      # @example
      #
      #   # bad
      #   :false
      #
      #   # good
      #   false
      class BooleanSymbol < Cop
        MSG = 'Symbol with a boolean name - ' \
              'you probably meant to use `%<boolean>s`.'

        def_node_matcher :boolean_symbol?, '(sym {:true :false})'

        def on_sym(node)
          return unless boolean_symbol?(node)

          add_offense(node, message: format(MSG, boolean: node.value))
        end

        def autocorrect(node)
          lambda do |corrector|
            boolean_literal = node.source.delete(':')
            parent = node.parent
            if parent&.pair_type? && node.equal?(parent.children[0])
              corrector.remove(parent.loc.operator)
              boolean_literal = "#{node.source} =>"
            end
            corrector.replace(node, boolean_literal)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/lint/boolean_symbol.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.87.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.87.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.86.0 lib/rubocop/cop/lint/boolean_symbol.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/boolean_symbol.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/lint/boolean_symbol.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.85.1 lib/rubocop/cop/lint/boolean_symbol.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.85.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.84.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.83.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-0.82.0 lib/rubocop/cop/lint/boolean_symbol.rb