From a78ab356815e990c7750f895cca97274b0f76402 Mon Sep 17 00:00:00 2001 From: White-Green <43771790+White-Green@users.noreply.github.com> Date: Fri, 18 Apr 2025 23:05:33 +0900 Subject: [PATCH 1/2] Add test for 'it' local variable in block parameters --- scenario/block/it.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 scenario/block/it.rb diff --git a/scenario/block/it.rb b/scenario/block/it.rb new file mode 100644 index 000000000..94699fb0f --- /dev/null +++ b/scenario/block/it.rb @@ -0,0 +1,13 @@ +## update +def foo(&b) + b.call(1) +end + +foo do + it +end + +## assert +class Object + def foo: { (Integer) -> Integer } -> Integer +end From f003ab3a33711db8eda4919c9cc0ec339e22adb9 Mon Sep 17 00:00:00 2001 From: White-Green <43771790+White-Green@users.noreply.github.com> Date: Fri, 18 Apr 2025 23:05:42 +0900 Subject: [PATCH 2/2] Add support for 'it' local variable in AST and parameter handling --- lib/typeprof/core/ast.rb | 2 ++ lib/typeprof/core/ast/call.rb | 2 ++ lib/typeprof/core/ast/variable.rb | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/lib/typeprof/core/ast.rb b/lib/typeprof/core/ast.rb index 6426a6d56..9225b745b 100644 --- a/lib/typeprof/core/ast.rb +++ b/lib/typeprof/core/ast.rb @@ -92,6 +92,8 @@ def self.create_node(raw_node, lenv, use_result = true) # variables when :local_variable_read_node LocalVariableReadNode.new(raw_node, lenv) + when :it_local_variable_read_node + ItLocalVariableReadNode.new(raw_node, lenv) when :local_variable_write_node LocalVariableWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv) when :local_variable_operator_write_node diff --git a/lib/typeprof/core/ast/call.rb b/lib/typeprof/core/ast/call.rb index 5b253c6b8..cc7b86bf3 100644 --- a/lib/typeprof/core/ast/call.rb +++ b/lib/typeprof/core/ast/call.rb @@ -55,6 +55,8 @@ def initialize(raw_node, recv, mid, mid_code_range, raw_args, last_arg, raw_bloc raw_block.parameters.parameters.requireds.map {|n| n.is_a?(Prism::MultiTargetNode) ? nil : n.name } when Prism::NumberedParametersNode 1.upto(raw_block.parameters.maximum).map { |n| :"_#{n}" } + when Prism::ItParametersNode + [:it] when nil [] else diff --git a/lib/typeprof/core/ast/variable.rb b/lib/typeprof/core/ast/variable.rb index 1c12492f1..1c81cf84c 100644 --- a/lib/typeprof/core/ast/variable.rb +++ b/lib/typeprof/core/ast/variable.rb @@ -19,6 +19,22 @@ def retrieve_at(pos) end end + class ItLocalVariableReadNode < Node + def initialize(raw_node, lenv) + super(raw_node, lenv) + end + + def attrs = {} + + def install0(genv) + @lenv.get_var(:it) + end + + def retrieve_at(pos) + yield self if code_range.include?(pos) + end + end + class LocalVariableWriteNode < Node def initialize(raw_node, rhs, lenv) super(raw_node, lenv)