From c8003390743c8858f1a672771b7a26bb34df1d0f Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 5 Jun 2017 14:58:29 -0400 Subject: [PATCH] [Fix #4418] Fix the check for the longest line when the longest line is the assignment --- CHANGELOG.md | 1 + .../cop/style/conditional_assignment.rb | 22 +++---------------- ...nal_assignment_assign_to_condition_spec.rb | 15 +++++++++++++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c145496157f9..a9823231680c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#4434](https://github.com/bbatsov/rubocop/issues/4434): Prevent bad auto-correct in `Style/Alias` for non-literal arguments. ([@drenmi][]) * [#4451](https://github.com/bbatsov/rubocop/issues/4451): Make `Style/AndOr` cop aware of comparison methods. ([@drenmi][]) * [#4457](https://github.com/bbatsov/rubocop/pull/4457): Fix false negative in `Lint/Void` with initialize and setter methods. ([@pocke][]) +* [#4418](https://github.com/bbatsov/rubocop/issues/4418): Register an offense in `Style/ConditionalAssignment` when the assignment line is the longest line, and it does not exceed the max line length. ([@rrosenblum][]) ### Changes diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb index 7a35ca5e80ff..29e0883eda06 100644 --- a/lib/rubocop/cop/style/conditional_assignment.rb +++ b/lib/rubocop/cop/style/conditional_assignment.rb @@ -380,36 +380,20 @@ def correction_exceeds_line_limit?(node, branches) assignment = lhs(tail(branches[0])) - longest_rhs_exceeds_line_limit?(branches, assignment) || - longest_line_exceeds_line_limit?(node, assignment) - end - - def longest_rhs_exceeds_line_limit?(branches, assignment) - longest_rhs_full_length(branches, assignment) > max_line_length + longest_line_exceeds_line_limit?(node, assignment) end def longest_line_exceeds_line_limit?(node, assignment) longest_line(node, assignment).length > max_line_length end - def longest_rhs_full_length(branches, assignment) - longest_rhs(branches) + indentation_width + assignment.length - end - def longest_line(node, assignment) - assignment_regex = /#{Regexp.escape(assignment).gsub(' ', '\s*')}/ + assignment_regex = /\s*#{Regexp.escape(assignment).gsub('\ ', '\s*')}/ lines = node.source.lines.map do |line| line.chomp.sub(assignment_regex, '') end longest_line = lines.max_by(&:length) - longest_line + assignment - end - - def longest_rhs(branches) - line_lengths = branches.flat_map do |branch| - branch.children.last.source.split("\n").map(&:length) - end - line_lengths.max + assignment + longest_line end def line_length_cop_enabled? diff --git a/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb b/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb index eae89fea4963..b0ac0ce770a7 100644 --- a/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb +++ b/spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb @@ -344,6 +344,21 @@ RUBY end + it 'registers an offense in an if else if the assignment is already ' \ + 'at the line length limit' do + source = <<-RUBY.strip_indent + if foo + bar = #{'a' * 72} + else + bar = #{'b' * 72} + end + RUBY + + inspect_source(cop, source) + + expect(cop.messages).to eq([message]) + end + context 'correction would exceed max line length' do it 'allows assignment to the same variable in if else if the correction ' \ 'would create a line longer than the configured LineLength' do