Skip to content

Commit

Permalink
Support Ruby 2.7's pattern matching syntax for `Layout/SpaceAroundKey…
Browse files Browse the repository at this point in the history
…word`

This PR supports Ruby 2.7's pattern matching syntax for `Layout/SpaceAroundKeyword`.

The following is a technical note.

There are the following differences between `case ... when` and `case ... in`.
So there is a TODO test for this patch's test and I confirmed this syntax below:
https://bugs.ruby-lang.org/issues/17925

An answer will determine whether to enable or discard the commented out test in the future.
  • Loading branch information
koic authored and bbatsov committed May 30, 2021
1 parent b47de70 commit 3d2354c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rubocop/cop/layout/space_around_keyword.rb
Expand Up @@ -53,6 +53,10 @@ def on_case(node)
check(node, %i[keyword else].freeze)
end

def on_case_match(node)
check(node, %i[keyword else].freeze)
end

def on_ensure(node)
check(node, [:keyword].freeze)
end
Expand All @@ -65,6 +69,10 @@ def on_if(node)
check(node, %i[keyword else begin end].freeze, 'then')
end

def on_in_pattern(node)
check(node, [:keyword].freeze)
end

def on_kwbegin(node)
check(node, %i[begin end].freeze, nil)
end
Expand Down
@@ -0,0 +1 @@
* [#9838](https://github.com/rubocop/rubocop/pull/9838): Support Ruby 2.7's pattern matching syntax for `Layout/SpaceAroundKeyword`. ([@koic][])
16 changes: 16 additions & 0 deletions spec/rubocop/cop/layout/space_around_keyword_spec.rb
Expand Up @@ -51,6 +51,9 @@
it_behaves_like 'missing after', 'break', 'break""', 'break ""'
it_behaves_like 'accept after', '(', 'break(1)'
it_behaves_like 'missing after', 'case', 'case"" when 1; end', 'case "" when 1; end'
context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription
it_behaves_like 'missing after', 'case', 'case""; in 1; end', 'case ""; in 1; end'
end

it_behaves_like 'missing before', 'do', 'a "b"do end', 'a "b" do end'
it_behaves_like 'missing after', 'do', 'a do|x| end', 'a do |x| end'
Expand All @@ -77,6 +80,11 @@
'case a; when b; "" else end'
it_behaves_like 'missing after', 'else', 'case a; when b; else"" end',
'case a; when b; else "" end'
context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription
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'
end

it_behaves_like 'missing before', 'elsif', 'if a; ""elsif b; end', 'if a; "" elsif b; end'
it_behaves_like 'missing after', 'elsif', 'if a; elsif""; end', 'if a; elsif ""; end'
Expand Down Expand Up @@ -110,6 +118,14 @@
it_behaves_like 'missing after', 'until', '1 until""', '1 until ""'
it_behaves_like 'missing before', 'when', 'case ""when a; end', 'case "" when a; end'
it_behaves_like 'missing after', 'when', 'case a when""; end', 'case a when ""; end'
context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription
# TODO: `case ""in a; end` is syntax error in Ruby 3.0.1.
# This syntax is confirmed: https://bugs.ruby-lang.org/issues/17925
# The answer will determine whether to enable or discard the test in the future.
# it_behaves_like 'missing before', 'in', 'case ""in a; end', 'case "" in a; end'
it_behaves_like 'missing after', 'in', 'case a; in""; end', 'case a; in ""; end'
end

it_behaves_like 'missing before', 'while', '1while ""', '1 while ""'
it_behaves_like 'missing after', 'while', '1 while""', '1 while ""'
it_behaves_like 'missing after', 'yield', 'yield""', 'yield ""'
Expand Down

0 comments on commit 3d2354c

Please sign in to comment.