Skip to content

Commit

Permalink
Fix RBS translation of shape string keys
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Jan 14, 2025
1 parent ccdd026 commit a1acaef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rbi/rbs_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,16 @@ def visit_tuple(type)
def visit_shape(type)
@string << "{"
type.types.each_with_index do |(key, value), index|
@string << "#{key}: "
@string << case key
when String
"\"#{key}\" => "
when Symbol
if key.match?(/\A[a-zA-Z_]+[a-zA-Z0-9_]*\z/)
"#{key}: "
else
"\"#{key}\": "
end
end
visit(value)
@string << ", " if index < type.types.size - 1
end
Expand Down
26 changes: 26 additions & 0 deletions test/rbi/rbs_printer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,32 @@ def foo: { (?) -> untyped } -> void
RBI
end

def test_print_record_type
rbi = parse_rbi(<<~RBI)
sig { returns({a: A, b: B}) }
def a; end
sig { returns({"a" => A, "b" => B}) }
def b; end
sig { returns({a: A, "b": B, :c => C, "d" => D}) }
def c; end
sig { returns({a: A, "B-B": B})}
def d; end
RBI

assert_equal(<<~RBI, rbi.rbs_string)
def a: -> {a: A, b: B}
def b: -> {"a" => A, "b" => B}
def c: -> {a: A, b: B, c: C, "d" => D}
def d: -> {a: A, "B-B": B}
RBI
end

def test_print_t_structs
rbi = parse_rbi(<<~RBI)
class Foo < T::Struct; end
Expand Down

0 comments on commit a1acaef

Please sign in to comment.