Skip to content

Commit dfd4ced

Browse files
committed
Combine duplicated code state check into method
1 parent d4510e3 commit dfd4ced

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/irb/ruby-lex.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def configure_io(io)
6969
# Accept any single-line input for symbol aliases or commands that transform args
7070
next true if single_line_command?(code)
7171

72-
code.gsub!(/\s*\z/, '').concat("\n")
73-
tokens = self.class.ripper_lex_without_warning(code, context: @context)
74-
ltype, indent, continue, code_block_open = check_state(code, tokens)
72+
ltype, indent, continue, code_block_open = check_code_state(code)
7573
if ltype or indent > 0 or continue or code_block_open
7674
false
7775
else
@@ -209,6 +207,12 @@ def check_state(code, tokens)
209207
[ltype, indent, continue, code_block_open]
210208
end
211209

210+
def check_code_state(code)
211+
check_target_code = code.gsub(/\s*\z/, '').concat("\n")
212+
tokens = self.class.ripper_lex_without_warning(check_target_code, context: @context)
213+
check_state(check_target_code, tokens)
214+
end
215+
212216
def save_prompt_to_context_io(ltype, indent, continue, line_num_offset)
213217
# Implicitly saves prompt string to `@context.io.prompt`. This will be used in the next `@input.call`.
214218
@prompt.call(ltype, indent, continue, @line_no + line_num_offset)
@@ -233,9 +237,7 @@ def readmultiline
233237
# Accept any single-line input for symbol aliases or commands that transform args
234238
return code if single_line_command?(code)
235239

236-
check_target_code = code.gsub(/\s*\z/, '').concat("\n")
237-
tokens = self.class.ripper_lex_without_warning(check_target_code, context: @context)
238-
ltype, indent, continue, code_block_open = check_state(check_target_code, tokens)
240+
ltype, indent, continue, code_block_open = check_code_state(code)
239241
return code unless ltype or indent > 0 or continue or code_block_open
240242

241243
line_offset += 1

test/irb/test_ruby_lex.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ def assert_code_block_open(lines, expected, local_variables: [])
9696

9797
def check_state(lines, local_variables: [])
9898
context = build_context(local_variables)
99-
code = lines.join("\n")
100-
tokens = RubyLex.ripper_lex_without_warning(code, context: context)
10199
ruby_lex = RubyLex.new(context)
102-
_ltype, indent, _continue, code_block_open = ruby_lex.check_state(code, tokens)
100+
_ltype, indent, _continue, code_block_open = ruby_lex.check_code_state(lines.join("\n"))
103101
[indent, code_block_open]
104102
end
105103

0 commit comments

Comments
 (0)