diff --git a/changelog/new_support_guard_if_unless_of_pattern_matching_for_layout_space_around_keyword.md b/changelog/new_support_guard_if_unless_of_pattern_matching_for_layout_space_around_keyword.md new file mode 100644 index 00000000000..a35a3f40876 --- /dev/null +++ b/changelog/new_support_guard_if_unless_of_pattern_matching_for_layout_space_around_keyword.md @@ -0,0 +1 @@ +* [#9841](https://github.com/rubocop/rubocop/pull/9841): Support guard `if` and `unless` syntax keywords of Ruby 2.7's pattern matching for `Layout/SpaceAroundKeyword`. ([@koic][]) diff --git a/lib/rubocop/cop/layout/space_around_keyword.rb b/lib/rubocop/cop/layout/space_around_keyword.rb index c5e2da56b59..d968cdd89af 100644 --- a/lib/rubocop/cop/layout/space_around_keyword.rb +++ b/lib/rubocop/cop/layout/space_around_keyword.rb @@ -69,6 +69,10 @@ def on_if(node) check(node, %i[keyword else begin end].freeze, 'then') end + def on_if_guard(node) + check(node, [:keyword].freeze) + end + def on_in_pattern(node) check(node, [:keyword].freeze) end @@ -117,6 +121,10 @@ def on_zsuper(node) check(node, [:keyword].freeze) end + def on_unless_guard(node) + check(node, [:keyword].freeze) + end + def on_until(node) check(node, %i[begin end keyword].freeze) end diff --git a/spec/rubocop/cop/layout/space_around_keyword_spec.rb b/spec/rubocop/cop/layout/space_around_keyword_spec.rb index 603cf946d81..5a54df070f0 100644 --- a/spec/rubocop/cop/layout/space_around_keyword_spec.rb +++ b/spec/rubocop/cop/layout/space_around_keyword_spec.rb @@ -84,6 +84,15 @@ it_behaves_like 'missing before', 'else', 'case a; in b; ""else end', 'case a; in b; "" else end' it_behaves_like 'missing after', 'else', 'case a; in b; else"" end', 'case a; in b; else "" end' + + it_behaves_like 'missing before', 'if', 'case a; in "pattern"if "condition"; else "" end', + 'case a; in "pattern" if "condition"; else "" end' + it_behaves_like 'missing after', 'if', 'case a; in "pattern" if"condition"; else "" end', + 'case a; in "pattern" if "condition"; else "" end' + it_behaves_like 'missing before', 'unless', 'case a; in "pattern"unless "condition"; else "" end', + 'case a; in "pattern" unless "condition"; else "" end' + it_behaves_like 'missing after', 'unless', 'case a; in "pattern" unless"condition"; else "" end', + 'case a; in "pattern" unless "condition"; else "" end' end it_behaves_like 'missing before', 'elsif', 'if a; ""elsif b; end', 'if a; "" elsif b; end'