Skip to content

Commit

Permalink
Use new option added in prism 0.17.1 to parse code with localvars
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Nov 5, 2023
1 parent a41c769 commit 386d0ca
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ gem "test-unit"
gem "test-unit-ruby-core"
gem "debug", github: "ruby/debug"
if RUBY_VERSION >= "3.0.0"
gem "rbs"
gem "rbs", ">= 0.17.1"
gem "prism"
end
6 changes: 1 addition & 5 deletions lib/irb/type_completion/completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ def analyze(code, binding = Object::TOPLEVEL_BINDING)
# Workaround for https://github.com/ruby/prism/issues/1592
return if code.match?(/%[qQ]\z/)

lvars_code = binding.local_variables.map do |name|
"#{name}="
end.join + "nil;\n"
code = lvars_code + code
ast = Prism.parse(code).value
ast = Prism.parse(code, scopes: [binding.local_variables]).value
name = code[/(@@|@|\$)?\w*[!?=]?\z/]
*parents, target_node = find_target ast, code.bytesize - name.bytesize
return unless target_node
Expand Down
9 changes: 1 addition & 8 deletions lib/irb/type_completion/type_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1155,20 +1155,13 @@ def method_call(receiver, method_name, args, kwargs, block, scope, name_match: t
Types::UnionType[*types, *breaks]
end

def evaluate_program(program, scope)
# statements.body[0] is local variable assign code
program.statements.body[1..].each do |statement|
evaluate statement, scope
end
end

def self.calculate_target_type_scope(binding, parents, target)
dig_targets = DigTarget.new(parents, target) do |type, scope|
return type, scope
end
program = parents.first
scope = Scope.from_binding(binding, program.locals)
new(dig_targets).evaluate_program program, scope
new(dig_targets).evaluate program, scope
[Types::NIL, scope]
end
end
Expand Down

0 comments on commit 386d0ca

Please sign in to comment.