Skip to content

Commit

Permalink
Deprecate Kernel#TypeName too
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Dec 18, 2024
1 parent c8b02bf commit c95e936
Show file tree
Hide file tree
Showing 37 changed files with 242 additions and 234 deletions.
6 changes: 3 additions & 3 deletions bin/query-rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ source.load()

case
when match = name.match(/(?<constant_name>[^#]+)#(?<method_name>.+)/)
type_name = TypeName(match[:constant_name] || raise)
type_name = RBS::TypeName.parse(match[:constant_name] || raise)
instance_method = (match[:method_name] or raise).to_sym

doc = annotator.doc_for_method(type_name, instance_method: instance_method, tester: tester)
when match = name.match(/(?<constant_name>[^#]+)\.(?<method_name>.+)/)
type_name = TypeName(match[:constant_name] || raise)
type_name = RBS::TypeName.parse(match[:constant_name] || raise)
singleton_method = (match[:method_name] or raise).to_sym

doc = annotator.doc_for_method(type_name, singleton_method: singleton_method, tester: tester)
else
type_name = TypeName(name)
type_name = RBS::TypeName.parse(name)

doc = annotator.doc_for_class(type_name, tester: tester) || annotator.doc_for_constant(type_name, tester: tester)
end
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The `#build_singleton` calculates the type of `.new` methods based on the defini
`DefinitionBuilder#expand_alias` and its variants provide one step *unfold* operation of type aliases.

```ruby
builder.expand_alias2(TypeName("::int"), []) # => returns `::Integer | ::_ToInt`
builder.expand_alias2(RBS::TypeName.parse("::int"), []) # => returns `::Integer | ::_ToInt`
```

We don't have *normalize* operation for type aliases, because RBS allows recursive type alias definition, which cannot be *fully* unfolded.
Expand Down
6 changes: 3 additions & 3 deletions lib/rbs/annotate/annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ def partition
case
when match = source.match(/(?<constant_name>[^#]+)#(?<method_name>.+)/)
[
TypeName(match[:constant_name] || raise),
TypeName.parse(match[:constant_name] || raise),
[
false,
(match[:method_name] or raise).to_sym
]
]
when match = source.match(/(?<constant_name>[^#]+)\.(?<method_name>.+)/)
[
TypeName(match[:constant_name] || raise),
TypeName.parse(match[:constant_name] || raise),
[
true,
(match[:method_name] or raise).to_sym
]
]
else
[
TypeName(source),
TypeName.parse(source),
nil
]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/annotate/rdoc_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def class_docs(typename)
def find_const(const_name)
namespace =
if const_name.namespace.empty?
TypeName("::Object")
TypeName.parse("::Object")
else
const_name.namespace.to_type_name
end
Expand Down
10 changes: 5 additions & 5 deletions lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def run_ancestors(args, options)
env = Environment.from_loader(loader).resolve_type_names

builder = DefinitionBuilder::AncestorBuilder.new(env: env)
type_name = TypeName(args[0]).absolute!
type_name = TypeName.parse(args[0]).absolute!

case env.constant_entry(type_name)
when Environment::ClassEntry, Environment::ModuleEntry, Environment::ClassAliasEntry, Environment::ModuleAliasEntry
Expand Down Expand Up @@ -353,7 +353,7 @@ def run_methods(args, options)
env = Environment.from_loader(loader).resolve_type_names

builder = DefinitionBuilder.new(env: env)
type_name = TypeName(args[0]).absolute!
type_name = TypeName.parse(args[0]).absolute!

if env.module_name?(type_name)
definition = case kind
Expand Down Expand Up @@ -406,7 +406,7 @@ def run_method(args, options)
env = Environment.from_loader(loader).resolve_type_names

builder = DefinitionBuilder.new(env: env)
type_name = TypeName(args[0]).absolute!
type_name = TypeName.parse(args[0]).absolute!
method_name = args[1].to_sym

unless env.module_name?(type_name)
Expand Down Expand Up @@ -479,9 +479,9 @@ def run_constant(args, options)
builder = DefinitionBuilder.new(env: env)
resolver = Resolver::ConstantResolver.new(builder: builder)

resolver_context = context ? [nil, TypeName(context).absolute!] : nil #: Resolver::context
resolver_context = context ? [nil, TypeName.parse(context).absolute!] : nil #: Resolver::context
stdout.puts "Context: #{context}"
const_name = TypeName(args[0])
const_name = TypeName.parse(args[0])
stdout.puts "Constant name: #{const_name}"

if const_name.absolute?
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/cli/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
end

@diff = RBS::Diff.new(
type_name: TypeName(type_name).absolute!,
type_name: TypeName.parse(type_name).absolute!,
library_options: library_options,
after_path: after_path,
before_path: before_path,
Expand Down
4 changes: 2 additions & 2 deletions lib/rbs/prototype/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,9 @@ def param_type(node, default: Types::Bases::Any.new(location: nil))
when :FLOAT
BuiltinNames::Float.instance_type
when :RATIONAL
Types::ClassInstance.new(name: TypeName("::Rational"), args: [], location: nil)
Types::ClassInstance.new(name: TypeName.parse("::Rational"), args: [], location: nil)
when :IMAGINARY
Types::ClassInstance.new(name: TypeName("::Complex"), args: [], location: nil)
Types::ClassInstance.new(name: TypeName.parse("::Complex"), args: [], location: nil)
when :LIT
case node.children[0]
when Symbol
Expand Down
4 changes: 2 additions & 2 deletions lib/rbs/prototype/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ def generate_constants(mod, decls)
location: nil
)
when ARGF
Types::ClassInstance.new(name: TypeName("::RBS::Unnamed::ARGFClass"), args: [], location: nil)
Types::ClassInstance.new(name: TypeName.parse("::RBS::Unnamed::ARGFClass"), args: [], location: nil)
when ENV
Types::ClassInstance.new(name: TypeName("::RBS::Unnamed::ENVClass"), args: [], location: nil)
Types::ClassInstance.new(name: TypeName.parse("::RBS::Unnamed::ENVClass"), args: [], location: nil)
else
value_type_name = to_type_name(const_name!(Reflection.object_class(value)), full_name: true).absolute!
args = type_args(value_type_name)
Expand Down
4 changes: 2 additions & 2 deletions lib/rbs/prototype/runtime/value_object_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def self.generatable?(target)
CAN_CALL_KEYWORD_INIT_P = Struct.new(:tmp).respond_to?(:keyword_init?)

def build_super_class
AST::Declarations::Class::Super.new(name: TypeName("::Struct"), args: [untyped], location: nil)
AST::Declarations::Class::Super.new(name: TypeName.parse("::Struct"), args: [untyped], location: nil)
end

def add_decl_members(decl)
Expand Down Expand Up @@ -223,7 +223,7 @@ def self.generatable?(target)
private

def build_super_class
AST::Declarations::Class::Super.new(name: TypeName("::Data"), args: [], location: nil)
AST::Declarations::Class::Super.new(name: TypeName.parse("::Data"), args: [], location: nil)
end

def add_decl_members(decl)
Expand Down
23 changes: 14 additions & 9 deletions lib/rbs/type_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,24 @@ def +(other)
)
end
end

def self.parse(string)
absolute = string.start_with?("::")

*path, name = string.delete_prefix("::").split("::").map(&:to_sym)
raise unless name

TypeName.new(
name: name,
namespace: RBS::Namespace.new(path: path, absolute: absolute)
)
end
end
end

module Kernel
def TypeName(string)
absolute = string.start_with?("::")

*path, name = string.delete_prefix("::").split("::").map(&:to_sym)
raise unless name

RBS::TypeName.new(
name: name,
namespace: RBS::Namespace.new(path: path, absolute: absolute)
)
warn "Kernel#TypeName() is deprecated. Use RBS::TypeName.parse instead.", category: :deprecated
RBS::TypeName.parse(string)
end
end
2 changes: 1 addition & 1 deletion lib/rbs/unit_test/type_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def assert_const_type(type, constant_name)

assert typecheck.value(constant, value_type), "`#{constant_name}` (#{constant.inspect}) must be compatible with given type `#{value_type}`"

type_name = TypeName(constant_name).absolute!
type_name = TypeName.parse(constant_name).absolute!
definition = env.constant_entry(type_name)
assert definition, "Cannot find RBS type definition of `#{constant_name}`"

Expand Down
8 changes: 4 additions & 4 deletions sig/ancestor_graph.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module RBS
# ```ruby
# graph = AncestorGraph.new(env: env, ancestor_builder: ancestor_builder)
#
# graph.each_parent(AncestorGraph::InstanceNode.new(type_name: TypeName("::Object")))
# graph.each_ancestor(AncestorGraph::InstanceNode.new(type_name: TypeName("::Object")))
# graph.each_parent(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object")))
# graph.each_ancestor(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object")))
#
# graph.each_child(AncestorGraph::InstanceNode.new(type_name: TypeName("::Object")))
# graph.each_descendant(AncestorGraph::InstanceNode.new(type_name: TypeName("::Object")))
# graph.each_child(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object")))
# graph.each_descendant(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object")))
# ```
#
# Note that the class works for class/module declarations.
Expand Down
4 changes: 2 additions & 2 deletions sig/resolver/constant_resolver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module RBS
# ```rb
# table = RBS::ConstantResolver::Table.new(env)
#
# table.children(TypeName("::Object")) # -> { ... } Returns a hash of name and constants.
# table.children(TypeName("::File::PATH_SEPARATOR")) # -> nil Returns nil because the constant is not a module.
# table.children(TypeName.parse("::Object")) # -> { ... } Returns a hash of name and constants.
# table.children(TypeName.parse("::File::PATH_SEPARATOR")) # -> nil Returns nil because the constant is not a module.
#
# table.toplevel # -> { ... } Returns a hash of top level constants.
# ```
Expand Down
2 changes: 1 addition & 1 deletion sig/resolver/context.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module RBS
#
# Note that the `TypeName` must be an absolute type name.
#
# The following Ruby code has context of `[[nil, TypeName("::Foo")], false]` where
# The following Ruby code has context of `[[nil, TypeName.parse("::Foo")], false]` where
#
# * `Foo` is a class defined in RBS file
# * `Bar` is not defined in RBS files
Expand Down
10 changes: 5 additions & 5 deletions sig/type_alias_regularity.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ module RBS
# ```rb
# validator = RBS::TypeAliasRegularity.validate(env: env)
#
# validator.nonregular?(TypeName("::foo")) # => nil
# validator.nonregular?(TypeName("::bar")) # => nil
# validator.nonregular?(TypeName("::baz")) # => TypeAliasRegularity::Diagnostic
# validator.nonregular?(TypeName.parse("::foo")) # => nil
# validator.nonregular?(TypeName.parse("::bar")) # => nil
# validator.nonregular?(TypeName.parse("::baz")) # => TypeAliasRegularity::Diagnostic
# ```
#
# A special case is when the type argument is `untyped`.
Expand Down Expand Up @@ -51,8 +51,8 @@ module RBS
# The type `t` is nonregular because it contains `t[T?]` on it's right hand side.
#
# ```
# diagnostic = validator.nonregular?(TypeName("::t"))
# diagnostic.type_name # => TypeName("::t")
# diagnostic = validator.nonregular?(TypeName.parse("::t"))
# diagnostic.type_name # => TypeName.parse("::t")
# diagnostic.nonregular_type # => t[T?]
# ```
#
Expand Down
13 changes: 8 additions & 5 deletions sig/typename.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ module RBS
# Returns a new type name with a namespace appended to given namespace.
#
# ```rb
# TypeName("Hello").with_prefix(Namespace("World")) # => World::Hello
# TypeName("Foo::Bar").with_prefix(Namespace("::Hello")) # => ::Hello::Foo::Bar
# TypeName("::A::B").with_prefix(Namespace("C")) # => ::A::B
# TypeName.parse("Hello").with_prefix(Namespace("World")) # => World::Hello
# TypeName.parse("Foo::Bar").with_prefix(Namespace("::Hello")) # => ::Hello::Foo::Bar
# TypeName.parse("::A::B").with_prefix(Namespace("C")) # => ::A::B
# ```
#
def with_prefix: (Namespace namespace) -> TypeName

def +: (TypeName) -> TypeName

def split: () -> Array[Symbol]

# Returns type name with given string representation.
def self.parse: (String name) -> RBS::TypeName
end
end

module Kernel
# Returns type name with given string representation.
def TypeName: (String name) -> RBS::TypeName
# Deprecated: Use `RBS::TypeName.parse` instead
%a{steep:deprecated} def TypeName: (String name) -> RBS::TypeName
end
2 changes: 1 addition & 1 deletion sig/use_map.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module RBS
# ```rb
# map = UseMap.build(environment)
#
# map.resolve?(TypeName("TN")) # => nil or resolved type name
# map.resolve?(TypeName.parse("TN")) # => nil or resolved type name
# ```
#
class UseMap
Expand Down
4 changes: 2 additions & 2 deletions test/rbs/ancestor_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,13 @@ module Bar[Y, Z]
assert_equal 5, a.ancestors.size
a.ancestors[0].tap do |ancestor|
assert_instance_of Ancestor::Instance, ancestor
assert_equal TypeName("::Foo"), ancestor.name
assert_equal RBS::TypeName.parse("::Foo"), ancestor.name
assert_equal [Types::Variable.build(:X)], ancestor.args
assert_nil ancestor.source
end
a.ancestors[1].tap do |ancestor|
assert_instance_of Ancestor::Instance, ancestor
assert_equal TypeName("::Bar"), ancestor.name
assert_equal RBS::TypeName.parse("::Bar"), ancestor.name
assert_equal [Types::Variable.build(:X), parse_type("::String")], ancestor.args
assert_instance_of AST::Members::Include, ancestor.source
end
Expand Down
4 changes: 2 additions & 2 deletions test/rbs/ancestor_graph_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class RBS::AncestorGraphTest < Test::Unit::TestCase

def InstanceNode(name)
if name.is_a?(String)
name = TypeName(name)
name = RBS::TypeName.parse(name)
end

AncestorGraph::InstanceNode.new(type_name: name)
end

def SingletonNode(name)
if name.is_a?(String)
name = TypeName(name)
name = RBS::TypeName.parse(name)
end

AncestorGraph::SingletonNode.new(type_name: name)
Expand Down
6 changes: 3 additions & 3 deletions test/rbs/annotate/annotations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ def test_copy
RBS::Annotate::Annotations.parse(an("annotate:rdoc:copy:Bar#baz")).tap do |a|
assert_instance_of RBS::Annotate::Annotations::Copy, a

assert_equal TypeName("Bar"), a.type_name
assert_equal RBS::TypeName.parse("Bar"), a.type_name
refute_predicate a, :singleton?
assert_equal :baz, a.method_name
end

RBS::Annotate::Annotations.parse(an("annotate:rdoc:copy:Bar.baz")).tap do |a|
assert_instance_of RBS::Annotate::Annotations::Copy, a

assert_equal TypeName("Bar"), a.type_name
assert_equal RBS::TypeName.parse("Bar"), a.type_name
assert_predicate a, :singleton?
assert_equal :baz, a.method_name
end

RBS::Annotate::Annotations.parse(an("annotate:rdoc:copy:Bar")).tap do |a|
assert_instance_of RBS::Annotate::Annotations::Copy, a

assert_equal TypeName("Bar"), a.type_name
assert_equal RBS::TypeName.parse("Bar"), a.type_name
refute_predicate a, :singleton?
assert_nil a.method_name
end
Expand Down
Loading

0 comments on commit c95e936

Please sign in to comment.