Sha256: 5a744437c33a905bfa0ea8f44b9ef66ad82b3124aa04022cccbdd79e1db1b7c0

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Capybara
      # Checks for redundant `within find(...)` calls.
      #
      # @example
      #   # bad
      #   within find('foo.bar') do
      #     # ...
      #   end
      #
      #   # good
      #   within 'foo.bar' do
      #     # ...
      #   end
      #
      #   # bad
      #   within find_by_id('foo') do
      #     # ...
      #   end
      #
      #   # good
      #   within '#foo' do
      #     # ...
      #   end
      #
      class RedundantWithinFind < ::RuboCop::Cop::Base
        extend AutoCorrector
        MSG = 'Redundant `within %<method>s(...)` call detected.'
        RESTRICT_ON_SEND = %i[within].freeze
        FIND_METHODS = Set.new(%i[find find_by_id]).freeze

        # @!method within_find(node)
        def_node_matcher :within_find, <<~PATTERN
          (send nil? :within
            $(send nil? %FIND_METHODS ...))
        PATTERN

        def on_send(node)
          within_find(node) do |find_node|
            add_offense(find_node, message: msg(find_node)) do |corrector|
              corrector.replace(find_node, replaced(find_node))
            end
          end
        end

        private

        def msg(node)
          format(MSG, method: node.method_name)
        end

        def replaced(node)
          replaced = node.arguments.map(&:source).join(', ')
          if node.method?(:find_by_id)
            replaced.sub(/\A(["'])/, '\1#')
          else
            replaced
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 4 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.21.0/lib/rubocop/cop/capybara/redundant_within_find.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/lib/rubocop/cop/capybara/redundant_within_find.rb
rubocop-capybara-2.21.0 lib/rubocop/cop/capybara/redundant_within_find.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.20.0/lib/rubocop/cop/capybara/redundant_within_find.rb
rubocop-capybara-2.20.0 lib/rubocop/cop/capybara/redundant_within_find.rb