Sha256: 3ca72c3551f5e9d83afc37a33a82603fd258329fadb3275309370c7b221229a7

Contents?: true

Size: 969 Bytes

Versions: 2

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module UmtsCustomCops
      # Prefer the easier-to-read be matcher for non-duplicable types.
      #
      # See the specs for examples.
      class BeMatcherForNonDuplicableTypes < Cop
        MSG = 'Prefer `be` matcher to `eq` or `eql` for non-duplicable types.'

        def_node_matcher :eq_on_non_duplicable_type?, <<-PATTERN
          (send
            _expectation {:to :not_to}
            (send
              _context {:eq :eql}
              {true false nil}
            )
          )
        PATTERN

        def autocorrect(node)
          lambda do |corrector|
            matcher = node.child_nodes[1]
            corrector.replace matcher.loc.selector, 'be'
          end
        end

        def on_send(node)
          return unless eq_on_non_duplicable_type? node

          add_offense node, location: :expression, message: MSG
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
umts-custom-cops-1.0.1 lib/umts-custom-cops/be_matcher_for_non_duplicable_types.rb
umts-custom-cops-1.0.0 lib/umts-custom-cops/be_matcher_for_non_duplicable_types.rb