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

ruby-changes:56391

From: Nobuyoshi <ko1@a...>
Date: Mon, 8 Jul 2019 17:14:04 +0900 (JST)
Subject: [ruby-changes:56391] Nobuyoshi Nakada: 995ae6d529 (master): Check indent of `end` against `else` if present

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

From 995ae6d529d6bef62ca756d4b1dbc1e01f062b8a Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 8 Jul 2019 17:09:59 +0900
Subject: Check indent of `end` against `else` if present


diff --git a/parse.y b/parse.y
index 33f1ef0..43fa102 100644
--- a/parse.y
+++ b/parse.y
@@ -905,6 +905,7 @@ PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char * https://github.com/ruby/ruby/blob/trunk/parse.y#L905
 # define compile_error parser_compile_error
 #endif
 
+static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
 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, int same, const rb_code_location_t *loc);
@@ -3004,11 +3005,12 @@ k_if		: keyword_if https://github.com/ruby/ruby/blob/trunk/parse.y#L3005
 		    {
 			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));
+			    p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
+			    const char *tok = p->lex.ptok;
+			    const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
+			    beg += rb_strlen_lit("else");
 			    while (beg < tok && ISSPACE(*beg)) beg++;
-			    if (tok - beg == 3 && !memcmp(beg, "else", 4)) {
+			    if (beg == tok) {
 				p->token_info->nonspc = 0;
 			    }
 			}
@@ -3098,6 +3100,13 @@ k_else		: keyword_else https://github.com/ruby/ruby/blob/trunk/parse.y#L3100
 			token_info *ptinfo_beg = p->token_info;
 			int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
 			token_info_warn(p, "else", p->token_info, same, &@$);
+			if (same) {
+			    token_info e;
+			    e.next = ptinfo_beg->next;
+			    e.token = "else";
+			    token_info_setup(&e, p->lex.pbeg, &@$);
+			    if (!e.nonspc) *ptinfo_beg = e;
+			}
 		    }
 		;
 
@@ -5383,7 +5392,7 @@ parser_isascii(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L5392
 }
 
 static void
-setup_token_info(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
+token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
 {
     int column = 1, nonspc = 0, i;
     for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
@@ -5410,7 +5419,7 @@ token_info_push(struct parser_params *p, const char *token, const rb_code_locati https://github.com/ruby/ruby/blob/trunk/parse.y#L5419
     ptinfo = ALLOC(token_info);
     ptinfo->token = token;
     ptinfo->next = p->token_info;
-    setup_token_info(ptinfo, p->lex.pbeg, loc);
+    token_info_setup(ptinfo, p->lex.pbeg, loc);
 
     p->token_info = ptinfo;
 }
@@ -5434,7 +5443,7 @@ token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_b https://github.com/ruby/ruby/blob/trunk/parse.y#L5443
     token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
     if (!p->token_info_enabled) return;
     if (!ptinfo_beg) return;
-    setup_token_info(ptinfo_end, p->lex.pbeg, loc);
+    token_info_setup(ptinfo_end, p->lex.pbeg, loc);
     if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
     if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
     if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index d63ca5d..0d22ba2 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -492,6 +492,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L492
           ["  case nil", "when true; end"],
           ["case nil; when true", "end"],
           ["if false;", "end", "if true\nelse ", "end"],
+          ["else", " end", "_ = if true\n"],
         ].each do
           |b, e = 'end', pre = nil, post = nil|
           src = ["#{pre}#{b}\n", " #{e}\n#{post}"]
@@ -500,7 +501,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L501
           n = 2
           n += pre.count("\n") if pre
 
-          a.for("no directives with #{b}") do
+          a.for("no directives with #{src}") do
             err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
             t.rewind
             t.truncate(0)
@@ -510,7 +511,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L511
             assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
           end
 
-          a.for("false directive with #{b}") do
+          a.for("false directive with #{src}") do
             t.rewind
             t.truncate(0)
             t.puts "# -*- warn-indent: false -*-"
@@ -519,7 +520,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L520
             assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
           end
 
-          a.for("false and true directives with #{b}") do
+          a.for("false and true directives with #{src}") do
             err = ["#{t.path}:#{n+2}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n+1}"]
             t.rewind
             t.truncate(0)
@@ -530,7 +531,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L531
             assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
           end
 
-          a.for("false directives after #{b}") do
+          a.for("false directives after #{src}") do
             t.rewind
             t.truncate(0)
             t.puts "# -*- warn-indent: true -*-"
@@ -541,7 +542,7 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L542
             assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
           end
 
-          a.for("BOM with #{b}") do
+          a.for("BOM with #{src}") do
             err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
             t.rewind
             t.truncate(0)
-- 
cgit v0.10.2


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

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