From 5dfb66eeb65d7c1f0ea240fe6021bc5118377587 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sun, 25 Dec 2022 17:20:56 +0200 Subject: [PATCH] Accept `super` within ranges for `Layout/SpaceAroundKeyword` cop --- ...ce_around_keyword_accept_super_within_ranges.md | 1 + lib/rubocop/cop/layout/space_around_keyword.rb | 2 +- spec/rubocop/cli/autocorrect_spec.rb | 14 ++++++++++++++ .../cop/layout/space_around_keyword_spec.rb | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_space_around_keyword_accept_super_within_ranges.md diff --git a/changelog/fix_space_around_keyword_accept_super_within_ranges.md b/changelog/fix_space_around_keyword_accept_super_within_ranges.md new file mode 100644 index 00000000000..091a3b75527 --- /dev/null +++ b/changelog/fix_space_around_keyword_accept_super_within_ranges.md @@ -0,0 +1 @@ +* [#8751](https://github.com/rubocop/rubocop/issues/8751): Accept `super` within ranges for `Layout/SpaceAroundKeyword` cop. ([@fatkodima][]) diff --git a/lib/rubocop/cop/layout/space_around_keyword.rb b/lib/rubocop/cop/layout/space_around_keyword.rb index d3725cd5d0e..7c91abb2454 100644 --- a/lib/rubocop/cop/layout/space_around_keyword.rb +++ b/lib/rubocop/cop/layout/space_around_keyword.rb @@ -256,7 +256,7 @@ def preceded_by_operator?(node, _range) # regular dotted method calls bind more tightly than operators # so we need to climb up the AST past them node.each_ancestor do |ancestor| - return true if ancestor.and_type? || ancestor.or_type? + return true if ancestor.and_type? || ancestor.or_type? || ancestor.range_type? return false unless ancestor.send_type? return true if ancestor.operator_method? end diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 7ef006b5afe..bc8c8f1ac24 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -653,6 +653,20 @@ def verify_section expect(File.read('example.rb')).to eq(corrected) end + it 'corrects `Layout/SpaceAroundKeyword` with `Layout/SpaceInsideRangeLiteral`' do + source = <<~RUBY + def method + 1..super + end + RUBY + create_file('example.rb', source) + expect( + cli.run(['-a', '--only', 'Layout/SpaceAroundKeyword,Layout/SpaceInsideRangeLiteral']) + ).to eq(0) + expect($stdout.string).to include('no offenses detected') + expect(File.read('example.rb')).to eq(source) + end + it 'corrects LineEndConcatenation offenses leaving the ' \ 'RedundantInterpolation offense unchanged' do # If we change string concatenation from plus to backslash, the string diff --git a/spec/rubocop/cop/layout/space_around_keyword_spec.rb b/spec/rubocop/cop/layout/space_around_keyword_spec.rb index a37ed6069bc..af5395923ea 100644 --- a/spec/rubocop/cop/layout/space_around_keyword_spec.rb +++ b/spec/rubocop/cop/layout/space_around_keyword_spec.rb @@ -172,6 +172,10 @@ # Layout/SpaceAroundBlockParameters it_behaves_like 'accept before', '|', 'loop { |x|break }' + # Layout/SpaceInsideRangeLiteral + it_behaves_like 'accept before', '..', '1..super.size' + it_behaves_like 'accept before', '...', '1...super.size' + # Layout/SpaceAroundOperators it_behaves_like 'accept before', '=', 'a=begin end' it_behaves_like 'accept before', '==', 'a==begin end'