Skip to content

Commit

Permalink
Use Cop::Base API for Layout department [F-M]
Browse files Browse the repository at this point in the history
Follow rubocop#7868.

This PR uses `Cop::Base` API for almost `Style` department's cops.
It targets cop that names begin between F and M. And some F-M cops
will be excluded from this PR target and addressed separately.
  • Loading branch information
koic authored and bbatsov committed Aug 28, 2020
1 parent af336ca commit ed864d9
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 177 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/correctors/empty_line_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def correct(corrector, node)
end
end

def insert_before(node)
->(corrector) { corrector.insert_before(node, "\n") }
def insert_before(corrector, node)
corrector.insert_before(node, "\n")
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ class MultilineLiteralBraceCorrector
include MultilineLiteralBraceLayout
include RangeHelp

def initialize(node, processed_source)
def self.correct(corrector, node, processed_source)
new(corrector, node, processed_source).call
end

def initialize(corrector, node, processed_source)
@corrector = corrector
@node = node
@processed_source = processed_source
end

def call(corrector)
def call
if closing_brace_on_same_line?(node)
correct_same_line_brace(corrector)
else
Expand All @@ -29,7 +34,7 @@ def call(corrector)

private

attr_reader :node, :processed_source
attr_reader :corrector, :node, :processed_source

def correct_same_line_brace(corrector)
corrector.insert_before(node.loc.end, "\n")
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/layout/first_array_element_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Layout
# :b]
#
# @api private
class FirstArrayElementLineBreak < Cop
class FirstArrayElementLineBreak < Base
include FirstElementLineBreak
extend AutoCorrector

MSG = 'Add a line break before the first element of a ' \
'multi-line array.'
Expand All @@ -30,10 +31,6 @@ def on_array(node)
check_children_line_break(node, node.children)
end

def autocorrect(node)
EmptyLineCorrector.insert_before(node)
end

private

def assignment_on_same_line?(node)
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/layout/first_hash_element_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Layout
# b: 2 }
#
# @api private
class FirstHashElementLineBreak < Cop
class FirstHashElementLineBreak < Base
include FirstElementLineBreak
extend AutoCorrector

MSG = 'Add a line break before the first element of a ' \
'multi-line hash.'
Expand All @@ -29,10 +30,6 @@ def on_hash(node)
# If it doesn't, Style/FirstMethodArgumentLineBreak will handle it
check_children_line_break(node, node.children) if node.loc.begin
end

def autocorrect(node)
EmptyLineCorrector.insert_before(node)
end
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/layout/first_method_argument_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ module Layout
# baz
#
# @api private
class FirstMethodArgumentLineBreak < Cop
class FirstMethodArgumentLineBreak < Base
include FirstElementLineBreak
extend AutoCorrector

MSG = 'Add a line break before the first argument of a ' \
'multi-line method argument list.'
Expand All @@ -44,10 +45,6 @@ def on_send(node)
end
alias on_csend on_send
alias on_super on_send

def autocorrect(node)
EmptyLineCorrector.insert_before(node)
end
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/layout/first_method_parameter_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ module Layout
# end
#
# @api private
class FirstMethodParameterLineBreak < Cop
class FirstMethodParameterLineBreak < Base
include FirstElementLineBreak
extend AutoCorrector

MSG = 'Add a line break before the first parameter of a ' \
'multi-line method parameter list.'
Expand All @@ -38,10 +39,6 @@ def on_def(node)
check_method_line_break(node, node.arguments)
end
alias on_defs on_def

def autocorrect(node)
EmptyLineCorrector.insert_before(node)
end
end
end
end
Expand Down
37 changes: 17 additions & 20 deletions lib/rubocop/cop/layout/hash_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ module Layout
# bar: 2})
#
# @api private
class HashAlignment < Cop
class HashAlignment < Base
include HashAlignmentStyles
include RangeHelp
extend AutoCorrector

MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \
'they span more than one line.',
Expand Down Expand Up @@ -213,13 +214,6 @@ def on_hash(node) # rubocop:todo Metrics/CyclomaticComplexity
check_pairs(node)
end

def autocorrect(node)
delta = column_deltas[alignment_for(node).first.class][node]
return if delta.nil?

correct_node(node, delta)
end

attr_accessor :offences_by, :column_deltas

private
Expand Down Expand Up @@ -255,7 +249,11 @@ def check_pairs(node)
def add_offences
format, offences = offences_by.min_by { |_, v| v.length }
(offences || []).each do |offence|
add_offense(offence, message: MESSAGES[format])
add_offense(offence, message: MESSAGES[format]) do |corrector|
delta = column_deltas[alignment_for(offence).first.class][offence]

correct_node(corrector, offence, delta) unless delta.nil?
end
end
end

Expand Down Expand Up @@ -294,25 +292,26 @@ def alignment_for_colons
new_alignment('EnforcedColonStyle')
end

def correct_node(node, delta)
def correct_node(corrector, node, delta)
# We can't use the instance variable inside the lambda. That would
# just give each lambda the same reference and they would all get the
# last value of each. A local variable fixes the problem.

if !node.value
correct_no_value(delta[:key] || 0, node.source_range)
delta_value = delta[:key] || 0
correct_no_value(corrector, delta_value, node.source_range)
else
correct_key_value(delta, node.key.source_range,
correct_key_value(corrector, delta, node.key.source_range,
node.value.source_range,
node.loc.operator)
end
end

def correct_no_value(key_delta, key)
->(corrector) { adjust(corrector, key_delta, key) }
def correct_no_value(corrector, key_delta, key)
adjust(corrector, key_delta, key)
end

def correct_key_value(delta, key, value, separator)
def correct_key_value(corrector, delta, key, value, separator)
# We can't use the instance variable inside the lambda. That would
# just give each lambda the same reference and they would all get the
# last value of each. Some local variables fix the problem.
Expand All @@ -323,11 +322,9 @@ def correct_key_value(delta, key, value, separator)
key_column = key.column
key_delta = -key_column if key_delta < -key_column

lambda do |corrector|
adjust(corrector, key_delta, key)
adjust(corrector, separator_delta, separator)
adjust(corrector, value_delta, value)
end
adjust(corrector, key_delta, key)
adjust(corrector, separator_delta, separator)
adjust(corrector, value_delta, value)
end

def new_alignment(key)
Expand Down
29 changes: 15 additions & 14 deletions lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ module Layout
# )
#
# @api private
class HeredocArgumentClosingParenthesis < Cop
class HeredocArgumentClosingParenthesis < Base
include RangeHelp
extend AutoCorrector

MSG = 'Put the closing parenthesis for a method call with a ' \
'HEREDOC parameter on the same line as the HEREDOC opening.'

def self.autocorrect_incompatible_with
[Style::TrailingCommaInArguments]
end

def on_send(node)
heredoc_arg = extract_heredoc_argument(node)
return unless heredoc_arg
Expand All @@ -66,9 +71,13 @@ def on_send(node)
return unless outermost_send.loc.end
return unless heredoc_arg.first_line != outermost_send.loc.end.line

add_offense(outermost_send, location: :end)
add_offense(outermost_send.loc.end) do |corrector|
autocorrect(corrector, outermost_send)
end
end

private

# Autocorrection note:
#
# Commas are a bit tricky to handle when the method call is
Expand All @@ -95,22 +104,14 @@ def on_send(node)
# SQL
# third_array_value,
# ]
def autocorrect(node)
lambda do |corrector|
fix_closing_parenthesis(node, corrector)
def autocorrect(corrector, node)
fix_closing_parenthesis(node, corrector)

remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node)
remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node)

fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node)
end
fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node)
end

def self.autocorrect_incompatible_with
[Style::TrailingCommaInArguments]
end

private

def outermost_send_on_same_line(heredoc)
previous = heredoc
current = previous.parent
Expand Down
24 changes: 14 additions & 10 deletions lib/rubocop/cop/layout/heredoc_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ module Layout
#
#
# @api private
class HeredocIndentation < Cop
class HeredocIndentation < Base
include Heredoc
extend AutoCorrector

TYPE_MSG = 'Use %<indentation_width>d spaces for indentation in a ' \
'heredoc by using `<<~` instead of `%<current_indent_type>s`.'
Expand All @@ -35,8 +36,9 @@ def on_heredoc(node)
return if body.strip.empty?

body_indent_level = indent_level(body)
heredoc_indent_type = heredoc_indent_type(node)

if heredoc_indent_type(node) == '~'
if heredoc_indent_type == '~'
expected_indent_level = base_indent_level(node) + indentation_width
return if expected_indent_level == body_indent_level
else
Expand All @@ -45,23 +47,25 @@ def on_heredoc(node)

return if line_too_long?(node)

add_offense(node, location: :heredoc_body)
register_offense(node, heredoc_indent_type)
end

def autocorrect(node)
lambda do |corrector|
if heredoc_indent_type(node) == '~'
private

def register_offense(node, heredoc_indent_type)
message = message(heredoc_indent_type)

add_offense(node.loc.heredoc_body, message: message) do |corrector|
if heredoc_indent_type == '~'
adjust_squiggly(corrector, node)
else
adjust_minus(corrector, node)
end
end
end

private

def message(node)
current_indent_type = "<<#{heredoc_indent_type(node)}"
def message(heredoc_indent_type)
current_indent_type = "<<#{heredoc_indent_type}"

if current_indent_type == '<<~'
width_message(indentation_width)
Expand Down
13 changes: 6 additions & 7 deletions lib/rubocop/cop/layout/initial_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ module Layout
# end
#
# @api private
class InitialIndentation < Cop
class InitialIndentation < Base
include RangeHelp
extend AutoCorrector

MSG = 'Indentation of first line in file detected.'

def investigate(_processed_source)
def on_new_investigation
space_before(first_token) do |space|
add_offense(space, location: first_token.pos)
add_offense(first_token.pos) do |corrector|
corrector.remove(space)
end
end
end

def autocorrect(range)
->(corrector) { corrector.remove(range) }
end

private

def first_token
Expand Down

0 comments on commit ed864d9

Please sign in to comment.