Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signature for Module#const_source_location #538

Merged
merged 1 commit into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core/module.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,47 @@ class Module < Object
#
def const_set: (Symbol | String arg0, untyped arg1) -> untyped

# Returns the Ruby source filename and line number containing first definition
# of constant specified. If the named constant is not found, `nil` is returned.
# If the constant is found, but its source location can not be extracted
# (constant is defined in C code), empty array is returned.
#
# *inherit* specifies whether to lookup in `mod.ancestors` (`true` by default).
#
# # test.rb:
# class A
# C1 = 1
# end
#
# module M
# C2 = 2
# end
#
# class B < A
# include M
# C3 = 3
# end
#
# class A # continuation of A definition
# end
#
# p B.const_source_location('C3') # => ["test.rb", 11]
# p B.const_source_location('C2') # => ["test.rb", 6]
# p B.const_source_location('C1') # => ["test.rb", 2]
#
# p B.const_source_location('C2', false) # => nil -- don't lookup in ancestors
#
# p Object.const_source_location('B') # => ["test.rb", 9]
# p Object.const_source_location('A') # => ["test.rb", 1] -- note it is first entry, not "continuation"
#
# p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors
# p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules
#
# p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported
# p Object.const_source_location('String') # => [] -- constant is defined in C code
#
def const_source_location: (Symbol | String name, ?boolish inherit) -> ([String, Integer] | [ ] | nil)

# Returns an array of the names of the constants accessible in *mod*. This
# includes the names of constants in any included modules (example at start of
# section), unless the *inherit* parameter is set to `false`.
Expand Down
42 changes: 39 additions & 3 deletions test/stdlib/Module_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
require_relative "test_helper"

class ModuleTest < StdlibTest
target Module
class ModuleSingletonTest < Minitest::Test
include TypeAssertions

testing "singleton(::Module)"

def test_used_modules
Module.used_modules
assert_send_type "() -> Array[Module]",
Module, :used_modules
end
end

class ModuleInstanceTest < Minitest::Test
include TypeAssertions

testing "::Module"

module Foo
BAR = 1
end

def test_const_source_location
assert_send_type "(Symbol) -> [String, Integer]",
Foo, :const_source_location, :BAR
assert_send_type "(Symbol) -> nil",
Foo, :const_source_location, :UNKNOWN
assert_send_type "(String) -> [String, Integer]",
Foo, :const_source_location, "BAR"
assert_send_type "(String) -> nil",
Foo, :const_source_location, "UNKNOWN"
assert_send_type "(Symbol, true) -> [String, Integer]",
Foo, :const_source_location, :BAR, true
assert_send_type "(String, nil) -> [String, Integer]",
Foo, :const_source_location, "BAR", nil
assert_send_type "(Symbol) -> [ ]",
Foo, :const_source_location, :String
assert_send_type "(String) -> [ ]",
Foo, :const_source_location, "String"
assert_send_type "(Symbol, true) -> [ ]",
Foo, :const_source_location, :String, true
assert_send_type "(String, nil) -> nil",
Foo, :const_source_location, "String", nil
end
end