Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Ruby 2.7's pattern matching for Layout/IndentationWidth #9857

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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