diff options
author | aycabta <aycabta@gmail.com> | 2020-06-01 19:22:50 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2020-07-22 02:31:46 +0900 |
commit | 1dfd24a7fc4e2877d49d2a3326925080e2972cf1 (patch) | |
tree | 462c8922830ec02662db3eeed8c69b1ba8cbb445 | |
parent | 78ccab25306d15c325baa0761d9505ac23956f22 (diff) | |
download | ruby-1dfd24a7fc4e2877d49d2a3326925080e2972cf1.tar.gz |
[ruby/irb] Suppress incomplete encoding magic comment error
https://github.com/ruby/irb/commit/443e90af80
-rw-r--r-- | lib/irb/ruby-lex.rb | 5 | ||||
-rw-r--r-- | 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(?<shebang>#.*\n)?#\s*coding\s*:.*(?<nl>\n)?/) - code = code.gsub(/\A(?<shebang>#.*\n)?#\s*coding\s*:.*(?<nl>\n)?/, "\\k<shebang>#\\k<nl>") + magic_comment_regexp = /\A(?<shebang>#.*\n)?#\s*(?:encoding|coding)\s*:.*(?<nl>\n)?/ + if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp) + code = code.gsub(magic_comment_regexp, "\\k<shebang>#\\k<nl>") 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 |