[ruby/irb] Move input line mutation out of Context#evaluate
(https://github.com/ruby/irb/pull/615) This makes sure `Context#evaluate` really just evaluates the input. It will also make #575's implementation cleaner.
This commit is contained in:
parent
913e01e80e
commit
caddd0274b
23
lib/irb.rb
23
lib/irb.rb
@ -563,11 +563,11 @@ module IRB
|
|||||||
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
|
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
|
||||||
IRB.set_measure_callback
|
IRB.set_measure_callback
|
||||||
end
|
end
|
||||||
# Assignment expression check should be done before @context.evaluate to handle code like `a /2#/ if false; a = 1`
|
# Assignment expression check should be done before evaluate_line to handle code like `a /2#/ if false; a = 1`
|
||||||
is_assignment = assignment_expression?(line)
|
is_assignment = assignment_expression?(line)
|
||||||
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
|
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
|
||||||
result = nil
|
result = nil
|
||||||
last_proc = proc{ result = @context.evaluate(line, line_no, exception: exc) }
|
last_proc = proc{ result = evaluate_line(line, line_no, exception: exc) }
|
||||||
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
|
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
|
||||||
_name, callback, arg = item
|
_name, callback, arg = item
|
||||||
proc {
|
proc {
|
||||||
@ -578,7 +578,7 @@ module IRB
|
|||||||
}.call
|
}.call
|
||||||
@context.set_last_value(result)
|
@context.set_last_value(result)
|
||||||
else
|
else
|
||||||
@context.evaluate(line, line_no, exception: exc)
|
evaluate_line(line, line_no, exception: exc)
|
||||||
end
|
end
|
||||||
if @context.echo?
|
if @context.echo?
|
||||||
if is_assignment
|
if is_assignment
|
||||||
@ -604,6 +604,23 @@ module IRB
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def evaluate_line(line, line_no, exception: nil)
|
||||||
|
# Transform a non-identifier alias (@, $) or keywords (next, break)
|
||||||
|
command, args = line.split(/\s/, 2)
|
||||||
|
if original = @context.command_aliases[command.to_sym]
|
||||||
|
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
|
||||||
|
command = original
|
||||||
|
end
|
||||||
|
|
||||||
|
# Hook command-specific transformation
|
||||||
|
command_class = ExtendCommandBundle.load_command(command)
|
||||||
|
if command_class&.respond_to?(:transform_args)
|
||||||
|
line = "#{command} #{command_class.transform_args(args)}"
|
||||||
|
end
|
||||||
|
|
||||||
|
@context.evaluate(line, line_no, exception: exception)
|
||||||
|
end
|
||||||
|
|
||||||
def convert_invalid_byte_sequence(str, enc)
|
def convert_invalid_byte_sequence(str, enc)
|
||||||
str.force_encoding(enc)
|
str.force_encoding(enc)
|
||||||
str.scrub { |c|
|
str.scrub { |c|
|
||||||
|
@ -475,25 +475,13 @@ module IRB
|
|||||||
|
|
||||||
def evaluate(line, line_no, exception: nil) # :nodoc:
|
def evaluate(line, line_no, exception: nil) # :nodoc:
|
||||||
@line_no = line_no
|
@line_no = line_no
|
||||||
|
|
||||||
if exception
|
if exception
|
||||||
line_no -= 1
|
line_no -= 1
|
||||||
line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
|
line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
|
||||||
@workspace.local_variable_set(:_, exception)
|
@workspace.local_variable_set(:_, exception)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Transform a non-identifier alias (@, $) or keywords (next, break)
|
|
||||||
command, args = line.split(/\s/, 2)
|
|
||||||
if original = command_aliases[command.to_sym]
|
|
||||||
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
|
|
||||||
command = original
|
|
||||||
end
|
|
||||||
|
|
||||||
# Hook command-specific transformation
|
|
||||||
command_class = ExtendCommandBundle.load_command(command)
|
|
||||||
if command_class&.respond_to?(:transform_args)
|
|
||||||
line = "#{command} #{command_class.transform_args(args)}"
|
|
||||||
end
|
|
||||||
|
|
||||||
set_last_value(@workspace.evaluate(line, irb_path, line_no))
|
set_last_value(@workspace.evaluate(line, irb_path, line_no))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user