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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --detail option to rbs diff #1623

Merged
merged 2 commits into from
Nov 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/rbs/cli/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
library_options = library_options
before_path = []
after_path = []
detail = false

opt = OptionParser.new do |o|
o.banner = <<~HELP
Expand All @@ -35,6 +36,7 @@ def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
o.on("--type-name NAME") { |arg| type_name = arg }
o.on("--before DIR") { |arg| before_path << arg }
o.on("--after DIR") { |arg| after_path << arg }
o.on("--[no-]detail") { |arg| detail = arg }
end
opt.parse!(argv)

Expand All @@ -47,7 +49,8 @@ def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
type_name: TypeName(type_name).absolute!,
library_options: library_options,
after_path: after_path,
before_path: before_path
before_path: before_path,
detail: detail,
)
end

Expand Down
11 changes: 7 additions & 4 deletions lib/rbs/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

module RBS
class Diff
def initialize(type_name:, library_options:, after_path: [], before_path: [])
def initialize(type_name:, library_options:, after_path: [], before_path: [], detail: false)
@type_name = type_name
@library_options = library_options
@after_path = after_path
@before_path = before_path
@detail = detail
end

def each_diff(&block)
Expand Down Expand Up @@ -100,11 +101,12 @@ def definition_method_to_s(key, kind, definition_method)
if definition_method
prefix = kind == :instance ? "" : "self."

detail_to_s = @detail ? "[#{definition_method.defined_in} #{definition_method.accessibility}] " : ""
if definition_method.alias_of
first_def = definition_method.alias_of.defs.first #: Definition::Method::TypeDef
"alias #{prefix}#{key} #{prefix}#{first_def.member.name}"
"#{detail_to_s}alias #{prefix}#{key} #{prefix}#{first_def.member.name}"
else
"def #{prefix}#{key}: #{definition_method.method_types.join(" | ")}"
"#{detail_to_s}def #{prefix}#{key}: #{definition_method.method_types.join(" | ")}"
end
else
+"-"
Expand All @@ -113,7 +115,8 @@ def definition_method_to_s(key, kind, definition_method)

def constant_to_s(constant)
if constant
"#{constant.name.name}: #{constant.type}"
detail_to_s = @detail ? "[#{constant.name.namespace.to_type_name.to_s}] " : ""
"#{detail_to_s}#{constant.name.name}: #{constant.type}"
else
+"-"
end
Expand Down
4 changes: 3 additions & 1 deletion sig/diff.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ module RBS
@library_options: RBS::CLI::LibraryOptions
@after_path: Array[String]
@before_path: Array[String]
@detail: boolish

def initialize: (
type_name: TypeName,
library_options: RBS::CLI::LibraryOptions,
?after_path: Array[String],
?before_path: Array[String]
?before_path: Array[String],
?detail: boolish
) -> void

def each_diff: () { (String before, String after) -> void } -> void
Expand Down
66 changes: 66 additions & 0 deletions test/rbs/diff_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,70 @@ class Foo
], results
end
end

def test_detail
mktmpdir do |path|
dir1 = (path / "dir1")
dir1.mkdir
(dir1 / 'before.rbs').write(<<~RBS)
class Foo
def bar: () -> void
def self.baz: () -> (Integer | String)
def qux: (untyped) -> untyped
def quux: () -> void

SAME_MOD_SAME_VALUE: 1
SAME_MOD_OTHER_VALUE: 2
SAME_MOD_BEFORE_ONLY: 3
OTHER_MOD_SAME_VALUE: 4
OTHER_MOD_OTHER_VALUE: 5
end
RBS

dir2 = (path / "dir2")
dir2.mkdir
(dir2 / 'after.rbs').write(<<~RBS)
module Bar
def bar: () -> void
OTHER_MOD_SAME_VALUE: 4
OTHER_MOD_OTHER_VALUE: Array[Integer]
end

module Baz
def baz: (Integer) -> Integer?
end

class Foo
include Bar
extend Baz
alias quux bar
SAME_MOD_SAME_VALUE: 1
SAME_MOD_OTHER_VALUE: String
SAME_MOD_AFTER_ONLY: 3
end
RBS

diff = Diff.new(
type_name: TypeName("::Foo"),
library_options: RBS::CLI::LibraryOptions.new,
before_path: [dir1],
after_path: [dir2],
detail: true,
)
results = diff.each_diff.map do |before, after|
[before, after]
end
assert_equal [
["[::Foo public] def bar: () -> void", "[::Bar public] def bar: () -> void"],
["[::Foo public] def qux: (untyped) -> untyped", "-"],
["[::Foo public] def quux: () -> void", "[::Foo public] alias quux bar"],
["[::Foo public] def self.baz: () -> (::Integer | ::String)", "[::Baz public] def self.baz: (::Integer) -> ::Integer?"],
["[::Foo] SAME_MOD_OTHER_VALUE: 2", "[::Foo] SAME_MOD_OTHER_VALUE: ::String"],
["[::Foo] SAME_MOD_BEFORE_ONLY: 3", "-"],
["[::Foo] OTHER_MOD_SAME_VALUE: 4", "[::Bar] OTHER_MOD_SAME_VALUE: 4"],
["[::Foo] OTHER_MOD_OTHER_VALUE: 5", "[::Bar] OTHER_MOD_OTHER_VALUE: ::Array[::Integer]"],
["-", "[::Foo] SAME_MOD_AFTER_ONLY: 3"]
], results
end
end
end