Sha256: ae9a3a747902520875f38f2e24cd26aa8688876cffa6cf70eb8af0861d636bda

Contents?: true

Size: 1.89 KB

Versions: 107

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true
# typed: false

module T::Private::Sealed
  module NoInherit
    def inherited(other)
      super
      this_line = Kernel.caller.find {|line| !line.match(/in `inherited'$/)}
      T::Private::Sealed.validate_inheritance(this_line, self, 'inherited')
    end
  end

  module NoIncludeExtend
    def included(other)
      super
      this_line = Kernel.caller.find {|line| !line.match(/in `included'$/)}
      T::Private::Sealed.validate_inheritance(this_line, self, 'included')
    end

    def extended(other)
      super
      this_line = Kernel.caller.find {|line| !line.match(/in `extended'$/)}
      T::Private::Sealed.validate_inheritance(this_line, self, 'extended')
    end
  end

  def self.declare(mod, decl_file)
    if !mod.is_a?(Module)
      raise "#{mod} is not a class or module and cannot be declared `sealed!`"
    end
    if sealed_module?(mod)
      raise "#{mod} was already declared `sealed!` and cannot be re-declared `sealed!`"
    end
    if T::Private::Final.final_module?(mod)
      raise "#{mod} was already declared `final!` and cannot be declared `sealed!`"
    end
    mod.extend(mod.is_a?(Class) ? NoInherit : NoIncludeExtend)
    if !decl_file
      raise "Couldn't determine declaration file for sealed class."
    end
    mod.instance_variable_set(:@sorbet_sealed_module_decl_file, decl_file)
  end

  def self.sealed_module?(mod)
    mod.instance_variable_defined?(:@sorbet_sealed_module_decl_file)
  end

  def self.validate_inheritance(this_line, parent, verb)
    this_file = this_line&.split(':').first
    decl_file = parent.instance_variable_get(:@sorbet_sealed_module_decl_file)

    if !this_file || !decl_file
      raise "Couldn't determine enough file information for checking sealed modules"
    end

    if !this_file.start_with?(decl_file)
      raise "#{parent} was declared sealed and can only be #{verb} in #{decl_file}, not #{this_file}"
    end
  end
end

Version data entries

107 entries across 107 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.5247 lib/types/private/sealed.rb
sorbet-runtime-0.5.5238 lib/types/private/sealed.rb
sorbet-runtime-0.5.5226 lib/types/private/sealed.rb
sorbet-runtime-0.5.5213 lib/types/private/sealed.rb
sorbet-runtime-0.5.5207 lib/types/private/sealed.rb
sorbet-runtime-0.5.5200 lib/types/private/sealed.rb
sorbet-runtime-0.5.5188 lib/types/private/sealed.rb
sorbet-runtime-0.5.5182 lib/types/private/sealed.rb
sorbet-runtime-0.5.5181 lib/types/private/sealed.rb
sorbet-runtime-0.5.5179 lib/types/private/sealed.rb
sorbet-runtime-0.5.5176 lib/types/private/sealed.rb
sorbet-runtime-0.5.5175 lib/types/private/sealed.rb
sorbet-runtime-0.4.5173 lib/types/private/sealed.rb
sorbet-runtime-0.4.5168 lib/types/private/sealed.rb
sorbet-runtime-0.4.5163 lib/types/private/sealed.rb
sorbet-runtime-0.4.5158 lib/types/private/sealed.rb
sorbet-runtime-0.4.5146 lib/types/private/sealed.rb
sorbet-runtime-0.4.5144 lib/types/private/sealed.rb
sorbet-runtime-0.4.5140 lib/types/private/sealed.rb
sorbet-runtime-0.4.5136 lib/types/private/sealed.rb