From a5b6d7bca84fce6e13c68e8753893c4697960e3a Mon Sep 17 00:00:00 2001 From: aycabta Date: Wed, 13 Nov 2019 15:10:05 +0900 Subject: Suppress warnings except for when last evaluation Co-authored-by: Kazuhiro NISHIYAMA --- lib/irb.rb | 5 ++++- lib/irb/color.rb | 2 ++ lib/irb/ruby-lex.rb | 15 +++++++++++---- test/irb/test_context.rb | 6 ++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/irb.rb b/lib/irb.rb index 97af046d05..2bf46aa227 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -766,7 +766,10 @@ module IRB # s-expression where the second selement of the top level array is an # array of parsed expressions. The first element of each expression is the # expression's type. - ASSIGNMENT_NODE_TYPES.include?(Ripper.sexp(line)&.dig(1,-1,0)) + verbose, $VERBOSE = $VERBOSE, nil + result = ASSIGNMENT_NODE_TYPES.include?(Ripper.sexp(line)&.dig(1,-1,0)) + $VERBOSE = verbose + result end ATTR_TTY = "\e[%sm" diff --git a/lib/irb/color.rb b/lib/irb/color.rb index 3ce628ff24..d2b9674a71 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -154,6 +154,7 @@ module IRB # :nodoc: def scan(code, allow_last_error:) pos = [1, 0] + verbose, $VERBOSE = $VERBOSE, nil lexer = Ripper::Lexer.new(code) if lexer.respond_to?(:scan) # Ruby 2.7+ lexer.scan.each do |elem| @@ -177,6 +178,7 @@ module IRB # :nodoc: yield(elem.event, elem.tok, elem.state) end end + $VERBOSE = verbose end def dispatch_seq(token, expr, str, in_symbol:) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index a7b8ad8a07..58a3e1e264 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -71,20 +71,27 @@ class RubyLex end end + def ripper_lex_without_warning(code) + verbose, $VERBOSE = $VERBOSE, nil + tokens = Ripper.lex(code) + $VERBOSE = verbose + tokens + end + def set_auto_indent(context) if @io.respond_to?(:auto_indent) and context.auto_indent_mode @io.auto_indent do |lines, line_index, byte_pointer, is_newline| if is_newline md = lines[line_index - 1].match(/(\A +)/) prev_spaces = md.nil? ? 0 : md[1].count(' ') - @tokens = Ripper.lex(lines[0..line_index].join("\n")) + @tokens = ripper_lex_without_warning(lines[0..line_index].join("\n")) depth_difference = check_newline_depth_difference prev_spaces + depth_difference * 2 else code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join last_line = lines[line_index]&.byteslice(0, byte_pointer) code += last_line if last_line - @tokens = Ripper.lex(code) + @tokens = ripper_lex_without_warning(code) corresponding_token_depth = check_corresponding_token_depth if corresponding_token_depth corresponding_token_depth @@ -97,7 +104,7 @@ class RubyLex end def check_state(code) - @tokens = Ripper.lex(code) + @tokens = ripper_lex_without_warning(code) ltype = process_literal_type indent = process_nesting_level continue = process_continue @@ -160,7 +167,7 @@ class RubyLex end code = @line + (line.nil? ? '' : line) code.gsub!(/\s*\z/, '').concat("\n") - @tokens = Ripper.lex(code) + @tokens = ripper_lex_without_warning(code) @continue = process_continue @code_block_open = check_code_block(code) @indent = process_nesting_level diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index d58477c84e..075db35d2b 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -63,6 +63,12 @@ module TestIRB assert_not_match(/rescue _\.class/, e.message) end + def test_evaluate_with_onigmo_warning + assert_warning("(irb):1: warning: character class has duplicated range: /[aa]/\n") do + @context.evaluate('/[aa]/', 1) + end + end + def test_eval_input verbose, $VERBOSE = $VERBOSE, nil input = TestInputMethod.new([ -- cgit v1.2.3