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

ruby-changes:50934

From: nobu <ko1@a...>
Date: Fri, 13 Apr 2018 07:13:16 +0900 (JST)
Subject: [ruby-changes:50934] nobu:r63141 (trunk): parse.y: `when` indent

nobu	2018-04-13 07:13:09 +0900 (Fri, 13 Apr 2018)

  New Revision: 63141

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63141

  Log:
    parse.y: `when` indent
    
    * parse.y (k_when): warn less-indented `when` than `case`.
      [ruby-core:86492] [Bug #14674]

  Modified files:
    trunk/parse.y
    trunk/test/ruby/test_rubyoptions.rb
Index: test/ruby/test_rubyoptions.rb
===================================================================
--- test/ruby/test_rubyoptions.rb	(revision 63140)
+++ test/ruby/test_rubyoptions.rb	(revision 63141)
@@ -450,13 +450,13 @@ class TestRubyOptions < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L450
           ["begin", "rescue ; end"],
           ["begin rescue", "else ; end"],
           ["begin", "ensure ; end"],
-          ["case nil", "when true; end"],
+          ["  case nil", "when true; end"],
           ["case nil; when true", "end"],
         ].each do
           |b, e = 'end'|
           src = ["#{b}\n", " #{e}\n"]
-          k = b[/\A\S+/]
-          e = e[/\A\S+/]
+          k = b[/\A\s*(\S+)/, 1]
+          e = e[/\A\s*(\S+)/, 1]
 
           a.for("no directives with #{b}") do
             err = ["#{t.path}:2: warning: mismatched indentations at '#{e}' with '#{k}' at 1"]
Index: parse.y
===================================================================
--- parse.y	(revision 63140)
+++ parse.y	(revision 63141)
@@ -738,7 +738,7 @@ PRINTF_ARGS(static void parser_compile_e https://github.com/ruby/ruby/blob/trunk/parse.y#L738
 
 static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
 static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
-static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, const rb_code_location_t *loc);
+static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
 %}
 
 %pure-parser
@@ -2731,31 +2731,31 @@ k_do_block	: keyword_do_block https://github.com/ruby/ruby/blob/trunk/parse.y#L2731
 
 k_rescue	: keyword_rescue
 		    {
-			token_info_warn(p, "rescue", p->token_info, &@$);
+			token_info_warn(p, "rescue", p->token_info, 1, &@$);
 		    }
 		;
 
 k_ensure	: keyword_ensure
 		    {
-			token_info_warn(p, "ensure", p->token_info, &@$);
+			token_info_warn(p, "ensure", p->token_info, 1, &@$);
 		    }
 		;
 
 k_when		: keyword_when
 		    {
-			token_info_warn(p, "when", p->token_info, &@$);
+			token_info_warn(p, "when", p->token_info, 0, &@$);
 		    }
 		;
 
 k_else		: keyword_else
 		    {
-			token_info_warn(p, "else", p->token_info, &@$);
+			token_info_warn(p, "else", p->token_info, 1, &@$);
 		    }
 		;
 
 k_elsif 	: keyword_elsif
 		    {
-			token_info_warn(p, "elsif", p->token_info, &@$);
+			token_info_warn(p, "elsif", p->token_info, 1, &@$);
 		    }
 		;
 
@@ -4533,12 +4533,12 @@ token_info_pop(struct parser_params *p, https://github.com/ruby/ruby/blob/trunk/parse.y#L4533
     p->token_info = ptinfo_beg->next;
 
     /* indentation check of matched keywords (begin..end, if..end, etc.) */
-    token_info_warn(p, token, ptinfo_beg, loc);
+    token_info_warn(p, token, ptinfo_beg, 1, loc);
     xfree(ptinfo_beg);
 }
 
 static void
-token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, const rb_code_location_t *loc)
+token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
 {
     token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
     if (!p->token_info_enabled) return;
@@ -4547,6 +4547,7 @@ token_info_warn(struct parser_params *p, https://github.com/ruby/ruby/blob/trunk/parse.y#L4547
     if (ptinfo_beg->linenum == ptinfo_end->linenum) return; /* ignore one-line block */
     if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
     if (ptinfo_beg->column == ptinfo_end->column) return; /* the indents are matched */
+    if (!same && ptinfo_beg->column < ptinfo_end->column) return;
     rb_warn3L(ptinfo_end->linenum,
 	      "mismatched indentations at '%s' with '%s' at %d",
 	      WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->linenum));

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

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