Sha256: d9ab39ed2bf7104c4a299235c515d0538bc69a062777655d7888880c2812fac3

Contents?: true

Size: 1.36 KB

Versions: 17

Compression:

Stored size: 1.36 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 < Base
        extend AutoCorrector

        MSG = 'Symbol with a boolean name - you probably meant to use `%<boolean>s`.'

        # @!method boolean_symbol?(node)
        def_node_matcher :boolean_symbol?, '(sym {:true :false})'

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

          parent = node.parent
          return if parent&.array_type? && parent&.percent_literal?(:symbol)

          add_offense(node, message: format(MSG, boolean: node.value)) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          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

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.20.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.19.1 lib/rubocop/cop/lint/boolean_symbol.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.19.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.18.4 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.18.3 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.18.2 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.18.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.18.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.17.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.16.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.16.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.15.0 lib/rubocop/cop/lint/boolean_symbol.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.14.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.13.0 lib/rubocop/cop/lint/boolean_symbol.rb