Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move input line mutation out of Context#evaluate #615

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ def eval_input
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
IRB.set_measure_callback
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)
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
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|
_name, callback, arg = item
proc {
Expand All @@ -578,7 +578,7 @@ def eval_input
}.call
@context.set_last_value(result)
else
@context.evaluate(line, line_no, exception: exc)
evaluate_line(line, line_no, exception: exc)
end
if @context.echo?
if is_assignment
Expand All @@ -604,6 +604,23 @@ def eval_input
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)
str.force_encoding(enc)
str.scrub { |c|
Expand Down
14 changes: 1 addition & 13 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,13 @@ def inspect_mode=(opt)

def evaluate(line, line_no, exception: nil) # :nodoc:
@line_no = line_no

if exception
line_no -= 1
line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
@workspace.local_variable_set(:_, exception)
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))
end

Expand Down