manual/cops_sorbet.md in rubocop-sorbet-0.7.0 vs manual/cops_sorbet.md in rubocop-sorbet-0.7.1

- old
+ new

@@ -4,14 +4,14 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.2.0 | - -This cop disallows using `.override(allow_incompatible: true)`. +Disallows using `.override(allow_incompatible: true)`. Using `allow_incompatible` suggests a violation of the Liskov Substitution Principle, meaning that a subclass is not a valid -subtype of it's superclass. This Cop prevents these design smells +subtype of its superclass. This Cop prevents these design smells from occurring. ### Examples ```ruby @@ -26,11 +26,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.2.0 | - -This cop disallows binding the return value of `T.any`, `T.all`, `T.enum` +Disallows binding the return value of `T.any`, `T.all`, `T.enum` to a constant directly. To bind the value, one must use `T.type_alias`. ### Examples ```ruby @@ -45,11 +45,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | No | Yes | 0.7.0 | - -This cop ensures that callback conditionals are bound to the right type +Ensures that callback conditionals are bound to the right type so that they are type checked properly. Auto-correction is unsafe because other libraries define similar style callbacks as Rails, but don't always need binding to the attached class. Auto-correcting those usages can lead to false positives and auto-correction introduces new typing errors. @@ -83,11 +83,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.2.0 | - -This cop disallows the usage of `checked(true)`. This usage could cause +Disallows the usage of `checked(true)`. This usage could cause confusion; it could lead some people to believe that a method would be checked even if runtime checks have not been enabled on the class or globally. Additionally, in the event where checks are enabled, `checked(true)` would be redundant; only `checked(false)` or `soft` would change the behaviour. @@ -105,11 +105,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.2.0 | - -This cop disallows the calls that are used to get constants fom Strings +Disallows the calls that are used to get constants fom Strings such as +constantize+, +const_get+, and +constants+. The goal of this cop is to make the code easier to statically analyze, more IDE-friendly, and more predictable. It leads to code that clearly expresses which values the constant can have. @@ -142,11 +142,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.7.0 | - -This cop checks for blank lines after signatures. +Checks for blank lines after signatures. It also suggests an autocorrect ### Examples @@ -165,11 +165,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.3.4 | - -This cop checks that the Sorbet sigil comes as the first magic comment in the file. +Checks that the Sorbet sigil comes as the first magic comment in the file. The expected order for magic comments is: (en)?coding, typed, warn_indent then frozen_string_literal. For example, the following bad ordering: @@ -194,11 +194,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.4 | - -This cop checks that every method definition and attribute accessor has a Sorbet signature. +Checks that every method definition and attribute accessor has a Sorbet signature. It also suggest an autocorrect with placeholders so the following code: ``` def foo(a, b, c); end @@ -220,11 +220,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.7.0 | - -This cop checks that there is only one Sorbet sigil in a given file +Checks that there is only one Sorbet sigil in a given file For example, the following class with two sigils ```ruby # typed: true @@ -247,11 +247,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet `false` sigil mandatory in all files. +Makes the Sorbet `false` sigil mandatory in all files. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- @@ -263,11 +263,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.6.0 | - -This cop ensures RBI shims do not include a call to extend T::Sig +Ensures RBI shims do not include a call to extend T::Sig or to extend T::Helpers ### Examples ```ruby @@ -297,19 +297,38 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.2.0 | 0.5.0 -No documentation +Correct `send` expressions in include statements by constant literals. +Sorbet, the static checker, is not (yet) able to support constructs on the +following form: + +```ruby +class MyClass + include send_expr +end +``` + +Multiple occurences of this can be found in Shopify's code base like: + +```ruby +include Rails.application.routes.url_helpers +``` +or +```ruby +include Polaris::Engine.helpers +``` + ## Sorbet/ForbidRBIOutsideOfAllowedPaths Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.6.1 | - -This cop makes sure that RBI files are always located under the defined allowed paths. +Makes sure that RBI files are always located under the defined allowed paths. Options: * `AllowedPaths`: A list of the paths where RBI files are allowed (default: ["rbi/**", "sorbet/rbi/**"]) @@ -337,12 +356,29 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | No | 0.2.0 | 0.6.1 -No documentation +Correct superclass `send` expressions by constant literals. +Sorbet, the static checker, is not (yet) able to support constructs on the +following form: + +```ruby +class Foo < send_expr; end +``` + +Multiple occurences of this can be found in Shopify's code base like: + +```ruby +class ShopScope < Component::TrustedIdScope[ShopIdentity::ShopId] +``` +or +```ruby +class ApiClientEligibility < Struct.new(:api_client, :match_results, :shop) +``` + ### Configurable attributes Name | Default value | Configurable values --- | --- | --- Exclude | `db/migrate/*.rb` | Array @@ -351,11 +387,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | No | 0.7.0 | 0.7.0 -This cop disallows using `T.unsafe` anywhere. +Disallows using `T.unsafe` anywhere. ### Examples ```ruby # bad @@ -369,11 +405,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | No | 0.6.9 | 0.7.0 -This cop disallows using `T.untyped` anywhere. +Disallows using `T.untyped` anywhere. ### Examples ```ruby # bad @@ -389,11 +425,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.4.0 | - -This cop disallows use of `T.untyped` or `T.nilable(T.untyped)` +Disallows use of `T.untyped` or `T.nilable(T.untyped)` as a prop type for `T::Struct` or `T::ImmutableStruct`. ### Examples ```ruby @@ -414,11 +450,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet typed sigil mandatory in all files. +Makes the Sorbet typed sigil mandatory in all files. Options: * `SuggestedStrictness`: Sorbet strictness level suggested in offense messages (default: 'false') * `MinimumStrictness`: If set, make offense if the strictness level in the file is below this one @@ -428,37 +464,67 @@ ### Configurable attributes Name | Default value | Configurable values --- | --- | --- SuggestedStrictness | `false` | String -MinimumStrictness | `false` | String +MinimumStrictness | `nil` | String +ExactStrictness | `nil` | String Include | `**/*.{rb,rbi,rake,ru}` | Array Exclude | `bin/**/*`, `db/**/*.rb`, `script/**/*` | Array ## Sorbet/IgnoreSigil Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet `ignore` sigil mandatory in all files. +Makes the Sorbet `ignore` sigil mandatory in all files. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- SuggestedStrictness | `ignore` | String Include | `**/*.{rb,rbi,rake,ru}` | Array Exclude | `bin/**/*`, `db/**/*.rb`, `script/**/*` | Array +## Sorbet/ImplicitConversionMethod + +Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged +--- | --- | --- | --- | --- +Disabled | Yes | No | <<next>> | - + +Disallows declaring implicit conversion methods. +Since Sorbet is a nominal (not structural) type system, +implicit conversion is currently unsupported. + +### Examples + +```ruby +# bad +def to_str; end + +# good +def to_str(x); end + +# bad +def self.to_str; end + +# good +def self.to_str(x); end + +# bad +alias to_str to_s +``` + ## Sorbet/KeywordArgumentOrdering Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.2.0 | - -This cop checks for the ordering of keyword arguments required by +Checks for the ordering of keyword arguments required by sorbet-runtime. The ordering requires that all keyword arguments are at the end of the parameters list, and all keyword arguments with a default value must be after those without default values. ### Examples @@ -471,17 +537,54 @@ # good sig { params(b: String, a: Integer).void } def foo(b:, a: 1); end ``` +## Sorbet/ObsoleteStrictMemoization + +Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged +--- | --- | --- | --- | --- +Enabled | Yes | Yes | 0.7.1 | - + +Checks for the obsolete pattern for initializing instance variables that was required for older Sorbet +versions in `#typed: strict` files. + +It's no longer required, as of Sorbet 0.5.10210 +See https://sorbet.org/docs/type-assertions#put-type-assertions-behind-memoization + +### Examples + +```ruby +# bad +sig { returns(Foo) } +def foo + @foo = T.let(@foo, T.nilable(Foo)) + @foo ||= Foo.new +end + +# bad +sig { returns(Foo) } +def foo + # This would have been a mistake, causing the memoized value to be discarded and recomputed on every call. + @foo = T.let(nil, T.nilable(Foo)) + @foo ||= Foo.new +end + +# good +sig { returns(Foo) } +def foo + @foo ||= T.let(Foo.new, T.nilable(Foo)) +end +``` + ## Sorbet/OneAncestorPerLine Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.6.0 | - -This cop ensures one ancestor per requires_ancestor line +Ensures one ancestor per requires_ancestor line rather than chaining them as a comma-separated list. ### Examples ```ruby @@ -546,11 +649,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.6.0 | - -This cop ensures empty class/module definitions in RBI files are +Ensures empty class/module definitions in RBI files are done on a single line rather than being split across multiple lines. ### Examples ```ruby @@ -572,11 +675,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet `strict` sigil mandatory in all files. +Makes the Sorbet `strict` sigil mandatory in all files. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- @@ -588,11 +691,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet `strong` sigil mandatory in all files. +Makes the Sorbet `strong` sigil mandatory in all files. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- @@ -604,11 +707,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Disabled | Yes | Yes | 0.3.3 | - -This cop makes the Sorbet `true` sigil mandatory in all files. +Makes the Sorbet `true` sigil mandatory in all files. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- @@ -620,11 +723,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | No | 0.6.6 | - -This cop ensures all constants used as `T.type_alias` are using CamelCase. +Ensures all constants used as `T.type_alias` are using CamelCase. ### Examples ```ruby # bad @@ -645,25 +748,28 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- Enabled | Yes | Yes | 0.3.3 | - -This cop checks that every Ruby file contains a valid Sorbet sigil. +Checks that every Ruby file contains a valid Sorbet sigil. Adapted from: https://gist.github.com/clarkdave/85aca4e16f33fd52aceb6a0a29936e52 Options: * `RequireSigilOnAllFiles`: make offense if the Sorbet typed is not found in the file (default: false) * `SuggestedStrictness`: Sorbet strictness level suggested in offense messages (default: 'false') * `MinimumStrictness`: If set, make offense if the strictness level in the file is below this one +* `ExactStrictness`: If set, make offense if the strictness level in the file is different than this one -If a `MinimumStrictness` level is specified, it will be used in offense messages and autocorrect. +If an `ExactStrictness` level is specified, it will be used in offense messages and autocorrect. +Otherwise, if a `MinimumStrictness` level is specified, it will be used in offense messages and autocorrect. ### Configurable attributes Name | Default value | Configurable values --- | --- | --- RequireSigilOnAllFiles | `false` | Boolean SuggestedStrictness | `false` | String -MinimumStrictness | `false` | String +MinimumStrictness | `nil` | String +ExactStrictness | `nil` | String Include | `**/*.{rb,rbi,rake,ru}` | Array Exclude | `bin/**/*`, `db/**/*.rb`, `script/**/*` | Array