-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from Shopify/at-top-level-rewriter
Introduce NestTopLevelMembers rewriter
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
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,68 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RBI | ||
module Rewriters | ||
# This rewriter moves top-level members into a top-level Object class | ||
# | ||
# Example: | ||
# ~~~rb | ||
# def foo; end | ||
# attr_reader :bar | ||
# ~~~ | ||
# | ||
# will be rewritten to: | ||
# | ||
# ~~~rb | ||
# class Object | ||
# def foo; end | ||
# attr_reader :bar | ||
# end | ||
# ~~~ | ||
class NestTopLevelMembers < Visitor | ||
extend T::Sig | ||
|
||
sig { void } | ||
def initialize | ||
super | ||
|
||
@top_level_object_class = T.let(nil, T.nilable(Class)) | ||
end | ||
|
||
sig { override.params(node: T.nilable(Node)).void } | ||
def visit(node) | ||
return unless node | ||
|
||
case node | ||
when Tree | ||
visit_all(node.nodes.dup) | ||
else | ||
scope = node.parent_scope | ||
unless scope | ||
parent = node.parent_tree | ||
raise unless parent | ||
|
||
node.detach | ||
|
||
unless @top_level_object_class | ||
@top_level_object_class = Class.new("Object") | ||
parent.nodes << @top_level_object_class | ||
end | ||
|
||
@top_level_object_class << node | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Tree | ||
extend T::Sig | ||
|
||
sig { void } | ||
def nest_top_level_members! | ||
visitor = Rewriters::NestTopLevelMembers.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,89 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module RBI | ||
class NestTopLevelMembersTest < Minitest::Test | ||
include TestHelper | ||
|
||
def test_nest_top_level_members | ||
tree = parse_rbi(<<~RBI) | ||
module Foo | ||
def m1; end | ||
end | ||
def m2; end | ||
def self.m3; end | ||
attr_reader :foo | ||
send! | ||
class << self; end | ||
RBI | ||
|
||
tree.nest_top_level_members! | ||
|
||
assert_equal(<<~RBI, tree.string) | ||
module Foo | ||
def m1; end | ||
end | ||
class << self; end | ||
class Object | ||
def m2; end | ||
def self.m3; end | ||
attr_reader :foo | ||
send! | ||
end | ||
RBI | ||
end | ||
|
||
def test_nest_top_level_members_reuse_the_same_class | ||
tree = parse_rbi(<<~RBI) | ||
def foo; end | ||
class Foo; end | ||
def bar; end | ||
RBI | ||
|
||
tree.nest_top_level_members! | ||
|
||
assert_equal(<<~RBI, tree.string) | ||
class Foo; end | ||
class Object | ||
def foo; end | ||
def bar; end | ||
end | ||
RBI | ||
end | ||
|
||
def test_nest_top_level_members_duplicate_existing_object_class | ||
tree = parse_rbi(<<~RBI) | ||
class Object | ||
def foo; end | ||
end | ||
def bar; end | ||
class Object | ||
def baz; end | ||
end | ||
RBI | ||
|
||
tree.nest_top_level_members! | ||
|
||
assert_equal(<<~RBI, tree.string) | ||
class Object | ||
def foo; end | ||
end | ||
class Object | ||
def baz; end | ||
end | ||
class Object | ||
def bar; end | ||
end | ||
RBI | ||
end | ||
end | ||
end |