mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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]
This commit is contained in:
parent
789e49dc7e
commit
d548073f68
2 changed files with 17 additions and 5 deletions
9
parse.y
9
parse.y
|
@ -3003,6 +3003,15 @@ k_begin : keyword_begin
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
|
|
|
@ -491,14 +491,17 @@ class TestRubyOptions < Test::Unit::TestCase
|
|||
["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
|
|||
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
|
|||
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}"
|
||||
|
|
Loading…
Add table
Reference in a new issue