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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RBI generation for gems #303

Merged
merged 2 commits into from
May 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/tapioca/compilers/symbol_table/symbol_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,12 @@ def compile_signature(signature, parameters)
sig = RBI::Sig.new

parameters.each do |_, name|
type = parameter_types[name.to_sym].to_s
.gsub(".returns(<VOID>)", ".void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")

type = sanitize_signature_types(parameter_types[name.to_sym].to_s)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Factorized the sanitization so both params and return types are handled the same.

sig << RBI::SigParam.new(name, type)
end

sig.return_type = type_of(signature.return_type)
.gsub("<VOID>", "void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")
return_type = type_of(signature.return_type)
sig.return_type = sanitize_signature_types(return_type)

parameter_types.values.join(", ").scan(TYPE_PARAMETER_MATCHER).flatten.uniq.each do |k, _|
sig.type_params << k
Expand Down Expand Up @@ -832,6 +826,15 @@ def signature_of(method)
nil
end

sig { params(sig_string: String).returns(String) }
def sanitize_signature_types(sig_string)
sig_string
.gsub(".returns(<VOID>)", ".void")
.gsub("<VOID>", "void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")
end

sig { params(constant: T::Types::Base).returns(String) }
def type_of(constant)
constant.to_s.gsub(/\bAttachedClass\b/, "T.attached_class")
Expand Down
27 changes: 12 additions & 15 deletions lib/tapioca/rbi/rewriters/group_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ class Node
sig { returns(Group::Kind) }
def group_kind
case self
when Include
Group::Kind::Includes
when Extend
Group::Kind::Extends
when Include, Extend
Group::Kind::Mixins
when Helper
Group::Kind::Helpers
when TypeMember
Group::Kind::TypeMembers
when MixesInClassMethods
Group::Kind::Mixes
Group::Kind::MixesInClassMethods
when TStructField
Group::Kind::TStructFields
when TEnumBlock
Expand Down Expand Up @@ -92,16 +90,15 @@ def initialize(kind)

class Kind < T::Enum
enums do
Includes = new
Extends = new
Helpers = new
TypeMembers = new
Mixes = new
TStructFields = new
TEnums = new
Inits = new
Methods = new
Consts = new
Mixins = new
Helpers = new
TypeMembers = new
MixesInClassMethods = new
TStructFields = new
TEnums = new
Inits = new
Methods = new
Consts = new
end
end
end
Expand Down
32 changes: 15 additions & 17 deletions lib/tapioca/rbi/rewriters/sort_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ def visit(node)
def node_rank(node)
case node
when Group then kind_rank(node.kind)
when Include then 0
when Extend then 1
when Helper then 2
when TypeMember then 3
when MixesInClassMethods then 4
when TStructField then 5
when TEnumBlock then 6
when Include, Extend then 0
when Helper then 1
when TypeMember then 2
when MixesInClassMethods then 3
when TStructField then 4
when TEnumBlock then 5
when Method
if node.name == "initialize"
7
Expand All @@ -46,16 +45,15 @@ def node_rank(node)
sig { params(kind: Group::Kind).returns(Integer) }
def kind_rank(kind)
case kind
when Group::Kind::Includes then 0
when Group::Kind::Extends then 1
when Group::Kind::Helpers then 2
when Group::Kind::TypeMembers then 3
when Group::Kind::Mixes then 4
when Group::Kind::TStructFields then 5
when Group::Kind::TEnums then 6
when Group::Kind::Inits then 7
when Group::Kind::Methods then 8
when Group::Kind::Consts then 9
when Group::Kind::Mixins then 0
when Group::Kind::Helpers then 1
when Group::Kind::TypeMembers then 2
when Group::Kind::MixesInClassMethods then 3
when Group::Kind::TStructFields then 4
when Group::Kind::TEnums then 5
when Group::Kind::Inits then 6
when Group::Kind::Methods then 7
when Group::Kind::Consts then 8
else
T.absurd(kind)
end
Expand Down
1 change: 0 additions & 1 deletion spec/tapioca/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module Baz; end

class Baz::Role
include ::SmartProperties

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No more newline here since it's in the same group now.

extend ::SmartProperties::ClassMethods
end

Expand Down
15 changes: 8 additions & 7 deletions spec/tapioca/compilers/symbol_table_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class Bar
include ::ModuleA
include ::ModuleB
include ::ModuleC

extend ::ModuleC
extend ::ModuleB
extend ::ModuleA
Expand Down Expand Up @@ -226,7 +225,6 @@ class String
include ::Comparable
include ::JSON::Ext::Generator::GeneratorMethods::String
include ::Colorize::InstanceMethods

extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend
extend ::Colorize::ClassMethods

Expand Down Expand Up @@ -746,7 +744,6 @@ class Bar < Baz
class Bar < ::Baz
include ::Tutu
include ::Foo

extend ::Toto
end

Expand Down Expand Up @@ -1555,10 +1552,9 @@ def another_class_method; end
end

module Baz
extend ::SomeOtherConcern
include ::FooConcern
include ::BarConcern

extend ::SomeOtherConcern
end

module Concern
Expand Down Expand Up @@ -1678,9 +1674,8 @@ module ClassMethods
module ActiveModel; end

module ActiveModel::Validations
include ::ActiveModel::Validations::HelperMethods

extend ::ActiveSupport::Concern
include ::ActiveModel::Validations::HelperMethods

mixes_in_class_methods ::ActiveModel::Validations::ClassMethods
end
Expand Down Expand Up @@ -2055,6 +2050,9 @@ def baz(a)
def many_kinds_of_args(*a, b, c, d:, e: 42, **f, &blk)
end

sig { returns(T.proc.params(x: String).void) }
attr_reader :some_attribute

class << self
extend(T::Sig)

Expand Down Expand Up @@ -2241,6 +2239,9 @@ def foo(a, b:); end
sig { params(a: Integer, b: Integer, c: Integer, d: Integer, e: Integer, f: Integer, blk: T.proc.void).void }
def many_kinds_of_args(*a, b, c, d:, e: T.unsafe(nil), **f, &blk); end

sig { returns(T.proc.params(x: String).void) }
def some_attribute; end

class << self
sig { void }
def quux; end
Expand Down
26 changes: 9 additions & 17 deletions spec/tapioca/rbi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ def self.m2; end
rbi << RBI::Class.new("S2")
rbi << RBI::Method.new("m1")
rbi << RBI::Method.new("m2", is_singleton: true)
rbi << RBI::Include.new("I")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just changed the order in a few tests to show that we keep it when compiling.

rbi << RBI::Extend.new("E")
rbi << RBI::Include.new("I")
rbi << RBI::MixesInClassMethods.new("MICM")
rbi << RBI::Helper.new("h")
rbi << RBI::TStructConst.new("SC", "Type")
Expand All @@ -444,9 +444,8 @@ def self.m2; end
rbi.sort_nodes!

assert_equal(<<~RBI, rbi.string)
include I

extend E
include I

h!

Expand Down Expand Up @@ -523,8 +522,8 @@ class Scope3
rbi << RBI::Class.new("S2")
rbi << RBI::Method.new("m1")
rbi << RBI::Method.new("m2", is_singleton: true)
rbi << RBI::Include.new("I")
rbi << RBI::Extend.new("E")
rbi << RBI::Include.new("I")
rbi << RBI::MixesInClassMethods.new("MICM")
rbi << RBI::Helper.new("h")
rbi << RBI::TStructConst.new("SC", "Type")
Expand All @@ -536,9 +535,8 @@ class Scope3
rbi.sort_nodes!

assert_equal(<<~RBI, rbi.string)
include I

extend E
include I

h!

Expand Down Expand Up @@ -572,9 +570,8 @@ class TS < ::T::Struct; end

assert_equal(<<~RBI, rbi.string)
include I2
include I1

extend E2
include I1
extend E1

mixes_in_class_methods M2
Expand Down Expand Up @@ -626,7 +623,6 @@ class TS < ::T::Struct; end
assert_equal(<<~RBI, rbi.string)
module Scope
include I

extend E

h!
Expand Down Expand Up @@ -683,9 +679,8 @@ class TS < ::T::Struct; end

assert_equal(<<~RBI, rbi.string)
include I2
include I1

extend E2
include I1
extend E1

h1!
Expand Down Expand Up @@ -806,9 +801,8 @@ class TS2 < ::T::Struct; end
assert_equal(<<~RBI, rbi.string)
class Scope1
include I2
include I1

extend E2
include I1
extend E1

h1!
Expand Down Expand Up @@ -840,9 +834,8 @@ class TS2 < ::T::Struct; end

class Scope2
include I2
include I1

extend E2
include I1
extend E1

h1!
Expand All @@ -869,9 +862,8 @@ module S2; end

class Scope2.1
include I2
include I1

extend E2
include I1
extend E1

h1!
Expand Down