Skip to content

Commit 5bfa9ff

Browse files
mameclaude
andcommitted
Add module_function support
When `module_function` is called without arguments in a module body, subsequent method definitions are registered as both instance methods and singleton methods, matching Ruby's behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6b3c18a commit 5bfa9ff

5 files changed

Lines changed: 30 additions & 0 deletions

File tree

lib/typeprof/core/ast.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ def self.create_node(raw_node, lenv, use_result = true, allow_meta = false)
280280
return AttrReaderMetaNode.new(raw_node, lenv)
281281
when :attr_accessor
282282
return AttrAccessorMetaNode.new(raw_node, lenv)
283+
when :module_function
284+
if raw_node.arguments.nil?
285+
return ModuleFunctionMetaNode.new(raw_node, lenv)
286+
end
283287
end
284288
end
285289
CallNode.new(raw_node, lenv)

lib/typeprof/core/ast/meta.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,11 @@ def install0(genv)
146146
Source.new
147147
end
148148
end
149+
class ModuleFunctionMetaNode < Node
150+
def install0(genv)
151+
@lenv.module_function = true
152+
Source.new
153+
end
154+
end
149155
end
150156
end

lib/typeprof/core/ast/method.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ def install0(genv)
289289
)
290290

291291
@changes.add_method_def_box(genv, @lenv.cref.cpath, @singleton, @mid, f_args, @body.lenv.return_boxes)
292+
if @lenv.module_function && !@singleton
293+
@changes.add_method_def_box(genv, @lenv.cref.cpath, true, @mid, f_args, @body.lenv.return_boxes)
294+
end
292295

293296
Source.new(Type::Symbol.new(genv, @mid))
294297
end

lib/typeprof/core/env.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ def initialize(file_context, cref, locals, return_boxes)
331331
end
332332

333333
attr_reader :file_context, :cref, :locals, :return_boxes, :break_vtx, :next_boxes, :strict_const_scope
334+
attr_accessor :module_function
334335

335336
def path = @file_context&.path
336337
def code_range_from_node(node)

scenario/misc/module_function.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## update
2+
module Foo
3+
module_function
4+
5+
def bar
6+
42
7+
end
8+
end
9+
10+
Foo.bar
11+
12+
## assert
13+
module Foo
14+
def bar: -> Integer
15+
def self.bar: -> Integer
16+
end

0 commit comments

Comments
 (0)