Sha256: 87d6185550d053624e88705bea013ec984dbc890cc0ebea5a0803c2ab4faf2fd

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Sorbet
      # This cop 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.
      #
      # @example
      #
      #   # bad
      #   sig { void.checked(true) }
      #
      #   # good
      #   sig { void }
      class CheckedTrueInSignature < RuboCop::Cop::Cop
        include(RuboCop::Cop::RangeHelp)

        def_node_matcher(:signature?, <<~PATTERN)
          (block (send nil? :sig) (args) ...)
        PATTERN

        def_node_search(:offending_node, <<~PATTERN)
          (send _ :checked (true))
        PATTERN

        MESSAGE =
          'Using `checked(true)` in a method signature definition is not allowed. ' \
            '`checked(true)` is the default behavior for modules/classes with runtime checks enabled. ' \
            'To enable typechecking at runtime for this module, regardless of global settings, ' \
            '`include(WaffleCone::RuntimeChecks)` to this module and set other methods to `checked(false)`.'
        private_constant(:MESSAGE)

        def on_block(node)
          return unless signature?(node)

          error = offending_node(node).first
          return unless error

          add_offense(
            error,
            location: source_range(
              processed_source.buffer,
              error.location.line,
              (error.location.selector.begin_pos)..(error.location.end.begin_pos),
            ),
            message: MESSAGE
          )
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-sorbet-0.3.3 lib/rubocop/cop/sorbet/checked_true_in_signature.rb
rubocop-sorbet-0.3.2 lib/rubocop/cop/sorbet/checked_true_in_signature.rb
rubocop-sorbet-0.3.1 lib/rubocop/cop/sorbet/checked_true_in_signature.rb
rubocop-sorbet-0.3.0 lib/rubocop/cop/sorbet/checked_true_in_signature.rb
rubocop-sorbet-0.2.0 lib/rubocop/cop/sorbet/checked_true_in_signature.rb
rubocop-sorbet-0.0.2 lib/rubocop/cop/sorbet/checked_true_in_signature.rb