Skip to content

Commit

Permalink
Introduce :trace_point_filter option for filtering purpose and re-run…
Browse files Browse the repository at this point in the history
… rake sig
  • Loading branch information
hibariya committed Dec 15, 2024
1 parent 46857e0 commit e1c5bff
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 29 deletions.
6 changes: 3 additions & 3 deletions known_sig/orthoses/trace.rbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Orthoses
class Trace
interface _CallablePatterns
def call: (String, TracePoint) -> boolish
interface _CallableFilter
def call: (String) -> boolish
end

def initialize: (Orthoses::_Call loader, patterns: Array[String] | _CallablePatterns, ?sort_union_types: bool?) -> void
def initialize: (Orthoses::_Call loader, patterns: Array[String], trace_point_filter: _CallableFilter?, ?sort_union_types: bool?) -> void
def call: () -> Orthoses::store
end
end
2 changes: 1 addition & 1 deletion known_sig/orthoses/trace/attribute.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Orthoses
class Attribute
include Orthoses::Trace::Targetable

def initialize: (Orthoses::_Call loader, patterns: Array[String] | Orthoses::Trace::_CallablePatterns, ?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
Expand Down
2 changes: 1 addition & 1 deletion known_sig/orthoses/trace/method.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Orthoses
class Method
include Orthoses::Trace::Targetable

def initialize: (Orthoses::_Call loader, patterns: Array[String] | Orthoses::Trace::_CallablePatterns, ?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
Expand Down
3 changes: 2 additions & 1 deletion lib/orthoses/trace/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [] } }
Expand Down
6 changes: 3 additions & 3 deletions lib/orthoses/trace/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ class TraceAttributeTest::Foo::Baz
end
end

def test_pattern_proc(t)
patterns = ->(name) { name == "TraceAttributeTest::Foo" }
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: patterns).call
}, patterns: %w[*], trace_point_filter: trace_point_filter).call

actual = store.map { |n, c| c.to_rbs }.join("\n")
expect = <<~RBS
Expand Down
3 changes: 2 additions & 1 deletion lib/orthoses/trace/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
7 changes: 3 additions & 4 deletions lib/orthoses/trace/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ def self.a: () -> Integer
end
end

def test_pattern_proc(t)
# only non-private methods
patterns = ->(name) { name == "TraceMethodTest::M" }
def test_trace_point_filter(t)
trace_point_filter = ->(name) { name == "TraceMethodTest::M" }
store = Orthoses::Trace::Method.new(-> {
LOADER_METHOD.call

Expand All @@ -151,7 +150,7 @@ def test_pattern_proc(t)
m.call_priv(true)

Orthoses::Utils.new_store
}, patterns: patterns).call
}, patterns: %w[*], trace_point_filter: trace_point_filter).call

actual = store.map { |n, c| c.to_rbs }.join("\n")
expect = <<~RBS
Expand Down
16 changes: 7 additions & 9 deletions lib/orthoses/trace/targetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ module Orthoses
class Trace
module Targetable
def target?(name)
if @trace_point_filter
@trace_point_filter.call(name)
else
@patterns.any? do |pattern|
if pattern.end_with?("*")
(name || "").start_with?(pattern.chop)
else
name == pattern
end
return false if @trace_point_filter && !@trace_point_filter.call(name)

@patterns.any? do |pattern|
if pattern.end_with?("*")
(name || "").start_with?(pattern.chop)
else
name == pattern
end
end
end
Expand Down
1 change: 1 addition & 0 deletions sig/orthoses/content.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions sig/orthoses/resolve_type_names.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 9 additions & 6 deletions sig/orthoses/trace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
class Orthoses::Trace
@loader: untyped
@patterns: untyped
@trace_point_filter: untyped
@sort_union_types: untyped
def initialize: (Orthoses::_Call loader, patterns: Array[String] | _CallablePatterns, ?sort_union_types: bool?) -> void
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] | Orthoses::Trace::_CallablePatterns, ?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
Expand All @@ -32,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] | Orthoses::Trace::_CallablePatterns, ?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
Expand All @@ -49,9 +52,9 @@ class Orthoses::Trace::Method::Info < ::Struct[untyped]
end

module Orthoses::Trace::Targetable
def target?: (untyped name, untyped tp) -> untyped
def target?: (untyped name) -> (false | untyped)
end

interface Orthoses::Trace::_CallablePatterns
def call: (String, TracePoint) -> boolish
interface Orthoses::Trace::_CallableFilter
def call: (String) -> boolish
end

0 comments on commit e1c5bff

Please sign in to comment.