diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 5967cdb1ba..02bc548809 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -34,8 +34,9 @@ class RubyLex begin result = yield code rescue ArgumentError => e - if e.message.match?(/unknown encoding name/) && code.match?(/\A(?#.*\n)?#\s*coding\s*:.*(?\n)?/) - code = code.gsub(/\A(?#.*\n)?#\s*coding\s*:.*(?\n)?/, "\\k#\\k") + magic_comment_regexp = /\A(?#.*\n)?#\s*(?:encoding|coding)\s*:.*(?\n)?/ + if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp) + code = code.gsub(magic_comment_regexp, "\\k#\\k") retry end end diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 367d1e204a..bc5b4feaf7 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -139,5 +139,18 @@ module TestIRB assert_indenting(lines, row.new_line_spaces, true) end end + + def test_incomplete_encoding_magic_comment + input_with_correct_indents = [ + Row.new(%q(#encoding:u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end end end