Sha256: d4742cae23b3bc477eb886a99b334b142792f634731b13f45adc670f992a2bae

Contents?: true

Size: 1.56 KB

Versions: 17

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

17 entries across 17 versions & 1 rubygems

Version Path
rubocop-sorbet-0.6.2 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.1 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.6.0 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.5.1 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.5.0 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.4.1 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.4.0 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.3.7 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.3.6 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.3.5 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.3.4 lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb
rubocop-sorbet-0.3.3 lib/rubocop/cop/sorbet/allow_incompatible_override.rb
rubocop-sorbet-0.3.2 lib/rubocop/cop/sorbet/allow_incompatible_override.rb
rubocop-sorbet-0.3.1 lib/rubocop/cop/sorbet/allow_incompatible_override.rb
rubocop-sorbet-0.3.0 lib/rubocop/cop/sorbet/allow_incompatible_override.rb
rubocop-sorbet-0.2.0 lib/rubocop/cop/sorbet/allow_incompatible_override.rb
rubocop-sorbet-0.0.2 lib/rubocop/cop/sorbet/allow_incompatible_override.rb