Sha256: eb24f61b56337ce5e431cf1c492bf25649b885100e6f6b420aff4be6b3b2b2b3

Contents?: true

Size: 1.56 KB

Versions: 10

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module Sorbet
      # This cop 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
      # from occurring.
      #
      # @example
      #
      #   # bad
      #   sig.override(allow_incompatible: true)
      #
      #   # good
      #   sig.override
      class AllowIncompatibleOverride < RuboCop::Cop::Cop
        def_node_search(:sig?, <<-PATTERN)
          (
            send
            nil?
            :sig
             ...
          )
        PATTERN

        def not_nil?(node)
          !node.nil?
        end

        def_node_search(:allow_incompatible?, <<-PATTERN)
          (pair (sym :allow_incompatible) (true))
        PATTERN

        def_node_matcher(:allow_incompatible_override?, <<-PATTERN)
          (
            send
            [#not_nil? #sig?]
            :override
            [#not_nil? #allow_incompatible?]
          )
        PATTERN

        def on_send(node)
          return unless allow_incompatible_override?(node)
          add_offense(
            node.children[2],
            message: "Usage of `allow_incompatible` suggests a violation of the Liskov Substitution Principle. "\
            "Instead, strive to write interfaces which respect subtyping principles and remove `allow_incompatible`",
          )
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-sorbet-0.7.0 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.11 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.10 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.9 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.8 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.7 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.6 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.5 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.4 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.3 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb