[前][次][番号順一覧][スレッド一覧]

ruby-changes:62344

From: Nobuhiro <ko1@a...>
Date: Wed, 22 Jul 2020 02:42:21 +0900 (JST)
Subject: [ruby-changes:62344] f6e789e3b0 (master): [ruby/irb] handle rescue modifier properly

https://git.ruby-lang.org/ruby.git/commit/?id=f6e789e3b0

From f6e789e3b07ede0b5842c7f64940e4cd9ed876fd Mon Sep 17 00:00:00 2001
From: Nobuhiro IMAI <nov@y...>
Date: Mon, 2 Mar 2020 22:16:11 +0900
Subject: [ruby/irb] handle rescue modifier properly

https://github.com/ruby/irb/commit/6de1341f5e

diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index c7a47fd..6a7df1a 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -324,7 +324,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L324
         when 'def', 'case', 'for', 'begin', 'class', 'module'
           indent += 1
         when 'if', 'unless', 'while', 'until'
-          # postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
+          # postfix if/unless/while/until must be Ripper::EXPR_LABEL
           indent += 1 unless t[3].allbits?(Ripper::EXPR_LABEL)
         when 'end'
           indent -= 1
@@ -369,12 +369,12 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L369
           end
         when 'def', 'case', 'for', 'begin', 'class', 'module'
           depth_difference += 1
-        when 'if', 'unless', 'while', 'until'
+        when 'if', 'unless', 'while', 'until', 'rescue'
           # postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
           unless t[3].allbits?(Ripper::EXPR_LABEL)
             depth_difference += 1
           end
-        when 'else', 'elsif', 'rescue', 'ensure', 'when', 'in'
+        when 'else', 'elsif', 'ensure', 'when', 'in'
           depth_difference += 1
         end
       end
@@ -420,12 +420,16 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L420
         case t[2]
         when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
           spaces_of_nest.push(spaces_at_line_head)
+        when 'rescue'
+          unless t[3].allbits?(Ripper::EXPR_LABEL)
+            corresponding_token_depth = spaces_of_nest.last
+          end
         when 'if', 'unless', 'while', 'until'
-          # postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
+          # postfix if/unless/while/until must be Ripper::EXPR_LABEL
           unless t[3].allbits?(Ripper::EXPR_LABEL)
             spaces_of_nest.push(spaces_at_line_head)
           end
-        when 'else', 'elsif', 'rescue', 'ensure', 'when', 'in'
+        when 'else', 'elsif', 'ensure', 'when', 'in'
           corresponding_token_depth = spaces_of_nest.last
         when 'end'
           if is_first_printable_of_line
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index 7768a6a..afbad63 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -178,5 +178,32 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L178
         assert_indenting(lines, row.new_line_spaces, true)
       end
     end
+
+    def test_mixed_rescue
+      input_with_correct_indents = [
+        Row.new(%q(def m), nil, 2),
+        Row.new(%q(  begin), nil, 4),
+        Row.new(%q(    begin), nil, 6),
+        Row.new(%q(      x = a rescue 4), nil, 6),
+        Row.new(%q(      y = [(a rescue 5)]), nil, 6),
+        Row.new(%q(      [x, y]), nil, 6),
+        Row.new(%q(    rescue => e), 4, 6),
+        Row.new(%q(      raise e rescue 8), nil, 6),
+        Row.new(%q(    end), 4, 4),
+        Row.new(%q(  rescue), 2, 4),
+        Row.new(%q(    raise rescue 11), nil, 4),
+        Row.new(%q(  end), 2, 2),
+        Row.new(%q(rescue => e), 0, 2),
+        Row.new(%q(  raise e rescue 14), nil, 2),
+        Row.new(%q(end), 0, 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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]