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

ruby-changes:56379

From: Nobuyoshi <ko1@a...>
Date: Sun, 7 Jul 2019 15:50:57 +0900 (JST)
Subject: [ruby-changes:56379] Nobuyoshi Nakada: d548073f68 (master): Enable indentation warning against `if` just after `else`

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

From d548073f68ae719933c328686df224f74a60d366 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 7 Jul 2019 15:45:10 +0900
Subject: Enable indentation warning against `if` just after `else`

```ruby
if false
  puts 'false'
else if true
  puts 'true'
end # -:5: warning: mismatched indentations at 'end' with 'if' at 3
end
```

[Feature #15990]

diff --git a/parse.y b/parse.y
index 88c0208..7823b42 100644
--- a/parse.y
+++ b/parse.y
@@ -3003,6 +3003,15 @@ k_begin		: keyword_begin https://github.com/ruby/ruby/blob/trunk/parse.y#L3003
 k_if		: keyword_if
 		    {
 			token_info_push(p, "if", &@$);
+			if (p->token_info && p->token_info->nonspc &&
+			    p->token_info->next && !strcmp(p->token_info->next->token, "if")) {
+			    const char *tok = p->lex.ptok, *beg = p->lex.pbeg;
+			    while (tok > beg && ISSPACE(*--tok));
+			    while (beg < tok && ISSPACE(*beg)) beg++;
+			    if (tok - beg == 3 && !memcmp(beg, "else", 4)) {
+				p->token_info->nonspc = 0;
+			    }
+			}
 		    }
 		;
 
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index 2f78461..d63ca5d 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -491,14 +491,17 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L491
           ["begin", "ensure ; end"],
           ["  case nil", "when true; end"],
           ["case nil; when true", "end"],
+          ["if false;", "end", "if true\nelse ", "end"],
         ].each do
-          |b, e = 'end'|
-          src = ["#{b}\n", " #{e}\n"]
+          |b, e = 'end', pre = nil, post = nil|
+          src = ["#{pre}#{b}\n", " #{e}\n#{post}"]
           k = b[/\A\s*(\S+)/, 1]
           e = e[/\A\s*(\S+)/, 1]
+          n = 2
+          n += pre.count("\n") if pre
 
           a.for("no directives with #{b}") do
-            err = ["#{t.path}:2: warning: mismatched indentations at '#{e}' with '#{k}' at 1"]
+            err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
             t.rewind
             t.truncate(0)
             t.puts src
@@ -517,7 +520,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L520
           end
 
           a.for("false and true directives with #{b}") do
-            err = ["#{t.path}:4: warning: mismatched indentations at '#{e}' with '#{k}' at 3"]
+            err = ["#{t.path}:#{n+2}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n+1}"]
             t.rewind
             t.truncate(0)
             t.puts "# -*- warn-indent: false -*-"
@@ -539,7 +542,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L542
           end
 
           a.for("BOM with #{b}") do
-            err = ["#{t.path}:2: warning: mismatched indentations at '#{e}' with '#{k}' at 1"]
+            err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
             t.rewind
             t.truncate(0)
             t.print "\u{feff}"
-- 
cgit v0.10.2


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

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