Skip to content

Commit

Permalink
Skip type name resolution if resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Jan 15, 2025
1 parent 65371a2 commit 3f9a1ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/rbs/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,28 @@ def validate_type_params
end
end

def resolve_signature(resolver, table, dirs, decls, only: nil)
map = UseMap.new(table: table)
dirs.each do |dir|
case dir
when AST::Directives::Use
dir.clauses.each do |clause|
map.build_map(clause)
end
end
end

decls = decls.map do |decl|
if only && !only.member?(decl)
decl
else
resolve_declaration(resolver, map, decl, outer: [], prefix: Namespace.root)
end
end

[dirs, decls]
end

def resolve_type_names(only: nil)
resolver = Resolver::TypeNameResolver.new(self)
env = Environment.new
Expand All @@ -498,21 +520,9 @@ def resolve_type_names(only: nil)
table.compute_children

signatures.each do |buffer, (dirs, decls)|
map = UseMap.new(table: table)
dirs.each do |dir|
dir.clauses.each do |clause|
map.build_map(clause)
end
unless dirs.find { _1.is_a?(AST::Directives::Resolved) }
_, decls = resolve_signature(resolver, table, dirs, decls)
end

decls = decls.map do |decl|
if only && !only.member?(decl)
decl
else
resolve_declaration(resolver, map, decl, outer: [], prefix: Namespace.root)
end
end

env.add_signature(buffer: buffer, directives: dirs, decls: decls)
end

Expand Down
2 changes: 2 additions & 0 deletions sig/environment.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ module RBS
#
def resolve_type_names: (?only: Set[AST::Declarations::t]?) -> Environment

def resolve_signature: (Resolver::TypeNameResolver, UseMap::Table, Array[AST::Directives::t], Array[AST::Declarations::t], ?only: Set[AST::Declarations::t]?) -> [Array[AST::Directives::t], Array[AST::Declarations::t]]

def inspect: () -> String

def buffers: () -> Array[Buffer]
Expand Down
16 changes: 16 additions & 0 deletions test/rbs/environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,20 @@ class OB
assert_operator env.class_decls, :key?, RBS::TypeName.parse("::OB")
end
end

def test_resolved_directive
buf, dirs, decls = RBS::Parser.parse_signature(<<-RBS)
# resolved: true
type t = String
RBS

env = Environment.new
env.add_signature(buffer: buf, directives: dirs, decls: decls)

env.resolve_type_names.tap do |env|
alias_decl = env.type_alias_decls[RBS::TypeName.parse("::t")]
assert_equal "String", alias_decl.decl.type.to_s
end
end
end

0 comments on commit 3f9a1ff

Please sign in to comment.