Skip to content

Commit

Permalink
Suppress warnings except for when last evaluation
Browse files Browse the repository at this point in the history
Co-authored-by: Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
  • Loading branch information
aycabta and znz committed Nov 13, 2019
1 parent ab48edf commit d08ef68
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,10 @@ def assignment_expression?(line)
# 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"
Expand Down
2 changes: 2 additions & 0 deletions lib/irb/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def supported?
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|
Expand All @@ -177,6 +178,7 @@ def scan(code, allow_last_error:)
yield(elem.event, elem.tok, elem.state)
end
end
$VERBOSE = verbose
end

def dispatch_seq(token, expr, str, in_symbol:)
Expand Down
15 changes: 11 additions & 4 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,27 @@ def set_prompt(p = nil, &block)
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
Expand All @@ -97,7 +104,7 @@ def set_auto_indent(context)
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
Expand Down Expand Up @@ -160,7 +167,7 @@ def lex
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
Expand Down
6 changes: 6 additions & 0 deletions test/irb/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_evaluate_with_exception
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([
Expand Down

0 comments on commit d08ef68

Please sign in to comment.