From 8cc0e79f340a226fd236ca34ec6a958a1934b849 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Tue, 1 Nov 2022 12:48:24 -0400 Subject: [PATCH] Bump rubocop-packs to support pack based .rubocop.yml files (#41) * Remove deprecated guard * Bump rubocop-packs * Bump pp --- Gemfile.lock | 4 +- README.md | 2 +- .../namespaced_under_package_name.rb | 30 +++++------ package_protections.gemspec | 2 +- ...ks@0.0.11.rbi => rubocop-packs@0.0.14.rbi} | 52 +++++++++++++++++-- spec/package_protections_spec.rb | 4 +- 6 files changed, 67 insertions(+), 27 deletions(-) rename sorbet/rbi/gems/{rubocop-packs@0.0.11.rbi => rubocop-packs@0.0.14.rbi} (71%) diff --git a/Gemfile.lock b/Gemfile.lock index ba1d4d4..378c7ec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - package_protections (3.0.0) + package_protections (3.1.0) activesupport parse_packwerk rubocop @@ -68,7 +68,7 @@ GEM unicode-display_width (>= 1.4.0, < 3.0) rubocop-ast (1.21.0) parser (>= 3.1.1.0) - rubocop-packs (0.0.13) + rubocop-packs (0.0.14) activesupport parse_packwerk rubocop diff --git a/README.md b/README.md index 64094de..de2dd13 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ This protection only looks at files in `packs/your_pack/app` (it ignores spec fi This protection is implemented via Rubocop -- expect to see results for this when running `rubocop` however you normally do. To add to the TODO list, add to `.rubocop_todo.yml` Lastly – this protection can be configured by setting `globally_permitted_namespaces`, e.g.: ```ruby -PackageProtections.configure do |config| +RuboCop::Packs.configure do |config| config.globally_permitted_namespaces = ['SomeGlobalNamespace'] end ``` diff --git a/lib/rubocop/cop/package_protections/namespaced_under_package_name.rb b/lib/rubocop/cop/package_protections/namespaced_under_package_name.rb index 32fb8ef..f4fb23e 100644 --- a/lib/rubocop/cop/package_protections/namespaced_under_package_name.rb +++ b/lib/rubocop/cop/package_protections/namespaced_under_package_name.rb @@ -34,7 +34,7 @@ def cop_configs(packages) enabled: include_packs.any?, metadata: { 'IncludePacks' => include_packs, - 'GloballyPermittedNamespaces' => ::PackageProtections.config.globally_permitted_namespaces + 'GloballyPermittedNamespaces' => ::RuboCop::Packs.config.globally_permitted_namespaces } ) ] @@ -57,23 +57,19 @@ def identifier sig { override.params(behavior: ::PackageProtections::ViolationBehavior, package: ParsePackwerk::Package).returns(T.nilable(String)) } def unmet_preconditions_for_behavior(behavior, package) - if !behavior.enabled? && !package.metadata['global_namespaces'].nil? - "Invalid configuration for package `#{package.name}`. `#{identifier}` must be turned on to use `global_namespaces` configuration." - else - # We don't need to validate if the behavior is currentely fail_never - return if behavior.fail_never? + # We don't need to validate if the behavior is currentely fail_never + return if behavior.fail_never? - # The reason for this is precondition is the `MultipleNamespacesProtection` assumes this to work properly. - # To remove this precondition, we need to modify `MultipleNamespacesProtection` to be more generalized! - is_root_package = package.name == ParsePackwerk::ROOT_PACKAGE_NAME - in_allowed_directory = ::PackageProtections::EXPECTED_PACK_DIRECTORIES.any? do |expected_package_directory| - package.directory.to_s.start_with?(expected_package_directory) - end - if in_allowed_directory || is_root_package - nil - else - "Package #{package.name} must be located in one of #{::PackageProtections::EXPECTED_PACK_DIRECTORIES.join(', ')} (or be the root) to use this protection" - end + # The reason for this is precondition is the `MultipleNamespacesProtection` assumes this to work properly. + # To remove this precondition, we need to modify `MultipleNamespacesProtection` to be more generalized! + is_root_package = package.name == ParsePackwerk::ROOT_PACKAGE_NAME + in_allowed_directory = ::PackageProtections::EXPECTED_PACK_DIRECTORIES.any? do |expected_package_directory| + package.directory.to_s.start_with?(expected_package_directory) + end + if in_allowed_directory || is_root_package + nil + else + "Package #{package.name} must be located in one of #{::PackageProtections::EXPECTED_PACK_DIRECTORIES.join(', ')} (or be the root) to use this protection" end end diff --git a/package_protections.gemspec b/package_protections.gemspec index f4e9611..8001481 100644 --- a/package_protections.gemspec +++ b/package_protections.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = 'package_protections' - spec.version = '3.0.0' + spec.version = '3.1.0' spec.authors = ['Gusto Engineers'] spec.email = ['stephan.hagemann@gusto.com'] spec.summary = 'Package protections for Rails apps' diff --git a/sorbet/rbi/gems/rubocop-packs@0.0.11.rbi b/sorbet/rbi/gems/rubocop-packs@0.0.14.rbi similarity index 71% rename from sorbet/rbi/gems/rubocop-packs@0.0.11.rbi rename to sorbet/rbi/gems/rubocop-packs@0.0.14.rbi index 8202f9b..d24cd34 100644 --- a/sorbet/rbi/gems/rubocop-packs@0.0.11.rbi +++ b/sorbet/rbi/gems/rubocop-packs@0.0.14.rbi @@ -6,6 +6,7 @@ module RuboCop; end module RuboCop::Cop; end + module RuboCop::Cop::Packs; end class RuboCop::Cop::Packs::ClassMethodsAsPublicApis < ::RuboCop::Cop::Base @@ -29,9 +30,6 @@ class RuboCop::Cop::Packs::NamespaceConvention < ::RuboCop::Cop::Base sig { returns(RuboCop::Cop::Packs::NamespaceConvention::DesiredZeitwerkApi) } def desired_zeitwerk_api; end - - sig { returns(T::Hash[String, String]) } - def namespaces_to_packs; end end class RuboCop::Cop::Packs::NamespaceConvention::DesiredZeitwerkApi @@ -72,7 +70,27 @@ class RuboCop::Cop::Packs::RequireDocumentedPublicApis < ::RuboCop::Cop::Style:: def support_autocorrect?; end end -class RuboCop::Cop::Packs::TypedPublicApi < ::RuboCop::Cop::Sorbet::StrictSigil; end +class RuboCop::Cop::Packs::TypedPublicApi < ::RuboCop::Cop::Sorbet::StrictSigil + sig { params(processed_source: T.untyped).void } + def investigate(processed_source); end +end + +module RuboCop::Cop::PackwerkLite; end +class RuboCop::Cop::PackwerkLite::ConstantResolver; end + +class RuboCop::Cop::PackwerkLite::ConstantResolver::ConstantReference < ::T::Struct + const :constant_definition_location, Pathname + const :constant_name, String + const :global_namespace, String + const :referencing_file, Pathname + const :source_package, ParsePackwerk::Package + + sig { returns(T::Boolean) } + def public_api?; end + + sig { returns(ParsePackwerk::Package) } + def referencing_package; end +end module RuboCop::Packs class << self @@ -117,11 +135,23 @@ module RuboCop::Packs::Private sig { void } def bust_cache!; end + sig { params(rule: String).returns(T::Set[String]) } + def exclude_for_rule(rule); end + sig { void } def load_client_configuration; end sig { returns(T::Array[T::Hash[T.untyped, T.untyped]]) } def rubocop_todo_ymls; end + + sig { params(package: ParsePackwerk::Package).returns(T::Array[String]) } + def validate_failure_mode_strict(package); end + + sig { params(package: ParsePackwerk::Package).returns(T::Array[String]) } + def validate_rubocop_todo_yml(package); end + + sig { params(package: ParsePackwerk::Package).returns(T::Array[String]) } + def validate_rubocop_yml(package); end end end @@ -132,8 +162,22 @@ class RuboCop::Packs::Private::Configuration sig { void } def bust_cache!; end + sig { returns(T::Array[String]) } + def globally_permitted_namespaces; end + + def globally_permitted_namespaces=(_arg0); end + sig { returns(T::Array[String]) } def permitted_pack_level_cops; end def permitted_pack_level_cops=(_arg0); end + + sig { returns(T::Array[String]) } + def required_pack_level_cops; end + + def required_pack_level_cops=(_arg0); end end + +module RuboCop::PackwerkLite; end +class RuboCop::PackwerkLite::Error < ::StandardError; end + diff --git a/spec/package_protections_spec.rb b/spec/package_protections_spec.rb index 26dc7fa..5e0fed6 100644 --- a/spec/package_protections_spec.rb +++ b/spec/package_protections_spec.rb @@ -12,7 +12,7 @@ PackageProtections.bust_cache! allow(Bundler).to receive(:root).and_return(Pathname.new('.')) - PackageProtections.configure do |config| + RuboCop::Packs.configure do |config| config.globally_permitted_namespaces = globally_permitted_namespaces end end @@ -1615,7 +1615,7 @@ def get_new_violations let(:globally_permitted_namespaces) { ['MyNamespace'] } it 'properly configures package protections' do - expect(PackageProtections.config.globally_permitted_namespaces).to eq(['MyNamespace']) + expect(RuboCop::Packs.config.globally_permitted_namespaces).to eq(['MyNamespace']) end end end