Skip to content

Instantly share code, notes, and snippets.

@tk0miya
Created May 27, 2023 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tk0miya/83fcacaa5f7e125d2ba309143e84d2d3 to your computer and use it in GitHub Desktop.
Save tk0miya/83fcacaa5f7e125d2ba309143e84d2d3 to your computer and use it in GitHub Desktop.
diff --git a/lib/rbs/prototype/rb.rb b/lib/rbs/prototype/rb.rb
index 646ed909..cbbf4b80 100644
--- a/lib/rbs/prototype/rb.rb
+++ b/lib/rbs/prototype/rb.rb
@@ -347,10 +347,33 @@ module RBS
end
when :ITER
- method_name = node.children.first.children.first
- case method_name
- when :refine
+ call, block = node.children
+ iter = call_to_names(call)
+ case iter
+ when [:refine]
# ignore
+ when [:ActiveSupport, :on_load]
+ target = call.children.last.children.first.children.first
+ case target
+ when :active_record
+ # NOTE: ActiveSupport.on_load(:active_record) の呼び出しは ActiveRecord::Base の拡張に読み替える
+ kls = AST::Declarations::Class.new(
+ name: TypeName.new(name: :'ActiveRecord::Base', namespace: Namespace.empty),
+ super_class: nil,
+ type_params: [],
+ members: [],
+ annotations: [],
+ location: nil,
+ comment: nil
+ )
+ end
+
+ if kls
+ decls.push kls
+
+ new_ctx = Context.initial(namespace: context.namespace + kls.name.to_namespace)
+ process_children(block, decls: kls.members, comments: comments, context: new_ctx)
+ end
else
process_children(node, decls: decls, comments: comments, context: context)
end
@@ -418,6 +441,15 @@ module RBS
end
end
+ def call_to_names(node)
+ case node.children.first.type
+ when :CALL
+ call_to_names(node.children.first) + [node.children[1]]
+ else
+ [const_to_name!(node.children.first).name, node.children[1]]
+ end
+ end
+
def literal_to_symbol(node)
case node.type
when :LIT
@tk0miya
Copy link
Author

tk0miya commented May 27, 2023

ActiveSupport.on_load(:active_record) { ... }

というコードを rbs prototype rb にわたした場合に、ブロックの内容が Object クラスとして処理されてしまう問題を回避するためのスーパーワークアラウンドコードです。

rbs と activesupport、そして hook を利用するすべての(?) gem が密結合するのでやりすぎですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment