diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 54ea2a9e7b..544392228e 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -48,7 +48,7 @@ class RubyLex end # io functions - def set_input(io, p = nil, context: nil, &block) + def set_input(io, p = nil, context:, &block) @io = io if @io.respond_to?(:check_termination) @io.check_termination do |code| @@ -216,7 +216,7 @@ class RubyLex ltype = process_literal_type(tokens) indent = process_nesting_level(tokens) continue = process_continue(tokens) - lvars_code = self.class.generate_local_variables_assign_code(context&.local_variables || []) + lvars_code = self.class.generate_local_variables_assign_code(context.local_variables) code = "#{lvars_code}\n#{code}" if lvars_code code_block_open = check_code_block(code, tokens) [ltype, indent, continue, code_block_open] diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 1388d08962..5d66dc5bb0 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -24,13 +24,14 @@ module TestIRB last_line_index = lines.length - 1 byte_pointer = lines.last.length + context = build_context + context.auto_indent_mode = true ruby_lex = RubyLex.new() io = MockIO_AutoIndent.new([lines, last_line_index, byte_pointer, add_new_line]) do |auto_indent| error_message = "Calculated the wrong number of spaces for:\n #{lines.join("\n")}" assert_equal(correct_space_count, auto_indent, error_message) end - ruby_lex.set_input(io) - context = OpenStruct.new(auto_indent_mode: true) + ruby_lex.set_input(io, context: context) ruby_lex.set_auto_indent(context) end @@ -48,11 +49,10 @@ module TestIRB def ruby_lex_for_lines(lines, local_variables: []) ruby_lex = RubyLex.new() + + context = build_context(local_variables) io = proc{ lines.join("\n") } - ruby_lex.set_input(io, io) - unless local_variables.empty? - context = OpenStruct.new(local_variables: local_variables) - end + ruby_lex.set_input(io, io, context: context) ruby_lex.lex(context) ruby_lex end @@ -620,7 +620,8 @@ module TestIRB ruby_lex.set_prompt do |ltype, indent, continue, line_no| '%03d:%01d:%1s:%s ' % [line_no, indent, ltype, continue ? '*' : '>'] end - ruby_lex.set_input(io) + context = build_context + ruby_lex.set_input(io, context: context) end def test_dyanmic_prompt @@ -697,5 +698,19 @@ module TestIRB assert_equal('<