Skip to content

Commit

Permalink
Enable Layout/RedundantLineBreak cop
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Jul 30, 2022
1 parent e007571 commit 1e3f498
Show file tree
Hide file tree
Showing 67 changed files with 228 additions and 574 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -63,6 +63,9 @@ Style/FormatStringToken:
Exclude:
- spec/**/*

Layout/RedundantLineBreak:
Enabled: true

Layout/HashAlignment:
EnforcedHashRocketStyle:
- key
Expand Down
10 changes: 2 additions & 8 deletions Rakefile
Expand Up @@ -36,11 +36,7 @@ end
desc 'Run RuboCop over itself'
RuboCop::RakeTask.new(:internal_investigation)

task default: %i[
documentation_syntax_check
spec
internal_investigation
]
task default: %i[documentation_syntax_check spec internal_investigation]

desc 'Generate a new cop template'
task :new_cop, [:cop] do |_task, args|
Expand All @@ -55,9 +51,7 @@ task :new_cop, [:cop] do |_task, args|

generator.write_source
generator.write_spec
generator.inject_require(
root_file_path: 'lib/rubocop/cop/rails_cops.rb'
)
generator.inject_require(root_file_path: 'lib/rubocop/cop/rails_cops.rb')
generator.inject_config(config_file_path: 'config/default.yml')

puts generator.todo
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/mixin/active_record_helper.rb
Expand Up @@ -48,10 +48,7 @@ def table_name(class_node)

class_nodes = class_node.defined_module.each_node
namespaces = class_node.each_ancestor(:class, :module).map(&:identifier)
[*class_nodes, *namespaces]
.reverse
.map { |node| node.children[1] }.join('_')
.tableize
[*class_nodes, *namespaces].reverse.map { |node| node.children[1] }.join('_').tableize
end

# Resolve relation into column name.
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/mixin/active_record_migrations_helper.rb
Expand Up @@ -10,9 +10,7 @@ module ActiveRecordMigrationsHelper
bigint binary boolean date datetime decimal float integer json string
text time timestamp virtual
].freeze
RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS = %i[
column references belongs_to primary_key numeric
].freeze
RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS = %i[column references belongs_to primary_key numeric].freeze
POSTGRES_SCHEMA_DEFINITIONS = %i[
bigserial bit bit_varying cidr citext daterange hstore inet interval
int4range int8range jsonb ltree macaddr money numrange oid point line
Expand Down
18 changes: 4 additions & 14 deletions lib/rubocop/cop/mixin/index_method.rb
Expand Up @@ -98,10 +98,7 @@ def execute_correction(corrector, node, correction)

captures = extract_captures(correction.match)
correction.set_new_arg_name(captures.transformed_argname, corrector)
correction.set_new_body_expression(
captures.transforming_body_expr,
corrector
)
correction.set_new_body_expression(captures.transforming_body_expr, corrector)
end

# Internal helper class to hold match data
Expand All @@ -110,8 +107,7 @@ def execute_correction(corrector, node, correction)
:transforming_body_expr
) do
def noop_transformation?
transforming_body_expr.lvar_type? &&
transforming_body_expr.children == [transformed_argname]
transforming_body_expr.lvar_type? && transforming_body_expr.children == [transformed_argname]
end
end

Expand Down Expand Up @@ -157,17 +153,11 @@ def set_new_method_name(new_method_name, corrector)
end

def set_new_arg_name(transformed_argname, corrector)
corrector.replace(
block_node.arguments.loc.expression,
"|#{transformed_argname}|"
)
corrector.replace(block_node.arguments.loc.expression, "|#{transformed_argname}|")
end

def set_new_body_expression(transforming_body_expr, corrector)
corrector.replace(
block_node.body.loc.expression,
transforming_body_expr.loc.expression.source
)
corrector.replace(block_node.body.loc.expression, transforming_body_expr.loc.expression.source)
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/rails/active_record_aliases.rb
Expand Up @@ -21,10 +21,7 @@ class ActiveRecordAliases < Base

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'

ALIASES = {
update_attributes: :update,
update_attributes!: :update!
}.freeze
ALIASES = { update_attributes: :update, update_attributes!: :update! }.freeze

RESTRICT_ON_SEND = ALIASES.keys.freeze

Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/rails/active_record_override.rb
Expand Up @@ -25,12 +25,9 @@ module Rails
# end
#
class ActiveRecordOverride < Base
MSG =
'Use %<prefer>s callbacks instead of overriding the Active Record ' \
'method `%<bad>s`.'
MSG = 'Use %<prefer>s callbacks instead of overriding the Active Record method `%<bad>s`.'
BAD_METHODS = %i[create destroy save update].freeze
ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base
ActiveRecord::Base].freeze
ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base ActiveRecord::Base].freeze

def on_def(node)
return unless BAD_METHODS.include?(node.method_name)
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/rails/add_column_index.rb
Expand Up @@ -53,10 +53,7 @@ def on_send(node)
private

def index_range(pair_node)
range_with_surrounding_comma(
range_with_surrounding_space(pair_node.loc.expression, side: :left),
:left
)
range_with_surrounding_comma(range_with_surrounding_space(pair_node.loc.expression, side: :left), :left)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/rails/blank.rb
Expand Up @@ -63,8 +63,7 @@ class Blank < Base

MSG_NIL_OR_EMPTY = 'Use `%<prefer>s` instead of `%<current>s`.'
MSG_NOT_PRESENT = 'Use `%<prefer>s` instead of `%<current>s`.'
MSG_UNLESS_PRESENT = 'Use `if %<prefer>s` instead of ' \
'`%<current>s`.'
MSG_UNLESS_PRESENT = 'Use `if %<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[!].freeze

# `(send nil $_)` is not actually a valid match for an offense. Nodes
Expand Down
26 changes: 6 additions & 20 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Expand Up @@ -111,25 +111,13 @@ class BulkChangeTable < Base
remove_timestamps
].freeze

MYSQL_COMBINABLE_TRANSFORMATIONS = %i[
rename
index
remove_index
].freeze
MYSQL_COMBINABLE_TRANSFORMATIONS = %i[rename index remove_index].freeze

MYSQL_COMBINABLE_ALTER_METHODS = %i[
rename_column
add_index
remove_index
].freeze
MYSQL_COMBINABLE_ALTER_METHODS = %i[rename_column add_index remove_index].freeze

POSTGRESQL_COMBINABLE_TRANSFORMATIONS = %i[
change_default
].freeze
POSTGRESQL_COMBINABLE_TRANSFORMATIONS = %i[change_default].freeze

POSTGRESQL_COMBINABLE_ALTER_METHODS = %i[
change_column_default
].freeze
POSTGRESQL_COMBINABLE_ALTER_METHODS = %i[change_column_default].freeze

def on_def(node)
return unless support_bulk_alter?
Expand Down Expand Up @@ -186,8 +174,7 @@ def include_bulk_options?(node)
options = node.arguments[1]
return false unless options

options.hash_type? &&
options.keys.any? { |key| key.sym_type? && key.value == :bulk }
options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk }
end

def database
Expand Down Expand Up @@ -237,8 +224,7 @@ def support_bulk_alter?
end

def call_to_combinable_alter_method?(child_node)
child_node.send_type? &&
combinable_alter_methods.include?(child_node.method_name)
child_node.send_type? && combinable_alter_methods.include?(child_node.method_name)
end

def combinable_alter_methods
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/rails/content_tag.rb
Expand Up @@ -37,9 +37,7 @@ def on_send(node)
return if node.arguments.count >= 3

first_argument = node.first_argument
return if !first_argument ||
allowed_argument?(first_argument) ||
corrected_ancestor?(node)
return if !first_argument || allowed_argument?(first_argument) || corrected_ancestor?(node)

preferred_method = node.first_argument.value.to_s.underscore
message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)
Expand Down
13 changes: 4 additions & 9 deletions lib/rubocop/cop/rails/date.rb
Expand Up @@ -53,22 +53,17 @@ module Rails
class Date < Base
include ConfigurableEnforcedStyle

MSG = 'Do not use `Date.%<method_called>s` without zone. Use ' \
'`Time.zone.%<day>s` instead.'
MSG = 'Do not use `Date.%<method_called>s` without zone. Use `Time.zone.%<day>s` instead.'

MSG_SEND = 'Do not use `%<method>s` on Date objects, because they ' \
'know nothing about the time zone in use.'
MSG_SEND = 'Do not use `%<method>s` on Date objects, because they know nothing about the time zone in use.'

RESTRICT_ON_SEND = %i[to_time to_time_in_current_zone].freeze

BAD_DAYS = %i[today current yesterday tomorrow].freeze

DEPRECATED_METHODS = [
{ deprecated: 'to_time_in_current_zone', relevant: 'in_time_zone' }
].freeze
DEPRECATED_METHODS = [{ deprecated: 'to_time_in_current_zone', relevant: 'in_time_zone' }].freeze

DEPRECATED_MSG = '`%<deprecated>s` is deprecated. ' \
'Use `%<relevant>s` instead.'
DEPRECATED_MSG = '`%<deprecated>s` is deprecated. Use `%<relevant>s` instead.'

def on_const(node)
mod, klass = *node.children
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/rails/delegate.rb
Expand Up @@ -93,15 +93,12 @@ def arguments_match?(arg_array, body)
return false if arg_array.size != argument_array.size

arg_array.zip(argument_array).all? do |arg, argument|
arg.arg_type? &&
argument.lvar_type? &&
arg.children == argument.children
arg.arg_type? && argument.lvar_type? && arg.children == argument.children
end
end

def method_name_matches?(method_name, body)
method_name == body.method_name ||
(include_prefix_case? && method_name == prefixed_method_name(body))
method_name == body.method_name || (include_prefix_case? && method_name == prefixed_method_name(body))
end

def include_prefix_case?
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/rails/dynamic_find_by.rb
Expand Up @@ -70,8 +70,7 @@ def autocorrect(corrector, node)
end

def allowed_invocation?(node)
allowed_method?(node) || allowed_receiver?(node) ||
whitelisted?(node)
allowed_method?(node) || allowed_receiver?(node) || whitelisted?(node)
end

def allowed_method?(node)
Expand All @@ -95,8 +94,7 @@ def whitelisted?(node)
end

def autocorrect_method_name(corrector, node)
corrector.replace(node.loc.selector,
static_method_name(node.method_name.to_s))
corrector.replace(node.loc.selector, static_method_name(node.method_name.to_s))
end

def autocorrect_argument_keywords(corrector, node, keywords)
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/rails/enum_uniqueness.rb
Expand Up @@ -20,8 +20,7 @@ module Rails
class EnumUniqueness < Base
include Duplication

MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
'enum declaration.'
MSG = 'Duplicate value `%<value>s` found in `%<enum>s` enum declaration.'
RESTRICT_ON_SEND = %i[enum].freeze

def_node_matcher :enum?, <<~PATTERN
Expand All @@ -41,9 +40,7 @@ def on_send(node)
next unless duplicates?(items)

consecutive_duplicates(items).each do |item|
add_offense(item, message: format(
MSG, value: item.source, enum: enum_name(key)
))
add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/rails/environment_comparison.rb
Expand Up @@ -96,8 +96,7 @@ def build_predicate_method(node)
end

def rails_env_on_lhs?(node)
comparing_str_env_with_rails_env_on_lhs?(node) ||
comparing_sym_env_with_rails_env_on_lhs?(node)
comparing_str_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_lhs?(node)
end

def build_predicate_method_for_rails_env_on_lhs(node)
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/rails/file_path.rb
Expand Up @@ -50,8 +50,7 @@ def on_dstr(node)
return unless node.children.last.str_type?

last_child_source = node.children.last.source
return unless last_child_source.start_with?('.') ||
last_child_source.include?(File::SEPARATOR)
return unless last_child_source.start_with?('.') || last_child_source.include?(File::SEPARATOR)
return if last_child_source.start_with?(':')

register_offense(node)
Expand Down Expand Up @@ -97,8 +96,7 @@ def string_with_slash?(node)

def register_offense(node)
line_range = node.loc.column...node.loc.last_column
source_range = source_range(processed_source.buffer, node.first_line,
line_range)
source_range = source_range(processed_source.buffer, node.first_line, line_range)
add_offense(source_range)
end

Expand Down
7 changes: 3 additions & 4 deletions lib/rubocop/cop/rails/freeze_time.rb
Expand Up @@ -44,10 +44,9 @@ class FreezeTime < Base

def on_send(node)
child_node, method_name = *node.first_argument.children
if current_time?(child_node, method_name) ||
current_time_with_convert?(child_node, method_name)
add_offense(node) { |corrector| corrector.replace(node, 'freeze_time') }
end
return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)

add_offense(node) { |corrector| corrector.replace(node, 'freeze_time') }
end

private
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
Expand Up @@ -105,9 +105,7 @@ def contain_valid_options_in_with_options_block?(node)

return false unless node.parent

return true if contain_valid_options_in_with_options_block?(
node.parent.parent
)
return true if contain_valid_options_in_with_options_block?(node.parent.parent)
end

false
Expand Down
13 changes: 4 additions & 9 deletions lib/rubocop/cop/rails/http_positional_arguments.rb
Expand Up @@ -22,11 +22,8 @@ class HttpPositionalArguments < Base
extend AutoCorrector
extend TargetRailsVersion

MSG = 'Use keyword arguments instead of ' \
'positional arguments for http call: `%<verb>s`.'
KEYWORD_ARGS = %i[
method params session body flash xhr as headers env to
].freeze
MSG = 'Use keyword arguments instead of positional arguments for http call: `%<verb>s`.'
KEYWORD_ARGS = %i[method params session body flash xhr as headers env to].freeze
ROUTING_METHODS = %i[draw routes].freeze
RESTRICT_ON_SEND = %i[get post put patch delete head].freeze

Expand Down Expand Up @@ -75,8 +72,7 @@ def needs_conversion?(data)
return false if kwsplat_hash?(data)

data.each_pair.none? do |pair|
special_keyword_arg?(pair.key) ||
(format_arg?(pair.key) && data.pairs.one?)
special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
end
end

Expand All @@ -98,8 +94,7 @@ def convert_hash_data(data, type)
return '' if data.hash_type? && data.empty?

hash_data = if data.hash_type?
format('{ %<data>s }',
data: data.pairs.map(&:source).join(', '))
format('{ %<data>s }', data: data.pairs.map(&:source).join(', '))
else
# user supplies an object,
# no need to surround with braces
Expand Down

0 comments on commit 1e3f498

Please sign in to comment.