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

Allow NestNonPublicMethods to also nest Attr instances #351

Merged
merged 2 commits into from
Aug 7, 2024
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
2 changes: 1 addition & 1 deletion lib/rbi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions lib/rbi/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/rbi/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
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
private def m3; end
public def m4; end
public attr_reader :m4
RBI

tree.nest_non_public_methods!
tree.nest_non_public_members!

assert_equal(<<~RBI, tree.string)
def m1; end
def m4; end
attr_reader :m4

protected

Expand All @@ -31,15 +31,15 @@ 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
protected def m2; end

class S2
def m3; end
private def m4; end
private attr_reader :m4

class << self
def m5; end
Expand All @@ -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
Expand All @@ -66,7 +66,7 @@ def m3; end

private

def m4; end
attr_reader :m4
end

def m1; end
Expand All @@ -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
Expand Down
Loading