Skip to content

Commit

Permalink
Support Ruby 2.7's pattern matching for Layout/IndentationWidth
Browse files Browse the repository at this point in the history
This PR supports Ruby 2.7's pattern matching for `Layout/IndentationWidth`.
  • Loading branch information
koic committed Jun 7, 2021
1 parent a8ba2f9 commit 9fbb2a9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
@@ -0,0 +1 @@
* [#9857](https://github.com/rubocop/rubocop/pull/9857): Support Ruby 2.7's pattern matching for `Layout/IndentationWidth` cop. ([@koic][])
8 changes: 8 additions & 0 deletions lib/rubocop/cop/layout/indentation_width.rb
Expand Up @@ -138,6 +138,14 @@ def on_case(case_node)
check_indentation(case_node.when_branches.last.loc.keyword, case_node.else_branch)
end

def on_case_match(case_match)
case_match.each_in_pattern do |in_pattern_node|
check_indentation(in_pattern_node.loc.keyword, in_pattern_node.body)
end

check_indentation(case_match.in_pattern_branches.last.loc.keyword, case_match.else_branch)
end

def on_if(node, base = node)
return if ignored_node?(node)
return if node.ternary? || node.modifier_form?
Expand Down
85 changes: 85 additions & 0 deletions spec/rubocop/cop/layout/indentation_width_spec.rb
Expand Up @@ -866,6 +866,91 @@ def x
end
end

context 'with case match', :ruby27 do
it 'registers an offense for bad indentation in a case/in body' do
expect_offense(<<~RUBY)
case a
in b
c
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end

it 'registers an offense for bad indentation in a case/else body' do
expect_offense(<<~RUBY)
case a
in b
c
in d
e
else
f
^^^ Use 2 (not 3) spaces for indentation.
end
RUBY
end

it 'accepts correctly indented case/in/else' do
expect_no_offenses(<<~RUBY)
case a
in b
c
c
in d
else
f
end
RUBY
end

it 'accepts aligned values in `in` clause' do
expect_no_offenses(<<~'RUBY')
case condition
in [42]
foo
in [43]
bar
end
RUBY
end

it 'accepts case/in/else laid out as a table' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
in 'if' then cond, body, _else = *sexp
in 'unless' then cond, _else, body = *sexp
else cond, body = *sexp
end
RUBY
end

it 'accepts case/in/else with then beginning a line' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
in 'if'
then cond, body, _else = *sexp
end
RUBY
end

it 'accepts indented in/else plus indented body' do
# "Indent `in` as deep as `case`" is the job of another cop.
expect_no_offenses(<<~RUBY)
case code_type
in 'ruby' | 'sql' | 'plain'
code_type
in 'erb'
'ruby; html-script: true'
in "html"
'xml'
else
'plain'
end
RUBY
end
end

context 'with while/until' do
it 'registers an offense for bad indentation of a while body' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 9fbb2a9

Please sign in to comment.