Sha256: 7f2dc96b6834cc417532850d8bc73d65d5d10f9b1f6fb5102a0e271e9c1252b6

Contents?: true

Size: 1.55 KB

Versions: 182

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for `:true` and `:false` symbols.
      # In most cases it would be a typo.
      #
      # @safety
      #   Autocorrection is unsafe for this cop because code relying
      #   on `:true` or `:false` symbols will break when those are
      #   changed to actual booleans.
      #
      # @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

182 entries across 175 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/boolean_symbol.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.65.0 lib/rubocop/cop/lint/boolean_symbol.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/boolean_symbol.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/boolean_symbol.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/boolean_symbol.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.64.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.63.4 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.63.3 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.63.2 lib/rubocop/cop/lint/boolean_symbol.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.63.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.63.0 lib/rubocop/cop/lint/boolean_symbol.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.62.1 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.62.0 lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.61.0 lib/rubocop/cop/lint/boolean_symbol.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/boolean_symbol.rb
rubocop-1.60.2 lib/rubocop/cop/lint/boolean_symbol.rb