Skip to content

Conversation

@jeffcarbs
Copy link
Contributor

@jeffcarbs jeffcarbs commented Dec 1, 2021

Flattens scopes, so:

module A
  class B
    FOO = 42

    class C
      module D; end
    end
  end
end
module E; end

becomes:

module A; end
class A::B
  FOO = 42
end
class A::B::C; end
module A::B::C::D; end
module E; end

end
end
RBI
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add one more test showing the expected behaviour when the scopes are duplicated:

Suggested change
end
end
def test_flatten_duplicated_scopes
rbi = RBI::Parser.parse_string(<<~RBI)
module A
class B; end
end
module A
class C; end
end
module A
class D; end
end
RBI
rbi.flatten_scopes!
assert_equal(<<~RBI, rbi.string)
module A; end
module A; end
module A; end
class A::B; end
class A::C; end
class A::D; end
RBI
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should be smarter with this using an Index but I believe we can do this later if needed.

when Tree
visit_all(node.nodes)

parent_tree = node.parent_tree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern with this is that if we have nested trees, we will end up moving the node to the wrong place which might get it processed again later:

For example consider this:

rbi = Tree.new do |tree1|
  tree1 << Class.new("Foo") do |cls1|
    cls1 << Tree.new do |tree2|
      tree2 << Class.new("Bar") do |cls2|
        cls2 << Method.new("bar")
      end
    end
  end
end

Running rbi.flatten_scopes! will give us something like this:

class Foo; end

class Foo::Foo::Bar
  def bar; end
end

I wonder if we should first find the root tree and put everything in it as we go instead of trying to find the parent one?

Or maybe the method should be flatten_scopes instead of flatten_scopes! and always return a new root Tree instead of modifying the current one?

@paracycle paracycle requested a review from a team as a code owner August 12, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants