Skip to content

Commit 0db0a8d

Browse files
committed
Refactor ripper_lex_without_warning
1 parent be5a7b7 commit 0db0a8d

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/irb/ruby-lex.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,19 @@ def set_prompt(p = nil, &block)
139139
def self.ripper_lex_without_warning(code, context: nil)
140140
verbose, $VERBOSE = $VERBOSE, nil
141141
if context
142-
lvars = context&.workspace&.binding&.local_variables
142+
lvars = context.workspace&.binding&.local_variables
143143
if lvars && !lvars.empty?
144144
code = "#{lvars.join('=')}=nil\n#{code}"
145145
line_no = 0
146146
else
147147
line_no = 1
148148
end
149149
end
150-
tokens = nil
150+
151151
compile_with_errors_suppressed(code, line_no: line_no) do |inner_code, line_no|
152152
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
153153
if lexer.respond_to?(:scan) # Ruby 2.7+
154-
tokens = []
155-
lexer.scan.each do |t|
154+
lexer.scan.each_with_object([]) do |t, tokens|
156155
next if t.pos.first == 0
157156
prev_tk = tokens.last
158157
position_overlapped = prev_tk && t.pos[0] == prev_tk.pos[0] && t.pos[1] < prev_tk.pos[1] + prev_tk.tok.bytesize
@@ -163,10 +162,9 @@ def self.ripper_lex_without_warning(code, context: nil)
163162
end
164163
end
165164
else
166-
tokens = lexer.parse.reject { |it| it.pos.first == 0 }
165+
lexer.parse.reject { |it| it.pos.first == 0 }
167166
end
168167
end
169-
tokens
170168
ensure
171169
$VERBOSE = verbose
172170
end

0 commit comments

Comments
 (0)