From 1dfd24a7fc4e2877d49d2a3326925080e2972cf1 Mon Sep 17 00:00:00 2001 From: aycabta Date: Mon, 1 Jun 2020 19:22:50 +0900 Subject: [PATCH] [ruby/irb] Suppress incomplete encoding magic comment error https://github.com/ruby/irb/commit/443e90af80 --- lib/irb/ruby-lex.rb | 5 +++-- test/irb/test_ruby_lex.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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