Sha256: 4d0617a30bcc2c51ed95f5fb406d808d0f096886c971c3fe4c980f0776dbf1ed

Contents?: true

Size: 1.84 KB

Versions: 15

Compression:

Stored size: 1.84 KB

Contents

# typed: strict

module RuboCop
  module Cop
    module PackwerkLite
      module Private
        extend T::Sig

        sig { params(node: RuboCop::AST::ConstNode).returns(T::Boolean) }
        def self.partial_const_reference?(node)
          # This is a bit whacky, but if I have a reference in the code like this: Foo::Bar::Baz.any_method, `on_const` will be called three times:
          # One with `Foo`, one with `Foo::Bar`, and one with `Foo::Bar::Baz`.
          # As far as I can tell, there is no way to direct Rubocop to only look at the full constant name.
          # In order to ensure we're only operating on fully constant names, I check the "right sibling" of the `node`, which is the portion of the AST
          # immediately following the node.
          # If that right sibling is `nil` OR it's a lowercase string, we assume that it's the full constant.
          # If the right sibling is a non-nil capitalized string, we assume it's a part of the constant, because by convention, constants
          # start with capital letters and methods start with lowercase letters.
          # RegularRateOfPay::Types::HourlyEarningWithDate
          right_sibling = node.right_sibling
          return false if right_sibling.nil?

          right_sibling.to_s[0].capitalize == right_sibling.to_s[0]
        end

        sig { params(constant_reference: ConstantResolver::ConstantReference, type: String).returns(T::Boolean) }
        def self.violation_in_package_todo_yml?(constant_reference, type: 'privacy')
          existing_violations = ParsePackwerk::PackageTodo.for(constant_reference.referencing_package).violations
          existing_violations.any? do |v|
            v.class_name == "::#{constant_reference.constant_name}" && (type == 'privacy' ? v.privacy? : v.dependency?)
          end
        end
      end

      private_constant :Private
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rubocop-packs-0.0.45 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.44 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.43 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.42 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.41 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.40 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.39 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.38 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.37 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.36 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.35 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.34 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.33 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.32 lib/rubocop/cop/packwerk_lite/private.rb
rubocop-packs-0.0.31 lib/rubocop/cop/packwerk_lite/private.rb