Sha256: fffaea4890ab30d46547921fdb4c674bfd0e48406a8de6e7ba0b27287f8408e7

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module Sorbet
      # Disallows declaring implicit conversion methods.
      # Since Sorbet is a nominal (not structural) type system,
      # implicit conversion is currently unsupported.
      #
      # @example
      #
      #   # 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
      #
      # @see https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html
      # @note Since the arity of aliased methods is not checked, false positives may result.
      class ImplicitConversionMethod < RuboCop::Cop::Base
        IMPLICIT_CONVERSION_METHODS = [:to_ary, :to_int, :to_hash, :to_str].freeze
        MSG = "Avoid implicit conversion methods, as Sorbet does not support them. " \
          "Explicity convert to the desired type instead."
        RESTRICT_ON_SEND = [:alias_method].freeze

        def on_alias(node)
          new_id = node.new_identifier
          add_offense(new_id) if IMPLICIT_CONVERSION_METHODS.include?(new_id.value)
        end

        def on_def(node)
          return unless IMPLICIT_CONVERSION_METHODS.include?(node.method_name)
          return unless node.arguments.empty?

          add_offense(node)
        end
        alias_method :on_defs, :on_def

        def on_send(node)
          add_offense(node.first_argument) if IMPLICIT_CONVERSION_METHODS.include?(node.first_argument.value)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-sorbet-0.8.7 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.6 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.5 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.4 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.3 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.2 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.1 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.8.0 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.8 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.7 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.6 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.5 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.4 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.3 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.2 lib/rubocop/cop/sorbet/implicit_conversion_method.rb
rubocop-sorbet-0.7.1 lib/rubocop/cop/sorbet/implicit_conversion_method.rb