Sha256: d969a8817993189d85c5a02394275aa4fb1ff818a4b498886fc714a7c8d7e863

Contents?: true

Size: 997 Bytes

Versions: 15

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Help methods for determining node visibility.
    module VisibilityHelp
      extend NodePattern::Macros

      VISIBILITY_SCOPES = %i[private protected public].freeze

      private

      def node_visibility(node)
        scope = find_visibility_start(node)
        scope&.method_name || :public
      end

      def find_visibility_start(node)
        node.left_siblings
            .reverse
            .find(&method(:visibility_block?))
      end

      # Navigate to find the last protected method
      def find_visibility_end(node)
        possible_visibilities = VISIBILITY_SCOPES - [node_visibility(node)]
        right = node.right_siblings
        right.find do |child_node|
          possible_visibilities.include?(node_visibility(child_node))
        end || right.last
      end

      def_node_matcher :visibility_block?, <<~PATTERN
        (send nil? { :private :protected :public })
      PATTERN
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/visibility_help.rb
rubocop-1.3.1 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-1.3.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-1.2.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-1.1.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-1.0.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-0.93.1 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-0.93.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-0.92.0 lib/rubocop/cop/mixin/visibility_help.rb
rubocop-0.91.1 lib/rubocop/cop/mixin/visibility_help.rb