Class: RuboCop::Cop::PackageProtections::TypedPublicApi

Inherits:
Packs::TypedPublicApis
  • Object
show all
Extended by:
T::Sig
Includes:
PackageProtections::ProtectionInterface, PackageProtections::RubocopProtectionInterface
Defined in:
lib/rubocop/cop/package_protections/typed_public_api.rb

Overview

This inherits from ‘Sorbet::StrictSigil` and doesn’t change any behavior of it. The only reason we do this is so that configuration for this cop can live under a different cop namespace. This prevents this cop’s configuration from clashing with other configurations for the same cop. A concrete example of this would be if a user is using this package protection to make sure public APIs are typed, and separately the application as a whole requiring strict typing in certain parts of the application.

To prevent problems associated with needing to manage identical configurations for the same cop, we simply call it something else in the context of this protection.

We can apply this same pattern if we want to use other cops in the context of package protections and prevent clashing.

Constant Summary collapse

IDENTIFIER =
T.let('prevent_this_package_from_exposing_an_untyped_api'.freeze, String)

Instance Method Summary collapse

Methods included from PackageProtections::RubocopProtectionInterface

#cop_configs, #custom_cop_config, #get_offenses_for_existing_violations, #get_offenses_for_new_violations

Methods included from PackageProtections::ProtectionInterface

#default_behavior, #get_offenses, #get_offenses_for_existing_violations, #get_offenses_for_new_violations, #supports_violation_behavior?

Instance Method Details

#cop_nameObject



40
41
42
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 40

def cop_name
  'PackageProtections/TypedPublicApi'
end

#humanized_protection_descriptionObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 64

def humanized_protection_description
  <<~MESSAGE
    These files cannot have ANY Ruby files in the public API that are not typed strict or higher.
    This is failing because these files are in `.rubocop_todo.yml` under `#{cop_name}`.
    If you want to be able to ignore these files, you'll need to open the file's package's `package.yml` file and
    change `#{IDENTIFIER}` to `#{::PackageProtections::ViolationBehavior::FailOnNew.serialize}`

    See https://go/packwerk_cheatsheet_typed_api for more info.
  MESSAGE
end

#humanized_protection_nameObject



59
60
61
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 59

def humanized_protection_name
  'Typed API Violations'
end

#identifierObject



27
28
29
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 27

def identifier
  IDENTIFIER
end

#included_globs_for_packObject



45
46
47
48
49
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 45

def included_globs_for_pack
  [
    'app/public/**/*'
  ]
end

#message_for_fail_on_any(file) ⇒ Object



54
55
56
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 54

def message_for_fail_on_any(file)
  "#{file} should be `typed: strict`"
end

#unmet_preconditions_for_behavior(behavior, package) ⇒ Object



32
33
34
35
36
37
# File 'lib/rubocop/cop/package_protections/typed_public_api.rb', line 32

def unmet_preconditions_for_behavior(behavior, package)
  # We might decide that we should check that `package.enforces_privacy?` is true here too, since that signifies the app has decided they want
  # a public api in `app/public`. For now, we say there are no preconditions, because the user can still make `app/public` even if they are not yet
  # ready to enforce privacy, and they might want to enforce a typed API.
  nil
end