Sha256: c1b3d9a025e181a2183e9c77822d80c8f46968fe1a962f2747097199c28f04dd

Contents?: true

Size: 1.69 KB

Versions: 9

Compression:

Stored size: 1.69 KB

Contents

module RuboCop
  module Cop
    module Ezcater
      # Enforce use of `mock_feature_flag` helper instead of mocking `FeatureFlag.is_active?` directly.
      #
      # @example
      #
      #   # good
      #   mock_feature_flag("MyFeatureFlag", true)
      #   mock_feature_flag("MyFeatureFlag", { user: current_user }, true)
      #
      #   # bad
      #   allow(FeatureFlag).to receive(:is_active?).and_return(true)
      #   allow(FeatureFlag).to receive(:is_active?).with("MyFeatureFlag").and_return(true)
      #   allow(FeatureFlag).to receive(:is_active?).with("MyFeatureFlag", user: current_user).and_return(true)
      class RspecRequireFeatureFlagMock < Cop
        MSG = "Use the `mock_feature_flag` helper instead of mocking `allow(FeatureFlag)`".freeze

        def_node_matcher :feature_flag_const?, <<~PATTERN
          (const _ :FeatureFlag)
        PATTERN

        def on_const(node)
          return unless feature_flag_const?(node)

          # Walk to send node where method = :allow
          match_node = node
          match_node = match_node.parent while not_allow_send_node?(match_node.parent)
          match_node = match_node.parent

          # Validate send node, method = :allow; could have never been found
          return unless allow_send_node?(match_node)

          # Finish tree navigation to full line for highlighting
          match_node = match_node.parent while match_node.parent
          add_offense(match_node, :expression)
        end

        private

        def not_allow_send_node?(node)
          node && !allow_send_node?(node)
        end

        def allow_send_node?(node)
          node&.send_type? && node.method_name == :allow
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ezcater_rubocop-0.51.0 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.5 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.4 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.3 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.2 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.2.rc1 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.2.rc0 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.1 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb
ezcater_rubocop-0.50.0 lib/rubocop/cop/ezcater/rspec_require_feature_flag_mock.rb