Sha256: 3aec2f3e1b06861b3f989504a33aca9f55849c9c18543c06afdcd3a37d8a01bb

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module Sorbet
      # 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 its superclass. This Cop prevents these design smells
      # from occurring.
      #
      # @example
      #
      #   # bad
      #   sig.override(allow_incompatible: true)
      #
      #   # good
      #   sig.override
      class AllowIncompatibleOverride < RuboCop::Cop::Base
        MSG = "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`"
        RESTRICT_ON_SEND = [:override].freeze

        # @!method allow_incompatible_override?(node)
        def_node_matcher(:allow_incompatible_override?, <<~PATTERN)
          (send
            #sig?
            :override
            (hash <$(pair (sym :allow_incompatible) true) ...>)
          )
        PATTERN

        # @!method sig?(node)
        def_node_search :sig?, <<~PATTERN
          (send _ :sig ...)
        PATTERN

        def on_send(node)
          allow_incompatible_override?(node) do |allow_incompatible_pair|
            add_offense(allow_incompatible_pair)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-sorbet-0.7.2 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.7.1 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb