Skip to content

Commit a387e94

Browse files
committed
Support constant diff
1 parent a7ae993 commit a387e94

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

lib/rbs/diff.rb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ def initialize(type_name:, library_options:, after_path: [], before_path: [])
8585
def each_diff(&block)
8686
return to_enum(:each_diff) unless block
8787

88-
before_instance_methods, before_singleton_methods = build_methods(@before_path)
89-
after_instance_methods, after_singleton_methods = build_methods(@after_path)
88+
before_instance_methods, before_singleton_methods, before_constant_decls = build_methods(@before_path)
89+
after_instance_methods, after_singleton_methods, after_constant_decls = build_methods(@after_path)
9090

91-
each_diff_by(:instance, before_instance_methods, after_instance_methods, &block)
92-
each_diff_by(:singleton, before_singleton_methods, after_singleton_methods, &block)
91+
each_diff_methods(:instance, before_instance_methods, after_instance_methods, &block)
92+
each_diff_methods(:singleton, before_singleton_methods, after_singleton_methods, &block)
93+
94+
each_diff_constants(before_constant_decls, after_constant_decls, &block)
9395
end
9496

9597
private
9698

97-
def each_diff_by(kind, before_methods, after_methods)
99+
def each_diff_methods(kind, before_methods, after_methods)
98100
all_keys = before_methods.keys.to_set + after_methods.keys.to_set
99101
all_keys.each do |key|
100102
before = definition_method_to_s(key, kind, before_methods[key]) or next
@@ -105,6 +107,17 @@ def each_diff_by(kind, before_methods, after_methods)
105107
end
106108
end
107109

110+
def each_diff_constants(before_constant_decls, after_constant_decls)
111+
all_keys = before_constant_decls.keys.to_set + after_constant_decls.keys.to_set
112+
all_keys.each do |key|
113+
before = constant_to_s(key, before_constant_decls[key]) or next
114+
after = constant_to_s(key, after_constant_decls[key]) or next
115+
next if before == after
116+
117+
yield before, after
118+
end
119+
end
120+
108121
def build_methods(path)
109122
env = build_env(path)
110123
builder = build_builder(env)
@@ -121,8 +134,10 @@ def build_methods(path)
121134
RBS.logger.warn("#{path}: #{e.message}")
122135
{}
123136
end
137+
type_name_to_s = @type_name.to_s
138+
constant_decls = env.constant_decls.select { |key| key.to_s.start_with?(type_name_to_s) }
124139

125-
[ instance_methods, singleton_methods ]
140+
[ instance_methods, singleton_methods, constant_decls ]
126141
end
127142

128143
def build_env(path)
@@ -147,7 +162,15 @@ def definition_method_to_s(key, kind, definition_method)
147162
"def #{prefix}#{key}: #{definition_method.method_types.join(" | ")}"
148163
end
149164
else
150-
+"-"
165+
"-"
166+
end
167+
end
168+
169+
def constant_to_s(key, constant)
170+
if constant
171+
"#{key}: #{constant.decl.type}"
172+
else
173+
"-"
151174
end
152175
end
153176
end

test/rbs/diff_test.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def bar: () -> void
2222
def self.baz: () -> (Integer | String)
2323
def qux: (untyped) -> untyped
2424
def quux: () -> void
25+
26+
SAME_MOD_SAME_VALUE: 1
27+
SAME_MOD_OTHER_VALUE: 2
28+
SAME_MOD_BEFORE_ONLY: 3
29+
OTHER_MOD_SAME_VALUE: 4
30+
OTHER_MOD_OTHER_VALUE: 5
2531
end
2632
RBS
2733

@@ -30,6 +36,8 @@ def quux: () -> void
3036
(dir2 / 'after.rbs').write(<<~RBS)
3137
module Bar
3238
def bar: () -> void
39+
OTHER_MOD_SAME_VALUE: 4
40+
OTHER_MOD_OTHER_VALUE: Array[Integer]
3341
end
3442
3543
module Baz
@@ -40,6 +48,9 @@ class Foo
4048
include Bar
4149
extend Baz
4250
alias quux bar
51+
SAME_MOD_SAME_VALUE: 1
52+
SAME_MOD_OTHER_VALUE: String
53+
SAME_MOD_AFTER_ONLY: 3
4354
end
4455
RBS
4556

@@ -55,7 +66,12 @@ class Foo
5566
assert_equal [
5667
["def qux: (untyped) -> untyped", "-"],
5768
["def quux: () -> void", "alias quux bar"],
58-
["def self.baz: () -> (::Integer | ::String)", "def self.baz: (::Integer) -> ::Integer?"]
69+
["def self.baz: () -> (::Integer | ::String)", "def self.baz: (::Integer) -> ::Integer?"],
70+
["::Foo::SAME_MOD_OTHER_VALUE: 2", "::Foo::SAME_MOD_OTHER_VALUE: String"],
71+
["::Foo::SAME_MOD_BEFORE_ONLY: 3", "-"],
72+
["::Foo::OTHER_MOD_SAME_VALUE: 4", "-"],
73+
["::Foo::OTHER_MOD_OTHER_VALUE: 5", "-"],
74+
["-", "::Foo::SAME_MOD_AFTER_ONLY: 3"]
5975
], results
6076
end
6177
end

0 commit comments

Comments
 (0)