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

Allow a callable object as the :patterns option of the Trace middleware #81

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions known_sig/orthoses/trace.rbs
Original file line number Diff line number Diff line change
@@ -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
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], ?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], ?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
8 changes: 5 additions & 3 deletions lib/orthoses/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
23 changes: 23 additions & 0 deletions lib/orthoses/trace/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
26 changes: 26 additions & 0 deletions lib/orthoses/trace/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/orthoses/trace/targetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
18 changes: 13 additions & 5 deletions sig/orthoses/trace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Loading