lib/rubocop/cop/style/if_unless_modifier.rb in rubocop-0.77.0 vs lib/rubocop/cop/style/if_unless_modifier.rb in rubocop-0.78.0
- old
+ new
@@ -5,11 +5,11 @@
module Style
# Checks for `if` and `unless` statements that would fit on one line if
# written as modifier `if`/`unless`. The cop also checks for modifier
# `if`/`unless` lines that exceed the maximum line length.
#
- # The maximum line length is configured in the `Metrics/LineLength`
+ # The maximum line length is configured in the `Layout/LineLength`
# cop. The tab size is configured in the `IndentationWidth` of the
# `Layout/Tab` cop.
#
# @example
# # bad
@@ -30,10 +30,12 @@
# if long_condition
# do_something_in_a_method_with_a_long_name(arg)
# end
class IfUnlessModifier < Cop
include StatementModifier
+ include LineLengthHelp
+ include IgnoredPattern
MSG_USE_MODIFIER = 'Favor modifier `%<keyword>s` usage when having a ' \
'single-line body. Another good alternative is ' \
'the usage of control flow `&&`/`||`.'
MSG_USE_NORMAL =
@@ -64,22 +66,55 @@
->(corrector) { corrector.replace(node.source_range, replacement) }
end
private
+ def ignored_patterns
+ config.for_cop('Layout/LineLength')['IgnoredPatterns'] || []
+ end
+
def too_long_single_line?(node)
return false unless max_line_length
range = node.source_range
return false unless range.first_line == range.last_line
return false unless line_length_enabled_at_line?(range.first_line)
- range.last_column > max_line_length
+ line = range.source_line
+ return false if line_length(line) <= max_line_length
+
+ too_long_line_based_on_config?(range, line)
end
+ def too_long_line_based_on_config?(range, line)
+ return false if matches_ignored_pattern?(line)
+
+ too_long = too_long_line_based_on_ignore_cop_directives?(range, line)
+ return too_long unless too_long == :undetermined
+
+ too_long_line_based_on_allow_uri?(line)
+ end
+
+ def too_long_line_based_on_ignore_cop_directives?(range, line)
+ if ignore_cop_directives? && directive_on_source_line?(range.line - 1)
+ return line_length_without_directive(line) > max_line_length
+ end
+
+ :undetermined
+ end
+
+ def too_long_line_based_on_allow_uri?(line)
+ if allow_uri?
+ uri_range = find_excessive_uri_range(line)
+ return false if uri_range && allowed_uri_position?(line, uri_range)
+ end
+
+ true
+ end
+
def line_length_enabled_at_line?(line)
processed_source.comment_config
- .cop_enabled_at_line?('Metrics/LineLength', line)
+ .cop_enabled_at_line?('Layout/LineLength', line)
end
def named_capture_in_condition?(node)
node.condition.match_with_lvasgn_type?
end