-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add flatten_scopes rewriter #108
Open
jeffcarbs
wants to merge
3
commits into
Shopify:main
Choose a base branch
from
jeffcarbs:flatten-scopes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RBI | ||
module Rewriters | ||
class FlattenScopes < Visitor | ||
extend T::Sig | ||
|
||
sig { override.params(node: T.nilable(Node)).void } | ||
def visit(node) | ||
return unless node | ||
|
||
case node | ||
when Tree | ||
visit_all(node.nodes) | ||
|
||
parent_tree = node.parent_tree | ||
return unless parent_tree | ||
|
||
node.nodes.dup.each do |child| | ||
next unless child.is_a?(Class) || child.is_a?(Module) | ||
Morriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
parent_scope = child.parent_scope | ||
next unless parent_scope.is_a?(Class) || parent_scope.is_a?(Module) | ||
|
||
child.detach | ||
child.name = "#{parent_scope.name}::#{child.name}" | ||
parent_tree << child | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Tree | ||
extend T::Sig | ||
|
||
sig { void } | ||
def flatten_scopes! | ||
visitor = Rewriters::FlattenScopes.new | ||
visitor.visit(self) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module RBI | ||
class FlattenScopesTest < Minitest::Test | ||
def test_flatten_scopes_with_empty_scopes | ||
rbi = RBI::Tree.new | ||
scope1 = RBI::Module.new("A") | ||
scope2 = RBI::Class.new("B") | ||
scope3 = RBI::Class.new("C") | ||
scope4 = RBI::Module.new("D") | ||
scope5 = RBI::Module.new("E") | ||
scope3 << scope4 | ||
scope2 << scope3 | ||
scope1 << scope2 | ||
rbi << scope1 | ||
rbi << scope5 | ||
Morriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rbi.flatten_scopes! | ||
|
||
assert_equal(<<~RBI, rbi.string) | ||
module A; end | ||
module E; end | ||
class A::B; end | ||
class A::B::C; end | ||
module A::B::C::D; end | ||
RBI | ||
end | ||
|
||
def test_flatten_scopes_with_nonempty_scopes | ||
rbi = RBI::Tree.new | ||
scope1 = RBI::Module.new("A") | ||
scope1 << RBI::Const.new("A1", "42") | ||
scope2 = RBI::Class.new("B") | ||
scope3 = RBI::Class.new("C") | ||
scope3 << RBI::Const.new("C1", "42") | ||
scope4 = RBI::Module.new("D") | ||
scope5 = RBI::Module.new("E") | ||
scope5 << RBI::Const.new("E1", "42") | ||
scope3 << scope4 | ||
scope2 << scope3 | ||
scope1 << scope2 | ||
rbi << scope1 | ||
rbi << scope5 | ||
jeffcarbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rbi.flatten_scopes! | ||
|
||
assert_equal(<<~RBI, rbi.string) | ||
module A | ||
A1 = 42 | ||
end | ||
|
||
module E | ||
E1 = 42 | ||
end | ||
|
||
class A::B; end | ||
|
||
class A::B::C | ||
C1 = 42 | ||
end | ||
|
||
module A::B::C::D; end | ||
RBI | ||
end | ||
|
||
def test_flatten_scopes_with_singleton_classes | ||
Morriar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rbi = RBI::Tree.new | ||
scope1 = RBI::Module.new("A") | ||
scope2 = RBI::Class.new("B") | ||
scope3 = RBI::Class.new("C") | ||
scope3_singleton = RBI::SingletonClass.new | ||
scope3_singleton << RBI::Method.new("m1") | ||
scope4 = RBI::Module.new("D") | ||
scope5 = RBI::Module.new("E") | ||
scope3 << scope4 | ||
scope3 << scope3_singleton | ||
scope2 << scope3 | ||
scope1 << scope2 | ||
rbi << scope1 | ||
rbi << scope5 | ||
|
||
rbi.flatten_scopes! | ||
|
||
assert_equal(<<~RBI, rbi.string) | ||
module A; end | ||
module E; end | ||
class A::B; end | ||
|
||
class A::B::C | ||
class << self | ||
def m1; end | ||
end | ||
end | ||
|
||
module A::B::C::D; end | ||
RBI | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Running
rbi.flatten_scopes!
will give us something like this: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 offlatten_scopes!
and always return a new root Tree instead of modifying the current one?