From 454a873b88d52c9132a5792adc86adad6af459e2 Mon Sep 17 00:00:00 2001 From: ksss Date: Fri, 17 Nov 2023 15:15:22 +0900 Subject: [PATCH] Add `--detail` option to rbs diff --- lib/rbs/cli/diff.rb | 5 +++- lib/rbs/diff.rb | 11 +++++--- sig/diff.rbs | 4 ++- test/rbs/diff_test.rb | 66 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/lib/rbs/cli/diff.rb b/lib/rbs/cli/diff.rb index 1fc252967..61c6f9321 100644 --- a/lib/rbs/cli/diff.rb +++ b/lib/rbs/cli/diff.rb @@ -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 @@ -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) @@ -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 diff --git a/lib/rbs/diff.rb b/lib/rbs/diff.rb index deb46ac61..b1d6b3c84 100644 --- a/lib/rbs/diff.rb +++ b/lib/rbs/diff.rb @@ -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) @@ -100,10 +101,11 @@ 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 - "alias #{prefix}#{key} #{prefix}#{definition_method.alias_of.defs.first.member.name}" + "#{detail_to_s}alias #{prefix}#{key} #{prefix}#{definition_method.alias_of.defs.first.member.name}" else - "def #{prefix}#{key}: #{definition_method.method_types.join(" | ")}" + "#{detail_to_s}def #{prefix}#{key}: #{definition_method.method_types.join(" | ")}" end else +"-" @@ -112,7 +114,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 diff --git a/sig/diff.rbs b/sig/diff.rbs index 4f3b99269..4f1e165e3 100644 --- a/sig/diff.rbs +++ b/sig/diff.rbs @@ -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 diff --git a/test/rbs/diff_test.rb b/test/rbs/diff_test.rb index acd383c10..a08b79275 100644 --- a/test/rbs/diff_test.rb +++ b/test/rbs/diff_test.rb @@ -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