Skip to content

Commit 386d0ca

Browse files
committed
Use new option added in prism 0.17.1 to parse code with localvars
1 parent a41c769 commit 386d0ca

File tree

3 files changed

+3
-14
lines changed

3 files changed

+3
-14
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ gem "test-unit"
1818
gem "test-unit-ruby-core"
1919
gem "debug", github: "ruby/debug"
2020
if RUBY_VERSION >= "3.0.0"
21-
gem "rbs"
21+
gem "rbs", ">= 0.17.1"
2222
gem "prism"
2323
end

lib/irb/type_completion/completor.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ def analyze(code, binding = Object::TOPLEVEL_BINDING)
144144
# Workaround for https://github.com/ruby/prism/issues/1592
145145
return if code.match?(/%[qQ]\z/)
146146

147-
lvars_code = binding.local_variables.map do |name|
148-
"#{name}="
149-
end.join + "nil;\n"
150-
code = lvars_code + code
151-
ast = Prism.parse(code).value
147+
ast = Prism.parse(code, scopes: [binding.local_variables]).value
152148
name = code[/(@@|@|\$)?\w*[!?=]?\z/]
153149
*parents, target_node = find_target ast, code.bytesize - name.bytesize
154150
return unless target_node

lib/irb/type_completion/type_analyzer.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,20 +1155,13 @@ def method_call(receiver, method_name, args, kwargs, block, scope, name_match: t
11551155
Types::UnionType[*types, *breaks]
11561156
end
11571157

1158-
def evaluate_program(program, scope)
1159-
# statements.body[0] is local variable assign code
1160-
program.statements.body[1..].each do |statement|
1161-
evaluate statement, scope
1162-
end
1163-
end
1164-
11651158
def self.calculate_target_type_scope(binding, parents, target)
11661159
dig_targets = DigTarget.new(parents, target) do |type, scope|
11671160
return type, scope
11681161
end
11691162
program = parents.first
11701163
scope = Scope.from_binding(binding, program.locals)
1171-
new(dig_targets).evaluate_program program, scope
1164+
new(dig_targets).evaluate program, scope
11721165
[Types::NIL, scope]
11731166
end
11741167
end

0 commit comments

Comments
 (0)