diff --git a/known_sig/orthoses/trace.rbs b/known_sig/orthoses/trace.rbs new file mode 100644 index 0000000..26c93f7 --- /dev/null +++ b/known_sig/orthoses/trace.rbs @@ -0,0 +1,10 @@ +module Orthoses + class Trace + interface _CallableFilter + def call: (String) -> boolish + end + + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: _CallableFilter?, ?sort_union_types: bool?) -> void + def call: () -> Orthoses::store + end +end diff --git a/known_sig/orthoses/trace/attribute.rbs b/known_sig/orthoses/trace/attribute.rbs index 781f4bc..8ca8346 100644 --- a/known_sig/orthoses/trace/attribute.rbs +++ b/known_sig/orthoses/trace/attribute.rbs @@ -3,7 +3,7 @@ module Orthoses class Attribute include Orthoses::Trace::Targetable - def initialize: (Orthoses::_Call loader, patterns: Array[String], ?sort_union_types: bool?) -> void + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: Orthoses::Trace::_CallableFilter?, ?sort_union_types: bool?) -> void def call: () -> Orthoses::store end end diff --git a/known_sig/orthoses/trace/method.rbs b/known_sig/orthoses/trace/method.rbs index f8a168f..217b8e0 100644 --- a/known_sig/orthoses/trace/method.rbs +++ b/known_sig/orthoses/trace/method.rbs @@ -3,7 +3,7 @@ module Orthoses class Method include Orthoses::Trace::Targetable - def initialize: (Orthoses::_Call loader, patterns: Array[String], ?sort_union_types: bool?) -> void + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: Orthoses::Trace::_CallableFilter?, ?sort_union_types: bool?) -> void def call: () -> Orthoses::store end end diff --git a/lib/orthoses/trace.rb b/lib/orthoses/trace.rb index 7463c59..9ab759e 100644 --- a/lib/orthoses/trace.rb +++ b/lib/orthoses/trace.rb @@ -7,14 +7,16 @@ class Trace autoload :Method, 'orthoses/trace/method' autoload :Targetable, 'orthoses/trace/targetable' - def initialize(loader, patterns:) + def initialize(loader, patterns:, trace_point_filter: nil, sort_union_types: true) @loader = loader @patterns = patterns + @trace_point_filter = trace_point_filter + @sort_union_types = sort_union_types end def call - @loader = Trace::Attribute.new(@loader, patterns: @patterns) - @loader = Trace::Method.new(@loader, patterns: @patterns) + @loader = Trace::Attribute.new(@loader, patterns: @patterns, trace_point_filter: @trace_point_filter, sort_union_types: @sort_union_types) + @loader = Trace::Method.new(@loader, patterns: @patterns, trace_point_filter: @trace_point_filter, sort_union_types: @sort_union_types) @loader.call end end diff --git a/lib/orthoses/trace/attribute.rb b/lib/orthoses/trace/attribute.rb index 56ab1b0..2003a1e 100644 --- a/lib/orthoses/trace/attribute.rb +++ b/lib/orthoses/trace/attribute.rb @@ -23,9 +23,10 @@ def attr_writer(*names) include Targetable - def initialize(loader, patterns:, sort_union_types: true) + def initialize(loader, patterns:, trace_point_filter: nil, sort_union_types: true) @loader = loader @patterns = patterns + @trace_point_filter = trace_point_filter @sort_union_types = sort_union_types @captured_dict = Hash.new { |h, k| h[k] = Hash.new { |hh, kk| hh[kk] = [] } } diff --git a/lib/orthoses/trace/attribute_test.rb b/lib/orthoses/trace/attribute_test.rb index c36ef07..139518b 100644 --- a/lib/orthoses/trace/attribute_test.rb +++ b/lib/orthoses/trace/attribute_test.rb @@ -119,4 +119,27 @@ class TraceAttributeTest::Foo::Baz t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n") end end + + def test_trace_point_filter(t) + trace_point_filter = ->(name) { name == "TraceAttributeTest::Foo" } + store = Orthoses::Trace::Attribute.new(->{ + LOADER_ATTRIBUTE.call + foo = Foo.new + foo.attr_read_publ + Foo::Bar.new.attr_acce_publ = /reg/ + + Orthoses::Utils.new_store + }, patterns: %w[*], trace_point_filter: trace_point_filter).call + + actual = store.map { |n, c| c.to_rbs }.join("\n") + expect = <<~RBS + class TraceAttributeTest::Foo + attr_accessor attr_acce_priv: Integer + attr_reader attr_read_publ: Symbol + end + RBS + unless expect == actual + t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n") + end + end end diff --git a/lib/orthoses/trace/method.rb b/lib/orthoses/trace/method.rb index ba84803..3e1dd7a 100644 --- a/lib/orthoses/trace/method.rb +++ b/lib/orthoses/trace/method.rb @@ -7,9 +7,10 @@ class Method Info = Struct.new(:key, :op_name_types, :raised, keyword_init: true) include Targetable - def initialize(loader, patterns:, sort_union_types: true) + def initialize(loader, patterns:, trace_point_filter: nil, sort_union_types: true) @loader = loader @patterns = patterns + @trace_point_filter = trace_point_filter @sort_union_types = sort_union_types @stack = [] diff --git a/lib/orthoses/trace/method_test.rb b/lib/orthoses/trace/method_test.rb index 70cf54d..ea1ce4a 100644 --- a/lib/orthoses/trace/method_test.rb +++ b/lib/orthoses/trace/method_test.rb @@ -140,6 +140,32 @@ def self.a: () -> Integer end end + def test_trace_point_filter(t) + trace_point_filter = ->(name) { name == "TraceMethodTest::M" } + store = Orthoses::Trace::Method.new(-> { + LOADER_METHOD.call + + m = M.new(100) + m.a_ten + m.call_priv(true) + + Orthoses::Utils.new_store + }, patterns: %w[*], trace_point_filter: trace_point_filter).call + + actual = store.map { |n, c| c.to_rbs }.join("\n") + expect = <<~RBS + class TraceMethodTest::M + private def initialize: (Integer a) -> void + def a_ten: () -> Integer + private def priv: (bool bool) -> Integer + def call_priv: (bool c) -> Integer + end + RBS + unless expect == actual + t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n") + end + end + def test_raise_first(t) Orthoses::Trace::Method.new(->{ raise rescue nil diff --git a/lib/orthoses/trace/targetable.rb b/lib/orthoses/trace/targetable.rb index 63545ec..1d260c7 100644 --- a/lib/orthoses/trace/targetable.rb +++ b/lib/orthoses/trace/targetable.rb @@ -4,6 +4,8 @@ module Orthoses class Trace module Targetable def target?(name) + return false if @trace_point_filter && !@trace_point_filter.call(name) + @patterns.any? do |pattern| if pattern.end_with?("*") (name || "").start_with?(pattern.chop) diff --git a/sig/orthoses/content.rbs b/sig/orthoses/content.rbs index ff635f5..7465a68 100644 --- a/sig/orthoses/content.rbs +++ b/sig/orthoses/content.rbs @@ -86,6 +86,7 @@ class Orthoses::Content::HeaderBuilder @resolver: untyped def initialize: (env: untyped) -> void def build: (entry: untyped, ?name_hint: untyped?) -> untyped + private def resolve_full_name: (entry: untyped) -> untyped private def build_module: (entry: untyped, ?name_hint: untyped?) -> ::String private def build_class: (entry: untyped, ?name_hint: untyped?) -> ::String private def build_super_class: (untyped primary) -> (nil | untyped) diff --git a/sig/orthoses/resolve_type_names.rbs b/sig/orthoses/resolve_type_names.rbs index a80ea96..2816d42 100644 --- a/sig/orthoses/resolve_type_names.rbs +++ b/sig/orthoses/resolve_type_names.rbs @@ -5,4 +5,13 @@ class Orthoses::ResolveTypeNames @loader: untyped def initialize: (untyped loader) -> void def call: () -> untyped + private def content_header: (untyped entry) -> untyped + private def class_header: (untyped decl) -> ::String + private def module_header: (untyped decl) -> ::String +end + +module Orthoses::ResolveTypeNames::WriterCopy + def name_and_args: (untyped name, untyped args) -> (::String | nil) + + def name_and_params: (untyped name, untyped params) -> ::String end diff --git a/sig/orthoses/trace.rbs b/sig/orthoses/trace.rbs index dad4105..db4cb18 100644 --- a/sig/orthoses/trace.rbs +++ b/sig/orthoses/trace.rbs @@ -3,16 +3,19 @@ class Orthoses::Trace @loader: untyped @patterns: untyped - def initialize: (untyped loader, patterns: untyped) -> void - def call: () -> untyped + @trace_point_filter: untyped + @sort_union_types: untyped + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: _CallableFilter?, ?sort_union_types: bool?) -> void + def call: () -> Orthoses::store end class Orthoses::Trace::Attribute @loader: untyped @patterns: untyped + @trace_point_filter: untyped @sort_union_types: untyped @captured_dict: untyped - def initialize: (Orthoses::_Call loader, patterns: Array[String], ?sort_union_types: bool?) -> void + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: Orthoses::Trace::_CallableFilter?, ?sort_union_types: bool?) -> void def call: () -> Orthoses::store private def build_trace_hook: () -> untyped include Orthoses::Trace::Targetable @@ -31,11 +34,12 @@ end class Orthoses::Trace::Method @loader: untyped @patterns: untyped + @trace_point_filter: untyped @sort_union_types: untyped @stack: untyped @args_return_map: untyped @alias_map: untyped - def initialize: (Orthoses::_Call loader, patterns: Array[String], ?sort_union_types: bool?) -> void + def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: Orthoses::Trace::_CallableFilter?, ?sort_union_types: bool?) -> void def call: () -> Orthoses::store private def build_trace_point: () -> untyped private def build_members: () -> untyped @@ -48,5 +52,9 @@ class Orthoses::Trace::Method::Info < ::Struct[untyped] end module Orthoses::Trace::Targetable - def target?: (untyped name) -> untyped + def target?: (untyped name) -> (false | untyped) +end + +interface Orthoses::Trace::_CallableFilter + def call: (String) -> boolish end