Skip to content

Commit

Permalink
Change: SpaceAroundOperators To Work With ExtraSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
jfelchner authored and bbatsov committed Jul 18, 2019
1 parent 1c72cc0 commit bd485d6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/rubocop/cop/layout/space_around_operators.rb
Expand Up @@ -149,9 +149,18 @@ def offense_message(type, operator, with_space, right_operand)
end
end

def excess_leading_space?(operator, with_space)
with_space.source.start_with?(EXCESSIVE_SPACE) &&
(!allow_for_alignment? || !aligned_with_operator?(operator))
def excess_leading_space?(type, operator, with_space)
return false unless allow_for_alignment?
return false unless with_space.source.start_with?(EXCESSIVE_SPACE)

return !aligned_with_operator?(operator) unless type == :assignment

token = Token.new(operator, nil, operator.source)
align_preceding = aligned_with_preceding_assignment(token)

return align_preceding == :no unless align_preceding == :none

aligned_with_subsequent_assignment(token) != :yes
end

def excess_trailing_space?(right_operand, with_space)
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/mixin/preceding_following_alignment.rb
Expand Up @@ -25,6 +25,12 @@ def aligned_with_preceding_assignment(token)
aligned_with_assignment(token, preceding_line_range)
end

def aligned_with_subsequent_assignment(token)
subsequent_line_range = token.line.upto(processed_source.lines.length)

aligned_with_assignment(token, subsequent_line_range)
end

def aligned_with_adjacent_line?(range, predicate)
# minus 2 because node.loc.line is zero-based
pre = (range.line - 2).downto(0)
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -6,6 +6,7 @@
let(:config) do
RuboCop::Config
.new(
'Layout/ExtraSpacing' => { 'ForceEqualSignAlignment' => true },
'Layout/AlignHash' => { 'EnforcedHashRocketStyle' => hash_style },
'Layout/SpaceAroundOperators' => {
'AllowForAlignment' => allow_for_alignment
Expand Down Expand Up @@ -50,6 +51,34 @@ def !
RUBY
end

it 'accepts the result of the ExtraSpacing Cop' do
expect_no_offenses(<<-RUBY.strip_indent)
def batch
@areas = params[:param].map do
var_1 = 123_456
variable_2 = 456_123
end
@another = params[:param].map do
char_1 = begin
variable_1_1 = 'a'
variable_1_20 = 'b'
variable_1_300 = 'c'
# A Comment
variable_1_4000 = 'd'
variable_1_50000 = 'e'
puts 'a non-assignment statement without a blank line'
some_other_length_variable = 'f'
end
var_2 = 456_123
end
render json: @areas
end
RUBY
end

it 'accepts a unary' do
expect_no_offenses(<<~RUBY)
def bm(label_width = 0, *labels, &blk)
Expand Down

0 comments on commit bd485d6

Please sign in to comment.