From 9fbb2a971f15d8fed6bef811ed013f4d2c225dfa Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 7 Jun 2021 07:13:27 +0900 Subject: [PATCH] Support Ruby 2.7's pattern matching for `Layout/IndentationWidth` This PR supports Ruby 2.7's pattern matching for `Layout/IndentationWidth`. --- ...n_matching_for_layout_indentation_width.md | 1 + lib/rubocop/cop/layout/indentation_width.rb | 8 ++ .../cop/layout/indentation_width_spec.rb | 85 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 changelog/changelog/new_support_pattern_matching_for_layout_indentation_width.md diff --git a/changelog/changelog/new_support_pattern_matching_for_layout_indentation_width.md b/changelog/changelog/new_support_pattern_matching_for_layout_indentation_width.md new file mode 100644 index 00000000000..ec278d79a08 --- /dev/null +++ b/changelog/changelog/new_support_pattern_matching_for_layout_indentation_width.md @@ -0,0 +1 @@ +* [#9857](https://github.com/rubocop/rubocop/pull/9857): Support Ruby 2.7's pattern matching for `Layout/IndentationWidth` cop. ([@koic][]) diff --git a/lib/rubocop/cop/layout/indentation_width.rb b/lib/rubocop/cop/layout/indentation_width.rb index 5eb5596dc6a..a70ecea3594 100644 --- a/lib/rubocop/cop/layout/indentation_width.rb +++ b/lib/rubocop/cop/layout/indentation_width.rb @@ -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? diff --git a/spec/rubocop/cop/layout/indentation_width_spec.rb b/spec/rubocop/cop/layout/indentation_width_spec.rb index 0639a882e6c..2ca69fb771a 100644 --- a/spec/rubocop/cop/layout/indentation_width_spec.rb +++ b/spec/rubocop/cop/layout/indentation_width_spec.rb @@ -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)