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

Fix RBS translation of shape string keys #392

Merged
merged 1 commit into from
Jan 15, 2025
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
11 changes: 10 additions & 1 deletion lib/rbi/rbs_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,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 @@ -530,6 +530,32 @@ def foo: -> instance
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
Loading