From 706751788c4a569a3b51bfd94108424a12db0e18 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 6 Aug 2024 12:12:09 -0400 Subject: [PATCH 1/2] Allow NestNonPublicMethods to also nest `Attr` instances Signed-off-by: Alexandre Terrasa --- lib/rbi/rewriters/nest_non_public_methods.rb | 2 +- test/rbi/rewriters/nest_non_public_methods_test.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rbi/rewriters/nest_non_public_methods.rb b/lib/rbi/rewriters/nest_non_public_methods.rb index 2da03b68..5810058c 100644 --- a/lib/rbi/rewriters/nest_non_public_methods.rb +++ b/lib/rbi/rewriters/nest_non_public_methods.rb @@ -18,7 +18,7 @@ def visit(node) node.nodes.dup.each do |child| visit(child) - next unless child.is_a?(Method) + next unless child.is_a?(Attr) || child.is_a?(Method) child.detach case child.visibility diff --git a/test/rbi/rewriters/nest_non_public_methods_test.rb b/test/rbi/rewriters/nest_non_public_methods_test.rb index 847ccb1e..a58cf050 100644 --- a/test/rbi/rewriters/nest_non_public_methods_test.rb +++ b/test/rbi/rewriters/nest_non_public_methods_test.rb @@ -12,14 +12,14 @@ def test_nest_non_public_methods_in_tree def m1; end protected def m2; end private def m3; end - public def m4; end + public attr_reader :m4 RBI tree.nest_non_public_methods! assert_equal(<<~RBI, tree.string) def m1; end - def m4; end + attr_reader :m4 protected @@ -39,7 +39,7 @@ def m1; end class S2 def m3; end - private def m4; end + private attr_reader :m4 class << self def m5; end @@ -66,7 +66,7 @@ def m3; end private - def m4; end + attr_reader :m4 end def m1; end From 36302fc33b24011e6cb219f21b0e041c8dba63d8 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 6 Aug 2024 12:12:56 -0400 Subject: [PATCH 2/2] Rename `NestNonPublicMethods` into `NestNonPublicMembers` Breaking API change. Signed-off-by: Alexandre Terrasa --- lib/rbi.rb | 2 +- lib/rbi/formatter.rb | 8 ++++---- ..._public_methods.rb => nest_non_public_members.rb} | 6 +++--- test/rbi/formatter_test.rb | 4 ++-- ...thods_test.rb => nest_non_public_members_test.rb} | 12 ++++++------ 5 files changed, 16 insertions(+), 16 deletions(-) rename lib/rbi/rewriters/{nest_non_public_methods.rb => nest_non_public_members.rb} (91%) rename test/rbi/rewriters/{nest_non_public_methods_test.rb => nest_non_public_members_test.rb} (85%) diff --git a/lib/rbi.rb b/lib/rbi.rb index 9c224f67..03607dee 100644 --- a/lib/rbi.rb +++ b/lib/rbi.rb @@ -21,8 +21,8 @@ class Error < StandardError require "rbi/rewriters/filter_versions" require "rbi/rewriters/flatten_singleton_methods" require "rbi/rewriters/merge_trees" +require "rbi/rewriters/nest_non_public_members" require "rbi/rewriters/nest_singleton_methods" -require "rbi/rewriters/nest_non_public_methods" require "rbi/rewriters/nest_top_level_members" require "rbi/rewriters/group_nodes" require "rbi/rewriters/remove_known_definitions" diff --git a/lib/rbi/formatter.rb b/lib/rbi/formatter.rb index 565d4c1c..97b8cdf0 100644 --- a/lib/rbi/formatter.rb +++ b/lib/rbi/formatter.rb @@ -17,7 +17,7 @@ class Formatter group_nodes: T::Boolean, max_line_length: T.nilable(Integer), nest_singleton_methods: T::Boolean, - nest_non_public_methods: T::Boolean, + nest_non_public_members: T::Boolean, sort_nodes: T::Boolean, ).void end @@ -26,14 +26,14 @@ def initialize( group_nodes: false, max_line_length: nil, nest_singleton_methods: false, - nest_non_public_methods: false, + nest_non_public_members: false, sort_nodes: false ) @add_sig_templates = add_sig_templates @group_nodes = group_nodes @max_line_length = max_line_length @nest_singleton_methods = nest_singleton_methods - @nest_non_public_methods = nest_non_public_methods + @nest_non_public_members = nest_non_public_members @sort_nodes = sort_nodes end @@ -52,7 +52,7 @@ def format_file(file) def format_tree(tree) tree.add_sig_templates! if @add_sig_templates tree.nest_singleton_methods! if @nest_singleton_methods - tree.nest_non_public_methods! if @nest_non_public_methods + tree.nest_non_public_members! if @nest_non_public_members tree.group_nodes! if @group_nodes tree.sort_nodes! if @sort_nodes end diff --git a/lib/rbi/rewriters/nest_non_public_methods.rb b/lib/rbi/rewriters/nest_non_public_members.rb similarity index 91% rename from lib/rbi/rewriters/nest_non_public_methods.rb rename to lib/rbi/rewriters/nest_non_public_members.rb index 5810058c..2939d896 100644 --- a/lib/rbi/rewriters/nest_non_public_methods.rb +++ b/lib/rbi/rewriters/nest_non_public_members.rb @@ -3,7 +3,7 @@ module RBI module Rewriters - class NestNonPublicMethods < Visitor + class NestNonPublicMembers < Visitor extend T::Sig sig { override.params(node: T.nilable(Node)).void } @@ -43,8 +43,8 @@ class Tree extend T::Sig sig { void } - def nest_non_public_methods! - visitor = Rewriters::NestNonPublicMethods.new + def nest_non_public_members! + visitor = Rewriters::NestNonPublicMembers.new visitor.visit(self) end end diff --git a/test/rbi/formatter_test.rb b/test/rbi/formatter_test.rb index f0b311c8..462b7c41 100644 --- a/test/rbi/formatter_test.rb +++ b/test/rbi/formatter_test.rb @@ -117,10 +117,10 @@ module Foo file = File.new file.root = parse_rbi(rbi) - out = Formatter.new(nest_non_public_methods: false).print_file(file) + out = Formatter.new(nest_non_public_members: false).print_file(file) assert_equal(rbi, out) - out = Formatter.new(nest_non_public_methods: true).print_file(file) + out = Formatter.new(nest_non_public_members: true).print_file(file) assert_equal(<<~RBI, out) module Foo private diff --git a/test/rbi/rewriters/nest_non_public_methods_test.rb b/test/rbi/rewriters/nest_non_public_members_test.rb similarity index 85% rename from test/rbi/rewriters/nest_non_public_methods_test.rb rename to test/rbi/rewriters/nest_non_public_members_test.rb index a58cf050..b3459d65 100644 --- a/test/rbi/rewriters/nest_non_public_methods_test.rb +++ b/test/rbi/rewriters/nest_non_public_members_test.rb @@ -4,10 +4,10 @@ require "test_helper" module RBI - class NestNonPublicMethodsTest < Minitest::Test + class NestNonPublicMembersTest < Minitest::Test include TestHelper - def test_nest_non_public_methods_in_tree + def test_nest_non_public_members_in_tree tree = parse_rbi(<<~RBI) def m1; end protected def m2; end @@ -15,7 +15,7 @@ def m1; end public attr_reader :m4 RBI - tree.nest_non_public_methods! + tree.nest_non_public_members! assert_equal(<<~RBI, tree.string) def m1; end @@ -31,7 +31,7 @@ def m3; end RBI end - def test_nest_non_public_methods_in_scopes + def test_nest_non_public_members_in_scopes tree = parse_rbi(<<~RBI) module S1 def m1; end @@ -49,7 +49,7 @@ def m5; end end RBI - tree.nest_non_public_methods! + tree.nest_non_public_members! assert_equal(<<~RBI, tree.string) module S1 @@ -85,7 +85,7 @@ def test_nest_non_public_singleton_methods public def self.m3; end RBI - tree.nest_non_public_methods! + tree.nest_non_public_members! assert_equal(<<~RBI, tree.string) def self.m3; end