[ruby/irb] Refactor ripper_lex_without_warning
https://github.com/ruby/irb/commit/0db0a8ddc5
This commit is contained in:
parent
ded895baa9
commit
55e2116f2c
@ -139,7 +139,7 @@ class RubyLex
|
|||||||
def self.ripper_lex_without_warning(code, context: nil)
|
def self.ripper_lex_without_warning(code, context: nil)
|
||||||
verbose, $VERBOSE = $VERBOSE, nil
|
verbose, $VERBOSE = $VERBOSE, nil
|
||||||
if context
|
if context
|
||||||
lvars = context&.workspace&.binding&.local_variables
|
lvars = context.workspace&.binding&.local_variables
|
||||||
if lvars && !lvars.empty?
|
if lvars && !lvars.empty?
|
||||||
code = "#{lvars.join('=')}=nil\n#{code}"
|
code = "#{lvars.join('=')}=nil\n#{code}"
|
||||||
line_no = 0
|
line_no = 0
|
||||||
@ -147,12 +147,11 @@ class RubyLex
|
|||||||
line_no = 1
|
line_no = 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
tokens = nil
|
|
||||||
compile_with_errors_suppressed(code, line_no: line_no) do |inner_code, line_no|
|
compile_with_errors_suppressed(code, line_no: line_no) do |inner_code, line_no|
|
||||||
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
|
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
|
||||||
if lexer.respond_to?(:scan) # Ruby 2.7+
|
if lexer.respond_to?(:scan) # Ruby 2.7+
|
||||||
tokens = []
|
lexer.scan.each_with_object([]) do |t, tokens|
|
||||||
lexer.scan.each do |t|
|
|
||||||
next if t.pos.first == 0
|
next if t.pos.first == 0
|
||||||
prev_tk = tokens.last
|
prev_tk = tokens.last
|
||||||
position_overlapped = prev_tk && t.pos[0] == prev_tk.pos[0] && t.pos[1] < prev_tk.pos[1] + prev_tk.tok.bytesize
|
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 @@ class RubyLex
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
tokens = lexer.parse.reject { |it| it.pos.first == 0 }
|
lexer.parse.reject { |it| it.pos.first == 0 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
tokens
|
|
||||||
ensure
|
ensure
|
||||||
$VERBOSE = verbose
|
$VERBOSE = verbose
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user