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

Type more things #24

Merged
merged 24 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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 .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: |
bundle exec rbs collection install
# TODO: swallowing result until types pass
bundle exec rake steep:check || true
bundle exec rake steep:check
- name: Check generators
run: |
bundle exec rake docker:compose:generate[check]
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"soutaro.rbs-syntax",
"shopify.ruby-lsp",
"soutaro.steep-vscode",
"editorconfig.editorconfig"
]
}
8 changes: 5 additions & 3 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ target :lib do

check "lib"

# ignore per version dynamic definitions for old rubies
ignore "lib/**/*.ruby2.rb"

configure_code_diagnostics do |hash|
hash[D::Ruby::UnknownInstanceVariable] = :warning
hash[D::Ruby::NoMethod] = :warning
hash[D::Ruby::MethodDefinitionMissing] = :warning
hash[D::Ruby::UnknownInstanceVariable] = :error
hash[D::Ruby::MethodDefinitionMissing] = :error
end
end

Expand Down
6 changes: 5 additions & 1 deletion gemfiles/ruby-3.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ group :debug do
gem "irb"
end

group :ide do
gem "ruby-lsp"
end

group :check do
gem "rubocop", "~> 1.62.1", require: false
gem "rubocop-minitest", "~> 0.33.0", require: false
gem "rubocop-rake", "~> 0.6.0", require: false
gem "standard", "~> 1.0", require: false
gem "steep", "~> 1.6.0", require: false
gem "steep", "~> 1.7.0", require: false
end
2 changes: 1 addition & 1 deletion gemfiles/ruby-3.4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ group :check do
gem "rubocop-minitest", "~> 0.33.0", require: false
gem "rubocop-rake", "~> 0.6.0", require: false
gem "standard", "~> 1.0", require: false
gem "steep", "~> 1.6.0", require: false
gem "steep", "~> 1.7.0", require: false
end
23 changes: 9 additions & 14 deletions lib/graft/callback.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# frozen_string_literal: true

if RUBY_VERSION < "3.0"
require_relative "callback/callable.ruby2"
else
require_relative "callback/callable.ruby3"
end

module Graft
class Callback
include Callable

# @dynamic name
attr_reader :name

# NOTE: opts is not used in the current implementation
Expand All @@ -12,20 +21,6 @@ def initialize(name = nil, opts = {}, &block)
@enabled = true
end

if RUBY_VERSION < "3.0"
def call(*args, &block)
return unless enabled?

@block.call(*args, &block)
end
else
def call(*args, **kwargs, &block)
return unless enabled?

@block.call(*args, **kwargs, &block)
end
end

def disable
@enabled = false
end
Expand Down
13 changes: 13 additions & 0 deletions lib/graft/callback/callable.ruby2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Graft
class Callback
module Callable
def call(*args, &block)
return unless enabled?

@block.call(*args, &block)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/graft/callback/callable.ruby3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Graft
class Callback
module Callable
def call(*args, **kwargs, &block)
# TODO: should be useless, see `callable.rbs`
# @type self: Callback & Callable

return unless enabled?

@block.call(*args, **kwargs, &block)
end
end
end
end
55 changes: 12 additions & 43 deletions lib/graft/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
require_relative "callback"
require_relative "hook_point"

if RUBY_VERSION < "3.0"
require_relative "hook/wrapper.ruby2"
else
require_relative "hook/wrapper.ruby3"
end

module Graft
class Hook
DEFAULT_STRATEGY = HookPoint::DEFAULT_STRATEGY
KEY = "graft"

@hooks = {}

Expand Down Expand Up @@ -44,6 +51,7 @@ def self.ignore
Thread.current[:hook_entered] = false
end

# @dynamic point, stack
attr_reader :point, :stack

# NOTE: Push logic upward ClassMethods
Expand Down Expand Up @@ -107,54 +115,15 @@ def enabled?
def install
return unless point.exist?

point.install("hook", &Hook.wrapper(self))
point.install(Hook::KEY, &Hook.wrapper(self))
end

def uninstall
return unless point.exist?

point.uninstall("hook")
end

class << self
if RUBY_VERSION < "3.0"
def wrapper(hook)
proc do |*args, &block|
env = {
self: self,
args: args,
block: block
}
supa = proc { |*args, &block| super(*args, &block) }
mid = proc { |_, env| {return: supa.call(*env[:args], &env[:block])} }
stack = hook.stack.dup
stack << mid

stack.call(env)
end
end
else
def wrapper(hook)
proc do |*args, **kwargs, &block|
env = {
receiver: self,
method: hook.point.method_name,
kind: hook.point.method_kind,
strategy: hook.point.instance_variable_get(:@strategy),
args: args,
kwargs: kwargs,
block: block
}

supa = proc { |*args, **kwargs, &block| super(*args, **kwargs, &block) }
mid = proc { |_, env| {return: supa.call(*env[:args], **env[:kwargs], &env[:block])} }
stack = hook.stack.dup
stack << mid

stack.call(env)[:return]
end
end
end
point.uninstall(Hook::KEY)
end

extend Hook::Wrapper
end
end
30 changes: 30 additions & 0 deletions lib/graft/hook/wrapper.ruby2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Graft
class Hook
module Wrapper
def wrapper(hook)
proc do |*args, &block|
env = {
self: self,
args: args,
block: block
}
supa = case env[:strategy]
when :prepend
proc { |*args, &block| super(*args, &block) }
when :chain
proc { |*args, &block| hook.point.apply(env[:receiver], Hook::KEY, *args, **kwargs, &block) }
else
raise HookPointError, "unknown strategy: #{env[:strategy]}"
end
mid = proc { |_, env| {return: supa.call(*env[:args], &env[:block])} }
stack = hook.stack.dup
stack << mid

stack.call(env)
end
end
end
end
end
51 changes: 51 additions & 0 deletions lib/graft/hook/wrapper.ruby3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module Graft
class Hook
module Wrapper
def wrapper(hook)
point = hook.point

p = proc do |*args, **kwargs, &block|
# @type self: Object

env = {
receiver: self,
method: point.method_name,
kind: point.method_kind,
strategy: point.instance_variable_get(:@strategy),
args: args,
kwargs: kwargs,
block: block
}

supa = case point
when HookPoint::Prepend
proc do |*args, **kwargs, &block|
# @type self: Object
super(*args, **kwargs, &block)
end
when HookPoint::Chain
proc do |*args, **kwargs, &block|
# @type point: HookPoint & HookPoint::Chain
point.apply(env[:receiver], Hook::KEY, *args, **kwargs, &block)
end
else
raise HookPointError, "unknown strategy: #{env[:strategy]}"
end

# TODO: `case` eats `supa`'s type
# @type var supa: ^(*untyped, **untyped) -> untyped
mid = Callback.new { |_, env| {return: supa.call(*env[:args], **env[:kwargs], &env[:block])} }
stack = hook.stack.dup
stack << mid

stack.call(env)[:return]
end

# erase type: `proc` returns a `::Proc` type, not `^() -> untyped`
_ = p
end
end
end
end
Loading