From c4bae4a0d2ded19d9e76fb1a7706770ce519f68f Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 5 Aug 2024 12:31:14 -0400 Subject: [PATCH 1/2] Fix parsing of T::Structs Signed-off-by: Alexandre Terrasa --- lib/rbi/parser.rb | 6 ++++++ test/rbi/parser_test.rb | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/rbi/parser.rb b/lib/rbi/parser.rb index bfecfdf8..4a0de8f5 100644 --- a/lib/rbi/parser.rb +++ b/lib/rbi/parser.rb @@ -177,6 +177,12 @@ def visit_class_node(node) @last_node = node superclass_name = node_string(node.superclass) scope = case superclass_name + when /^(::)?T::Struct$/ + TStruct.new( + node_string!(node.constant_path), + loc: node_loc(node), + comments: node_comments(node), + ) when /^(::)?T::Enum$/ TEnum.new( node_string!(node.constant_path), diff --git a/test/rbi/parser_test.rb b/test/rbi/parser_test.rb index a8f89804..19a6956f 100644 --- a/test/rbi/parser_test.rb +++ b/test/rbi/parser_test.rb @@ -254,6 +254,10 @@ def foo; end RBI tree = parse_rbi(rbi) + + # Make sure the T::Struct is not parsed as a normal class + assert_equal(TStruct, tree.nodes.first.class) + assert_equal(<<~RBI, tree.string) class Foo < ::T::Struct const :a, A From 390539e7802a4b6260c55f8751a78061bf400c11 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 5 Aug 2024 13:11:49 -0400 Subject: [PATCH 2/2] Fix printing of T::Structs Signed-off-by: Alexandre Terrasa --- lib/rbi/printer.rb | 4 ++-- lib/rbi/rewriters/merge_trees.rb | 2 ++ lib/rbi/visitor.rb | 4 ++-- test/rbi/model_test.rb | 4 ++-- test/rbi/parser_test.rb | 8 ++++---- test/rbi/printer_test.rb | 6 +++--- test/rbi/rewriters/group_nodes_test.rb | 22 +++++++++++----------- test/rbi/rewriters/sort_nodes_test.rb | 12 ++++++------ 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/rbi/printer.rb b/lib/rbi/printer.rb index 30498c71..4c22ffa6 100644 --- a/lib/rbi/printer.rb +++ b/lib/rbi/printer.rb @@ -171,6 +171,8 @@ def visit_scope_header(node) printt("module #{node.name}") when TEnum printt("class #{node.name} < T::Enum") + when TStruct + printt("class #{node.name} < T::Struct") when Class printt("class #{node.name}") superclass = node.superclass_name @@ -186,8 +188,6 @@ def visit_scope_header(node) end when SingletonClass printt("class << self") - when TStruct - printt("class #{node.name} < T::Struct") else raise PrinterError, "Unhandled node: #{node.class}" end diff --git a/lib/rbi/rewriters/merge_trees.rb b/lib/rbi/rewriters/merge_trees.rb index b840d47e..3809e5f1 100644 --- a/lib/rbi/rewriters/merge_trees.rb +++ b/lib/rbi/rewriters/merge_trees.rb @@ -361,6 +361,8 @@ def dup_empty Module.new(name, loc: loc, comments: comments) when TEnum TEnum.new(name, loc: loc, comments: comments) + when TStruct + TStruct.new(name, loc: loc, comments: comments) when Class Class.new(name, superclass_name: superclass_name, loc: loc, comments: comments) when Struct diff --git a/lib/rbi/visitor.rb b/lib/rbi/visitor.rb index 746c91b6..7d594cba 100644 --- a/lib/rbi/visitor.rb +++ b/lib/rbi/visitor.rb @@ -21,6 +21,8 @@ def visit(node) visit_comment(node) when TEnum visit_tenum(node) + when TStruct + visit_tstruct(node) when Module visit_module(node) when Class @@ -83,8 +85,6 @@ def visit(node) visit_sig(node) when SigParam visit_sig_param(node) - when TStruct - visit_tstruct(node) when TStructConst visit_tstruct_const(node) when TStructProp diff --git a/test/rbi/model_test.rb b/test/rbi/model_test.rb index 0a0393ad..7e5fe0df 100644 --- a/test/rbi/model_test.rb +++ b/test/rbi/model_test.rb @@ -174,7 +174,7 @@ def foo( # comment private - class Struct < ::T::Struct + class Struct < T::Struct # comment const :foo, Foo @@ -457,7 +457,7 @@ def test_t_struct_with_types node << TStructProp.new("bar", Type.simple("Bar")) assert_equal(<<~RBI, node.string) - class MyStruct < ::T::Struct + class MyStruct < T::Struct const :foo, Foo prop :bar, Bar end diff --git a/test/rbi/parser_test.rb b/test/rbi/parser_test.rb index 19a6956f..8c6fa69c 100644 --- a/test/rbi/parser_test.rb +++ b/test/rbi/parser_test.rb @@ -244,7 +244,7 @@ def test_parse_visibility_labels_with_comments def test_parse_t_struct rbi = <<~RBI - class Foo < ::T::Struct + class Foo < T::Struct const :a, A const :b, B, default: B.new prop :c, C @@ -259,7 +259,7 @@ def foo; end assert_equal(TStruct, tree.nodes.first.class) assert_equal(<<~RBI, tree.string) - class Foo < ::T::Struct + class Foo < T::Struct const :a, A const :b, B, default: B.new prop :c, C @@ -566,7 +566,7 @@ def m3; end def test_parse_t_struct_locations rbi = <<~RBI - class Foo < ::T::Struct + class Foo < T::Struct const :a, A const :b, B, default: B.new prop :c, C @@ -578,7 +578,7 @@ def foo; end tree = parse_rbi(rbi) assert_equal(<<~RBI, tree.string(print_locs: true)) # -:1:0-7:3 - class Foo < ::T::Struct + class Foo < T::Struct # -:2:2-2:13 const :a, A # -:3:2-3:29 diff --git a/test/rbi/printer_test.rb b/test/rbi/printer_test.rb index a2ecd730..ace42e38 100644 --- a/test/rbi/printer_test.rb +++ b/test/rbi/printer_test.rb @@ -282,7 +282,7 @@ def test_print_t_structs struct << Method.new("foo") assert_equal(<<~RBI, struct.string) - class Foo < ::T::Struct + class Foo < T::Struct const :a, A const :b, B, default: B.new prop :c, C @@ -450,7 +450,7 @@ class << self; end foo # This is a single line comment - class Foo < ::T::Struct + class Foo < T::Struct # This is a # Multiline Comment const :a, A @@ -1098,7 +1098,7 @@ class << self; end # file.rbi:1:3-2:4 class TE < T::Enum; end # file.rbi:1:3-2:4 - class TS < ::T::Struct; end + class TS < T::Struct; end # file.rbi:1:3-2:4 C = 42 # file.rbi:1:3-2:4 diff --git a/test/rbi/rewriters/group_nodes_test.rb b/test/rbi/rewriters/group_nodes_test.rb index b989de45..0f2ec2bb 100644 --- a/test/rbi/rewriters/group_nodes_test.rb +++ b/test/rbi/rewriters/group_nodes_test.rb @@ -63,7 +63,7 @@ module S1; end class S2; end S3 = ::Struct.new class TE < T::Enum; end - class TS < ::T::Struct; end + class TS < T::Struct; end RBI end @@ -172,7 +172,7 @@ module S1; end class S2; end S3 = ::Struct.new class TE < T::Enum; end - class TS < ::T::Struct; end + class TS < T::Struct; end RBI end @@ -293,7 +293,7 @@ module S1; end class S2; end S3 = ::Struct.new class TE < T::Enum; end - class TS < ::T::Struct; end + class TS < T::Struct; end end RBI end @@ -367,8 +367,8 @@ module S2; end S3 = ::Struct.new class TE1 < T::Enum; end class TE2 < T::Enum; end - class TS1 < ::T::Struct; end - class TS2 < ::T::Struct; end + class TS1 < T::Struct; end + class TS2 < T::Struct; end RBI end @@ -502,8 +502,8 @@ module S2; end S3 = ::Struct.new class TE1 < T::Enum; end class TE2 < T::Enum; end - class TS1 < ::T::Struct; end - class TS2 < ::T::Struct; end + class TS1 < T::Struct; end + class TS2 < T::Struct; end end class Scope2 @@ -571,14 +571,14 @@ module S2; end S3 = ::Struct.new class TE1 < T::Enum; end class TE2 < T::Enum; end - class TS1 < ::T::Struct; end - class TS2 < ::T::Struct; end + class TS1 < T::Struct; end + class TS2 < T::Struct; end end class TE1 < T::Enum; end class TE2 < T::Enum; end - class TS1 < ::T::Struct; end - class TS2 < ::T::Struct; end + class TS1 < T::Struct; end + class TS2 < T::Struct; end end RBI end diff --git a/test/rbi/rewriters/sort_nodes_test.rb b/test/rbi/rewriters/sort_nodes_test.rb index fc1f547c..9682d167 100644 --- a/test/rbi/rewriters/sort_nodes_test.rb +++ b/test/rbi/rewriters/sort_nodes_test.rb @@ -183,10 +183,10 @@ def test_sort_tstructs rbi.sort_nodes! assert_equal(<<~RBI, rbi.string) - class A < ::T::Struct; end - class B < ::T::Struct; end - class C < ::T::Struct; end - class D < ::T::Struct; end + class A < T::Struct; end + class B < T::Struct; end + class C < T::Struct; end + class D < T::Struct; end RBI end @@ -247,7 +247,7 @@ def self.m3; end A = 42 module B; end class C < T::Enum; end - class D < ::T::Struct; end + class D < T::Struct; end class E; end RBI end @@ -306,7 +306,7 @@ class << self; end A = 42 module B; end class C < T::Enum; end - class D < ::T::Struct; end + class D < T::Struct; end class E; end RBI end