Sha256: 1a4617e7ace4c710e75737d23cd101db1644f0fc0c558b5d701ac6d3218418fa

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 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?
              corrector.remove(parent.loc.operator)
              boolean_literal = "#{node.source} =>"
            end
            corrector.replace(node.loc.expression, boolean_literal)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.81.0 lib/rubocop/cop/lint/boolean_symbol.rb