From 1e3f49823ac7841e2de29c4adf3c218217dd82d4 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 30 Jul 2022 15:41:28 +0900 Subject: [PATCH] Enable `Layout/RedundantLineBreak` cop Follow up https://github.com/rubocop/rubocop/pull/9678 and https://github.com/rubocop/rubocop-rails/pull/752#discussion_r932798003. --- .rubocop.yml | 3 + Rakefile | 10 +-- lib/rubocop/cop/mixin/active_record_helper.rb | 5 +- .../mixin/active_record_migrations_helper.rb | 4 +- lib/rubocop/cop/mixin/index_method.rb | 18 +--- .../cop/rails/active_record_aliases.rb | 5 +- .../cop/rails/active_record_override.rb | 7 +- lib/rubocop/cop/rails/add_column_index.rb | 5 +- lib/rubocop/cop/rails/blank.rb | 3 +- lib/rubocop/cop/rails/bulk_change_table.rb | 26 ++---- lib/rubocop/cop/rails/content_tag.rb | 4 +- lib/rubocop/cop/rails/date.rb | 13 +-- lib/rubocop/cop/rails/delegate.rb | 7 +- lib/rubocop/cop/rails/dynamic_find_by.rb | 6 +- lib/rubocop/cop/rails/enum_uniqueness.rb | 7 +- .../cop/rails/environment_comparison.rb | 3 +- lib/rubocop/cop/rails/file_path.rb | 6 +- lib/rubocop/cop/rails/freeze_time.rb | 7 +- .../rails/has_many_or_has_one_dependent.rb | 4 +- .../cop/rails/http_positional_arguments.rb | 13 +-- lib/rubocop/cop/rails/http_status.rb | 15 ++-- .../ignored_skip_action_filter_option.rb | 13 +-- lib/rubocop/cop/rails/inverse_of.rb | 9 +- .../rails/lexically_scoped_action_filter.rb | 8 +- lib/rubocop/cop/rails/link_to_blank.rb | 5 +- lib/rubocop/cop/rails/output.rb | 7 +- .../cop/rails/pluralization_grammar.rb | 3 +- lib/rubocop/cop/rails/presence.rb | 4 +- lib/rubocop/cop/rails/present.rb | 9 +- lib/rubocop/cop/rails/redundant_allow_nil.rb | 6 +- lib/rubocop/cop/rails/refute_methods.rb | 6 +- .../cop/rails/relative_date_constant.rb | 7 +- lib/rubocop/cop/rails/request_referer.rb | 3 +- lib/rubocop/cop/rails/reversible_migration.rb | 41 ++------- .../reversible_migration_method_definition.rb | 3 +- .../cop/rails/root_pathname_methods.rb | 23 +---- .../cop/rails/safe_navigation_with_blank.rb | 4 +- lib/rubocop/cop/rails/save_bang.rb | 32 +++---- lib/rubocop/cop/rails/short_i18n.rb | 5 +- .../cop/rails/skips_model_validations.rb | 3 +- .../cop/rails/squished_sql_heredocs.rb | 6 +- lib/rubocop/cop/rails/time_zone.rb | 27 ++---- lib/rubocop/cop/rails/uniq_before_pluck.rb | 9 +- .../rails/unique_validation_without_index.rb | 4 +- lib/rubocop/cop/rails/unknown_env.rb | 6 +- lib/rubocop/cop/rails/validation.rb | 16 +--- rubocop-rails.gemspec | 3 +- spec/project_spec.rb | 17 ++-- spec/rubocop/cop/rails/blank_spec.rb | 20 ++--- .../cop/rails/bulk_change_table_spec.rb | 39 +++------ .../create_table_with_timestamps_spec.rb | 9 +- spec/rubocop/cop/rails/delegate_spec.rb | 3 +- spec/rubocop/cop/rails/file_path_spec.rb | 12 +-- .../has_many_or_has_one_dependent_spec.rb | 9 +- spec/rubocop/cop/rails/inverse_of_spec.rb | 24 ++--- spec/rubocop/cop/rails/link_to_blank_spec.rb | 3 +- spec/rubocop/cop/rails/presence_spec.rb | 39 +++------ spec/rubocop/cop/rails/present_spec.rb | 16 +--- .../cop/rails/rake_environment_spec.rb | 15 ++-- ...redundant_receiver_in_with_options_spec.rb | 12 +-- spec/rubocop/cop/rails/refute_methods_spec.rb | 24 ++--- .../rubocop/cop/rails/safe_navigation_spec.rb | 87 +++++++------------ spec/rubocop/cop/rails/scope_args_spec.rb | 8 +- spec/rubocop/cop/rails/validation_spec.rb | 15 ++-- spec/rubocop/rails/schema_loader_spec.rb | 3 +- spec/support/schema_loader.rb | 3 +- tasks/cut_release.rake | 11 +-- 67 files changed, 228 insertions(+), 574 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d1a1e2c6b2..73a4404a29 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -63,6 +63,9 @@ Style/FormatStringToken: Exclude: - spec/**/* +Layout/RedundantLineBreak: + Enabled: true + Layout/HashAlignment: EnforcedHashRocketStyle: - key diff --git a/Rakefile b/Rakefile index 1958062847..6067b454e2 100644 --- a/Rakefile +++ b/Rakefile @@ -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| @@ -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 diff --git a/lib/rubocop/cop/mixin/active_record_helper.rb b/lib/rubocop/cop/mixin/active_record_helper.rb index 3a9b1ea4b3..4e776f90bd 100644 --- a/lib/rubocop/cop/mixin/active_record_helper.rb +++ b/lib/rubocop/cop/mixin/active_record_helper.rb @@ -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. diff --git a/lib/rubocop/cop/mixin/active_record_migrations_helper.rb b/lib/rubocop/cop/mixin/active_record_migrations_helper.rb index 0934d2693f..0364f2b99d 100644 --- a/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +++ b/lib/rubocop/cop/mixin/active_record_migrations_helper.rb @@ -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 diff --git a/lib/rubocop/cop/mixin/index_method.rb b/lib/rubocop/cop/mixin/index_method.rb index 952ffcf862..bf864ad554 100644 --- a/lib/rubocop/cop/mixin/index_method.rb +++ b/lib/rubocop/cop/mixin/index_method.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/rubocop/cop/rails/active_record_aliases.rb b/lib/rubocop/cop/rails/active_record_aliases.rb index d48e184723..6f81e3b6d7 100644 --- a/lib/rubocop/cop/rails/active_record_aliases.rb +++ b/lib/rubocop/cop/rails/active_record_aliases.rb @@ -21,10 +21,7 @@ class ActiveRecordAliases < Base MSG = 'Use `%s` instead of `%s`.' - ALIASES = { - update_attributes: :update, - update_attributes!: :update! - }.freeze + ALIASES = { update_attributes: :update, update_attributes!: :update! }.freeze RESTRICT_ON_SEND = ALIASES.keys.freeze diff --git a/lib/rubocop/cop/rails/active_record_override.rb b/lib/rubocop/cop/rails/active_record_override.rb index 16fe42e82d..695643fb57 100644 --- a/lib/rubocop/cop/rails/active_record_override.rb +++ b/lib/rubocop/cop/rails/active_record_override.rb @@ -25,12 +25,9 @@ module Rails # end # class ActiveRecordOverride < Base - MSG = - 'Use %s callbacks instead of overriding the Active Record ' \ - 'method `%s`.' + MSG = 'Use %s callbacks instead of overriding the Active Record method `%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) diff --git a/lib/rubocop/cop/rails/add_column_index.rb b/lib/rubocop/cop/rails/add_column_index.rb index 384a92c70c..96cf6174d1 100644 --- a/lib/rubocop/cop/rails/add_column_index.rb +++ b/lib/rubocop/cop/rails/add_column_index.rb @@ -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 diff --git a/lib/rubocop/cop/rails/blank.rb b/lib/rubocop/cop/rails/blank.rb index d5a0a8fa2c..1c0825a790 100644 --- a/lib/rubocop/cop/rails/blank.rb +++ b/lib/rubocop/cop/rails/blank.rb @@ -63,8 +63,7 @@ class Blank < Base MSG_NIL_OR_EMPTY = 'Use `%s` instead of `%s`.' MSG_NOT_PRESENT = 'Use `%s` instead of `%s`.' - MSG_UNLESS_PRESENT = 'Use `if %s` instead of ' \ - '`%s`.' + MSG_UNLESS_PRESENT = 'Use `if %s` instead of `%s`.' RESTRICT_ON_SEND = %i[!].freeze # `(send nil $_)` is not actually a valid match for an offense. Nodes diff --git a/lib/rubocop/cop/rails/bulk_change_table.rb b/lib/rubocop/cop/rails/bulk_change_table.rb index a9bfb2c9e5..a97f0485a3 100644 --- a/lib/rubocop/cop/rails/bulk_change_table.rb +++ b/lib/rubocop/cop/rails/bulk_change_table.rb @@ -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? @@ -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 @@ -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 diff --git a/lib/rubocop/cop/rails/content_tag.rb b/lib/rubocop/cop/rails/content_tag.rb index 2c36621d1c..9cc8dc9d45 100644 --- a/lib/rubocop/cop/rails/content_tag.rb +++ b/lib/rubocop/cop/rails/content_tag.rb @@ -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) diff --git a/lib/rubocop/cop/rails/date.rb b/lib/rubocop/cop/rails/date.rb index 1515387925..e9de8d44e0 100644 --- a/lib/rubocop/cop/rails/date.rb +++ b/lib/rubocop/cop/rails/date.rb @@ -53,22 +53,17 @@ module Rails class Date < Base include ConfigurableEnforcedStyle - MSG = 'Do not use `Date.%s` without zone. Use ' \ - '`Time.zone.%s` instead.' + MSG = 'Do not use `Date.%s` without zone. Use `Time.zone.%s` instead.' - MSG_SEND = 'Do not use `%s` on Date objects, because they ' \ - 'know nothing about the time zone in use.' + MSG_SEND = 'Do not use `%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 = '`%s` is deprecated. ' \ - 'Use `%s` instead.' + DEPRECATED_MSG = '`%s` is deprecated. Use `%s` instead.' def on_const(node) mod, klass = *node.children diff --git a/lib/rubocop/cop/rails/delegate.rb b/lib/rubocop/cop/rails/delegate.rb index 7bc8f27c08..2f1c80662b 100644 --- a/lib/rubocop/cop/rails/delegate.rb +++ b/lib/rubocop/cop/rails/delegate.rb @@ -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? diff --git a/lib/rubocop/cop/rails/dynamic_find_by.rb b/lib/rubocop/cop/rails/dynamic_find_by.rb index b3a8750353..654586e947 100644 --- a/lib/rubocop/cop/rails/dynamic_find_by.rb +++ b/lib/rubocop/cop/rails/dynamic_find_by.rb @@ -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) @@ -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) diff --git a/lib/rubocop/cop/rails/enum_uniqueness.rb b/lib/rubocop/cop/rails/enum_uniqueness.rb index e01ff831f8..dfb49502d1 100644 --- a/lib/rubocop/cop/rails/enum_uniqueness.rb +++ b/lib/rubocop/cop/rails/enum_uniqueness.rb @@ -20,8 +20,7 @@ module Rails class EnumUniqueness < Base include Duplication - MSG = 'Duplicate value `%s` found in `%s` ' \ - 'enum declaration.' + MSG = 'Duplicate value `%s` found in `%s` enum declaration.' RESTRICT_ON_SEND = %i[enum].freeze def_node_matcher :enum?, <<~PATTERN @@ -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 diff --git a/lib/rubocop/cop/rails/environment_comparison.rb b/lib/rubocop/cop/rails/environment_comparison.rb index c5a25465bf..1e1c5261aa 100644 --- a/lib/rubocop/cop/rails/environment_comparison.rb +++ b/lib/rubocop/cop/rails/environment_comparison.rb @@ -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) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index bde2094342..80b9e49a6c 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -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) @@ -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 diff --git a/lib/rubocop/cop/rails/freeze_time.rb b/lib/rubocop/cop/rails/freeze_time.rb index d0fe6ab3e6..21d2748544 100644 --- a/lib/rubocop/cop/rails/freeze_time.rb +++ b/lib/rubocop/cop/rails/freeze_time.rb @@ -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 diff --git a/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb b/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb index e8f4e69cc3..df752f52b1 100644 --- a/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +++ b/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb @@ -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 diff --git a/lib/rubocop/cop/rails/http_positional_arguments.rb b/lib/rubocop/cop/rails/http_positional_arguments.rb index bb0f214eae..361e5e5b2c 100644 --- a/lib/rubocop/cop/rails/http_positional_arguments.rb +++ b/lib/rubocop/cop/rails/http_positional_arguments.rb @@ -22,11 +22,8 @@ class HttpPositionalArguments < Base extend AutoCorrector extend TargetRailsVersion - MSG = 'Use keyword arguments instead of ' \ - 'positional arguments for http call: `%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: `%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 @@ -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 @@ -98,8 +94,7 @@ def convert_hash_data(data, type) return '' if data.hash_type? && data.empty? hash_data = if data.hash_type? - format('{ %s }', - data: data.pairs.map(&:source).join(', ')) + format('{ %s }', data: data.pairs.map(&:source).join(', ')) else # user supplies an object, # no need to surround with braces diff --git a/lib/rubocop/cop/rails/http_status.rb b/lib/rubocop/cop/rails/http_status.rb index adc4047a8c..b80498c27e 100644 --- a/lib/rubocop/cop/rails/http_status.rb +++ b/lib/rubocop/cop/rails/http_status.rb @@ -84,10 +84,8 @@ def checker_class # :nodoc: class SymbolicStyleChecker - MSG = 'Prefer `%s` over `%s` ' \ - 'to define HTTP status code.' - DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \ - 'to define HTTP status code.' + MSG = 'Prefer `%s` over `%s` to define HTTP status code.' + DEFAULT_MSG = 'Prefer `symbolic` over `numeric` to define HTTP status code.' attr_reader :node @@ -118,17 +116,14 @@ def number end def custom_http_status_code? - node.int_type? && - !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number) + node.int_type? && !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number) end end # :nodoc: class NumericStyleChecker - MSG = 'Prefer `%s` over `%s` ' \ - 'to define HTTP status code.' - DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \ - 'to define HTTP status code.' + MSG = 'Prefer `%s` over `%s` to define HTTP status code.' + DEFAULT_MSG = 'Prefer `numeric` over `symbolic` to define HTTP status code.' PERMITTED_STATUS = %i[error success missing redirect].freeze attr_reader :node diff --git a/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb b/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb index 57a2ca9847..de824f54f1 100644 --- a/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +++ b/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb @@ -42,12 +42,7 @@ class IgnoredSkipActionFilterOption < Base `%s` option will be ignored when `%s` and `%s` are used together. MSG - RESTRICT_ON_SEND = %i[ - skip_after_action - skip_around_action - skip_before_action - skip_action_callback - ].freeze + RESTRICT_ON_SEND = %i[skip_after_action skip_around_action skip_before_action skip_action_callback].freeze FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" } @@ -67,11 +62,9 @@ def on_send(node) options = options_hash(options) if if_and_only?(options) - add_offense(options[:if], - message: format(MSG, prefer: :only, ignore: :if)) + add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if)) elsif if_and_except?(options) - add_offense(options[:except], - message: format(MSG, prefer: :if, ignore: :except)) + add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except)) end end diff --git a/lib/rubocop/cop/rails/inverse_of.rb b/lib/rubocop/cop/rails/inverse_of.rb index e80351a7de..b2db3d0f1c 100644 --- a/lib/rubocop/cop/rails/inverse_of.rb +++ b/lib/rubocop/cop/rails/inverse_of.rb @@ -192,8 +192,7 @@ def on_send(node) end return if options_ignoring_inverse_of?(options) - return unless scope?(arguments) || - options_requiring_inverse_of?(options) + return unless scope?(arguments) || options_requiring_inverse_of?(options) return if options_contain_inverse_of?(options) @@ -206,8 +205,7 @@ def scope?(arguments) def options_requiring_inverse_of?(options) required = options.any? do |opt| - conditions_option?(opt) || - foreign_key_option?(opt) + conditions_option?(opt) || foreign_key_option?(opt) end return required if target_rails_version >= 5.2 @@ -227,8 +225,7 @@ def options_contain_inverse_of?(options) def with_options_arguments(recv, node) blocks = node.each_ancestor(:block).select do |block| - block.send_node.command?(:with_options) && - same_context_in_with_options?(block.arguments.first, recv) + block.send_node.command?(:with_options) && same_context_in_with_options?(block.arguments.first, recv) end blocks.flat_map { |n| n.send_node.arguments } end diff --git a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb b/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb index 3b96591f88..25cb6e2ffa 100644 --- a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +++ b/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb @@ -184,13 +184,9 @@ def array_values(node) # rubocop:disable Metrics/MethodLength # @return [String] def message(methods, parent) if methods.size == 1 - format(MSG, - action: "`#{methods[0]}` is", - type: parent.type) + format(MSG, action: "`#{methods[0]}` is", type: parent.type) else - format(MSG, - action: "`#{methods.join('`, `')}` are", - type: parent.type) + format(MSG, action: "`#{methods.join('`, `')}` are", type: parent.type) end end end diff --git a/lib/rubocop/cop/rails/link_to_blank.rb b/lib/rubocop/cop/rails/link_to_blank.rb index a0c2899f99..32811e7952 100644 --- a/lib/rubocop/cop/rails/link_to_blank.rb +++ b/lib/rubocop/cop/rails/link_to_blank.rb @@ -68,10 +68,7 @@ def autocorrect(corrector, send_node, node, option_nodes) def append_to_rel(rel_node, corrector) existing_rel = rel_node.children.last.value - str_range = rel_node.children.last.loc.expression.adjust( - begin_pos: 1, - end_pos: -1 - ) + str_range = rel_node.children.last.loc.expression.adjust(begin_pos: 1, end_pos: -1) corrector.replace(str_range, "#{existing_rel} noopener") end diff --git a/lib/rubocop/cop/rails/output.rb b/lib/rubocop/cop/rails/output.rb index 655cef658e..9f87c9a7a8 100644 --- a/lib/rubocop/cop/rails/output.rb +++ b/lib/rubocop/cop/rails/output.rb @@ -21,11 +21,8 @@ class Output < Base include RangeHelp extend AutoCorrector - MSG = 'Do not write to stdout. ' \ - "Use Rails's logger if you want to log." - RESTRICT_ON_SEND = %i[ - ap p pp pretty_print print puts binwrite syswrite write write_nonblock - ].freeze + MSG = "Do not write to stdout. Use Rails's logger if you want to log." + RESTRICT_ON_SEND = %i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze def_node_matcher :output?, <<~PATTERN (send nil? {:ap :p :pp :pretty_print :print :puts} ...) diff --git a/lib/rubocop/cop/rails/pluralization_grammar.rb b/lib/rubocop/cop/rails/pluralization_grammar.rb index 24f6f1e660..ef62d15c8c 100644 --- a/lib/rubocop/cop/rails/pluralization_grammar.rb +++ b/lib/rubocop/cop/rails/pluralization_grammar.rb @@ -94,8 +94,7 @@ def singularize(method_name) end def duration_method?(method_name) - SINGULAR_DURATION_METHODS.key?(method_name) || - PLURAL_DURATION_METHODS.key?(method_name) + SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name) end end end diff --git a/lib/rubocop/cop/rails/presence.rb b/lib/rubocop/cop/rails/presence.rb index 2fbba874c1..f9313ef75a 100644 --- a/lib/rubocop/cop/rails/presence.rb +++ b/lib/rubocop/cop/rails/presence.rb @@ -106,9 +106,7 @@ def ignore_other_node?(node) end def message(node, receiver, other) - format(MSG, - prefer: replacement(receiver, other), - current: node.source) + format(MSG, prefer: replacement(receiver, other), current: node.source) end def replacement(receiver, other) diff --git a/lib/rubocop/cop/rails/present.rb b/lib/rubocop/cop/rails/present.rb index 55b5bd923e..0eccfca32f 100644 --- a/lib/rubocop/cop/rails/present.rb +++ b/lib/rubocop/cop/rails/present.rb @@ -47,10 +47,8 @@ class Present < Base extend AutoCorrector MSG_NOT_BLANK = 'Use `%s` instead of `%s`.' - MSG_EXISTS_AND_NOT_EMPTY = 'Use `%s` instead of ' \ - '`%s`.' - MSG_UNLESS_BLANK = 'Use `if %s` instead of ' \ - '`%s`.' + MSG_EXISTS_AND_NOT_EMPTY = 'Use `%s` instead of `%s`.' + MSG_UNLESS_BLANK = 'Use `if %s` instead of `%s`.' RESTRICT_ON_SEND = %i[!].freeze def_node_matcher :exists_and_not_empty?, <<~PATTERN @@ -118,8 +116,7 @@ def on_if(node) unless_blank?(node) do |method_call, receiver| range = unless_condition(node, method_call) - msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver), - current: range.source) + msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver), current: range.source) add_offense(range, message: msg) do |corrector| autocorrect(corrector, node) end diff --git a/lib/rubocop/cop/rails/redundant_allow_nil.rb b/lib/rubocop/cop/rails/redundant_allow_nil.rb index 9fb17bdf73..b1dccd1335 100644 --- a/lib/rubocop/cop/rails/redundant_allow_nil.rb +++ b/lib/rubocop/cop/rails/redundant_allow_nil.rb @@ -30,11 +30,9 @@ class RedundantAllowNil < Base include RangeHelp extend AutoCorrector - MSG_SAME = - '`allow_nil` is redundant when `allow_blank` has the same value.' + MSG_SAME = '`allow_nil` is redundant when `allow_blank` has the same value.' - MSG_ALLOW_NIL_FALSE = - '`allow_nil: false` is redundant when `allow_blank` is true.' + MSG_ALLOW_NIL_FALSE = '`allow_nil: false` is redundant when `allow_blank` is true.' RESTRICT_ON_SEND = %i[validates].freeze diff --git a/lib/rubocop/cop/rails/refute_methods.rb b/lib/rubocop/cop/rails/refute_methods.rb index eb86dee436..e141b24ea6 100644 --- a/lib/rubocop/cop/rails/refute_methods.rb +++ b/lib/rubocop/cop/rails/refute_methods.rb @@ -81,11 +81,7 @@ def bad_method?(method_name) end def offense_message(method_name) - format( - MSG, - bad_method: method_name, - good_method: convert_good_method(method_name) - ) + format(MSG, bad_method: method_name, good_method: convert_good_method(method_name)) end def convert_good_method(bad_method) diff --git a/lib/rubocop/cop/rails/relative_date_constant.rb b/lib/rubocop/cop/rails/relative_date_constant.rb index ac57ef0350..92fd50d0b6 100644 --- a/lib/rubocop/cop/rails/relative_date_constant.rb +++ b/lib/rubocop/cop/rails/relative_date_constant.rb @@ -34,8 +34,7 @@ class RelativeDateConstant < Base include RangeHelp extend AutoCorrector - MSG = 'Do not assign `%s` to constants as it ' \ - 'will be evaluated only once.' + MSG = 'Do not assign `%s` to constants as it will be evaluated only once.' RELATIVE_DATE_METHODS = %i[since from_now after ago until before yesterday tomorrow].to_set.freeze def on_casgn(node) @@ -77,9 +76,7 @@ def autocorrect(corrector, node) return unless scope.nil? indent = ' ' * node.loc.column - new_code = ["def self.#{const_name.downcase}", - "#{indent}#{value.source}", - 'end'].join("\n#{indent}") + new_code = ["def self.#{const_name.downcase}", "#{indent}#{value.source}", 'end'].join("\n#{indent}") corrector.replace(node.source_range, new_code) end diff --git a/lib/rubocop/cop/rails/request_referer.rb b/lib/rubocop/cop/rails/request_referer.rb index 4ad0f3a237..38a68d8099 100644 --- a/lib/rubocop/cop/rails/request_referer.rb +++ b/lib/rubocop/cop/rails/request_referer.rb @@ -23,8 +23,7 @@ class RequestReferer < Base include ConfigurableEnforcedStyle extend AutoCorrector - MSG = 'Use `request.%s` instead of ' \ - '`request.%s`.' + MSG = 'Use `request.%s` instead of `request.%s`.' RESTRICT_ON_SEND = %i[referer referrer].freeze def_node_matcher :referer?, <<~PATTERN diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index ceb9940379..30f28b6584 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -240,10 +240,7 @@ def check_irreversible_schema_statement_node(node) def check_drop_table_node(node) drop_table_call(node) do unless node.parent.block_type? || node.last_argument.block_pass_type? - add_offense( - node, - message: format(MSG, action: 'drop_table(without block)') - ) + add_offense(node, message: format(MSG, action: 'drop_table(without block)')) end end end @@ -251,22 +248,12 @@ def check_drop_table_node(node) def check_reversible_hash_node(node) return if reversible_change_table_call?(node) - add_offense( - node, - message: format( - MSG, action: "#{node.method_name}(without :from and :to)" - ) - ) + add_offense(node, message: format(MSG, action: "#{node.method_name}(without :from and :to)")) end def check_remove_column_node(node) remove_column_call(node) do |args| - if args.to_a.size < 3 - add_offense( - node, - message: format(MSG, action: 'remove_column(without type)') - ) - end + add_offense(node, message: format(MSG, action: 'remove_column(without type)')) if args.to_a.size < 3 end end @@ -295,10 +282,7 @@ def check_remove_columns_node(node) unless all_hash_key?(args, :type) && target_rails_version >= 6.1 action = target_rails_version >= 6.1 ? 'remove_columns(without type)' : 'remove_columns' - add_offense( - node, - message: format(MSG, action: action) - ) + add_offense(node, message: format(MSG, action: action)) end end end @@ -306,18 +290,14 @@ def check_remove_columns_node(node) def check_remove_index_node(node) remove_index_call(node) do |args| if args.hash_type? && !all_hash_key?(args, :column) - add_offense( - node, - message: format(MSG, action: 'remove_index(without column)') - ) + add_offense(node, message: format(MSG, action: 'remove_index(without column)')) end end end def check_change_table_offense(receiver, node) method_name = node.method_name - return if receiver != node.receiver && - reversible_change_table_call?(node) + return if receiver != node.receiver && reversible_change_table_call?(node) action = if method_name == :remove target_rails_version >= 6.1 ? 't.remove (without type)' : 't.remove' @@ -325,10 +305,7 @@ def check_change_table_offense(receiver, node) "change_table(with #{method_name})" end - add_offense( - node, - message: format(MSG, action: action) - ) + add_offense(node, message: format(MSG, action: action)) end def reversible_change_table_call?(node) @@ -353,9 +330,7 @@ def within_change_method?(node) def within_reversible_or_up_only_block?(node) node.each_ancestor(:block).any? do |ancestor| - (ancestor.block_type? && - ancestor.send_node.method?(:reversible)) || - ancestor.send_node.method?(:up_only) + (ancestor.block_type? && ancestor.send_node.method?(:reversible)) || ancestor.send_node.method?(:up_only) end end diff --git a/lib/rubocop/cop/rails/reversible_migration_method_definition.rb b/lib/rubocop/cop/rails/reversible_migration_method_definition.rb index acc506cadf..fbf2b1d87d 100644 --- a/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +++ b/lib/rubocop/cop/rails/reversible_migration_method_definition.rb @@ -45,8 +45,7 @@ module Rails class ReversibleMigrationMethodDefinition < Base include MigrationsHelper - MSG = 'Migrations must contain either a `change` method, or ' \ - 'both an `up` and a `down` method.' + MSG = 'Migrations must contain either a `change` method, or both an `up` and a `down` method.' def_node_matcher :change_method?, <<~PATTERN `(def :change (args) _) diff --git a/lib/rubocop/cop/rails/root_pathname_methods.rb b/lib/rubocop/cop/rails/root_pathname_methods.rb index 1ce24bb0e2..5509d5eefd 100644 --- a/lib/rubocop/cop/rails/root_pathname_methods.rb +++ b/lib/rubocop/cop/rails/root_pathname_methods.rb @@ -33,19 +33,7 @@ class RootPathnameMethods < Base MSG = '`%s` is a `Pathname` so you can just append `#%s`.' - DIR_METHODS = %i[ - children - delete - each_child - empty? - entries - exist? - glob - mkdir - open - rmdir - unlink - ].to_set.freeze + DIR_METHODS = %i[children delete each_child empty? entries exist? glob mkdir open rmdir unlink].to_set.freeze FILE_METHODS = %i[ atime @@ -137,14 +125,7 @@ class RootPathnameMethods < Base zero? ].to_set.freeze - FILE_UTILS_METHODS = %i[ - chmod - chown - mkdir - mkpath - rmdir - rmtree - ].to_set.freeze + FILE_UTILS_METHODS = %i[chmod chown mkdir mkpath rmdir rmtree].to_set.freeze RESTRICT_ON_SEND = (DIR_METHODS + FILE_METHODS + FILE_TEST_METHODS + FILE_UTILS_METHODS).to_set.freeze diff --git a/lib/rubocop/cop/rails/safe_navigation_with_blank.rb b/lib/rubocop/cop/rails/safe_navigation_with_blank.rb index 37cb73474e..c19cd8180e 100644 --- a/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +++ b/lib/rubocop/cop/rails/safe_navigation_with_blank.rb @@ -31,9 +31,7 @@ module Rails class SafeNavigationWithBlank < Base extend AutoCorrector - MSG = - 'Avoid calling `blank?` with the safe navigation operator ' \ - 'in conditionals.' + MSG = 'Avoid calling `blank?` with the safe navigation operator in conditionals.' def_node_matcher :safe_navigation_blank_in_conditional?, <<~PATTERN (if $(csend ... :blank?) ...) diff --git a/lib/rubocop/cop/rails/save_bang.rb b/lib/rubocop/cop/rails/save_bang.rb index da2b0cc7bd..cd0f6288e7 100644 --- a/lib/rubocop/cop/rails/save_bang.rb +++ b/lib/rubocop/cop/rails/save_bang.rb @@ -121,18 +121,12 @@ class SaveBang < Base include NegativeConditional extend AutoCorrector - MSG = 'Use `%s` instead of `%s` if the return ' \ - 'value is not checked.' - CREATE_MSG = (MSG + - ' Or check `persisted?` on model returned from ' \ - '`%s`.').freeze - CREATE_CONDITIONAL_MSG = '`%s` returns a model which is ' \ - 'always truthy.' - - CREATE_PERSIST_METHODS = %i[create create_or_find_by - first_or_create find_or_create_by].freeze - MODIFY_PERSIST_METHODS = %i[save - update update_attributes destroy].freeze + MSG = 'Use `%s` instead of `%s` if the return value is not checked.' + CREATE_MSG = "#{MSG} Or check `persisted?` on model returned from `%s`." + CREATE_CONDITIONAL_MSG = '`%s` returns a model which is always truthy.' + + CREATE_PERSIST_METHODS = %i[create create_or_find_by first_or_create find_or_create_by].freeze + MODIFY_PERSIST_METHODS = %i[save update update_attributes destroy].freeze RESTRICT_ON_SEND = (CREATE_PERSIST_METHODS + MODIFY_PERSIST_METHODS).freeze def self.joining_forces @@ -244,8 +238,7 @@ def in_condition_or_compound_boolean?(node) parent = node.parent return false unless parent - operator_or_single_negative?(parent) || - (conditional?(parent) && node == parent.condition) + operator_or_single_negative?(parent) || (conditional?(parent) && node == parent.condition) end def operator_or_single_negative?(node) @@ -294,9 +287,7 @@ def receiver_chain_matches?(node, allowed_receiver) # NameSpace::Const != ::Const # Const != NameSpace::Const def const_matches?(const, allowed_const) - parts = allowed_const.split('::').reverse.zip( - const.split('::').reverse - ) + parts = allowed_const.split('::').reverse.zip(const.split('::').reverse) parts.all? do |(allowed_part, const_part)| allowed_part == const_part.to_s end @@ -335,9 +326,7 @@ def return_value_assigned?(node) end def persist_method?(node, methods = RESTRICT_ON_SEND) - methods.include?(node.method_name) && - expected_signature?(node) && - !allowed_receiver?(node) + methods.include?(node.method_name) && expected_signature?(node) && !allowed_receiver?(node) end # Check argument signature as no arguments or one hash @@ -345,8 +334,7 @@ def expected_signature?(node) !node.arguments? || (node.arguments.one? && node.method_name != :destroy && - (node.first_argument.hash_type? || - !node.first_argument.literal?)) + (node.first_argument.hash_type? || !node.first_argument.literal?)) end end end diff --git a/lib/rubocop/cop/rails/short_i18n.rb b/lib/rubocop/cop/rails/short_i18n.rb index 82152a1580..35e02faf2d 100644 --- a/lib/rubocop/cop/rails/short_i18n.rb +++ b/lib/rubocop/cop/rails/short_i18n.rb @@ -44,10 +44,7 @@ class ShortI18n < Base MSG = 'Use `%s` instead of `%s`.' - PREFERRED_METHODS = { - translate: :t, - localize: :l - }.freeze + PREFERRED_METHODS = { translate: :t, localize: :l }.freeze RESTRICT_ON_SEND = PREFERRED_METHODS.keys.freeze diff --git a/lib/rubocop/cop/rails/skips_model_validations.rb b/lib/rubocop/cop/rails/skips_model_validations.rb index 16266c2d86..cbc977eb95 100644 --- a/lib/rubocop/cop/rails/skips_model_validations.rb +++ b/lib/rubocop/cop/rails/skips_model_validations.rb @@ -93,8 +93,7 @@ def message(node) end def allowed_method?(node) - METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) && - !node.arguments? + METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) && !node.arguments? end def forbidden_methods diff --git a/lib/rubocop/cop/rails/squished_sql_heredocs.rb b/lib/rubocop/cop/rails/squished_sql_heredocs.rb index ec36e3be85..55f5654baf 100644 --- a/lib/rubocop/cop/rails/squished_sql_heredocs.rb +++ b/lib/rubocop/cop/rails/squished_sql_heredocs.rb @@ -72,11 +72,7 @@ def using_squish?(node) end def message(node) - format( - MSG, - expect: "#{node.source}#{SQUISH}", - current: node.source - ) + format(MSG, expect: "#{node.source}#{SQUISH}", current: node.source) end end end diff --git a/lib/rubocop/cop/rails/time_zone.rb b/lib/rubocop/cop/rails/time_zone.rb index 5cf55ec422..5955fba982 100644 --- a/lib/rubocop/cop/rails/time_zone.rb +++ b/lib/rubocop/cop/rails/time_zone.rb @@ -43,21 +43,17 @@ class TimeZone < Base include ConfigurableEnforcedStyle extend AutoCorrector - MSG = 'Do not use `%s` without zone. Use `%s` ' \ - 'instead.' + MSG = 'Do not use `%s` without zone. Use `%s` instead.' - MSG_ACCEPTABLE = 'Do not use `%s` without zone. ' \ - 'Use one of %s instead.' + MSG_ACCEPTABLE = 'Do not use `%s` without zone. Use one of %s instead.' - MSG_LOCALTIME = 'Do not use `Time.localtime` without ' \ - 'offset or zone.' + MSG_LOCALTIME = 'Do not use `Time.localtime` without offset or zone.' GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze DANGEROUS_METHODS = %i[now local new parse at].freeze - ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 - jisx0301 rfc3339 httpdate to_i to_f].freeze + ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze TIMEZONE_SPECIFIER = /[A-z]/.freeze @@ -100,13 +96,11 @@ def autocorrect_time_new(node, corrector) # remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone` def remove_redundant_in_time_zone(corrector, node) time_methods_called = extract_method_chain(node) - return unless time_methods_called.include?(:in_time_zone) || - time_methods_called.include?(:zone) + return unless time_methods_called.include?(:in_time_zone) || time_methods_called.include?(:zone) while node&.send_type? if node.children.last == :in_time_zone - in_time_zone_with_dot = - node.loc.selector.adjust(begin_pos: -1) + in_time_zone_with_dot = node.loc.selector.adjust(begin_pos: -1) corrector.remove(in_time_zone_with_dot) end node = node.parent @@ -144,9 +138,7 @@ def build_message(klass, method_name, node) ) else safe_method_name = safe_method(method_name, node) - format(MSG, - current: "#{klass}.#{method_name}", - prefer: "Time.zone.#{safe_method_name}") + format(MSG, current: "#{klass}.#{method_name}", prefer: "Time.zone.#{safe_method_name}") end end @@ -227,10 +219,7 @@ def good_methods end def acceptable_methods(klass, method_name, node) - acceptable = [ - "`Time.zone.#{safe_method(method_name, node)}`", - "`#{klass}.current`" - ] + acceptable = ["`Time.zone.#{safe_method(method_name, node)}`", "`#{klass}.current`"] ACCEPTED_METHODS.each do |am| acceptable << "`#{klass}.#{method_name}.#{am}`" diff --git a/lib/rubocop/cop/rails/uniq_before_pluck.rb b/lib/rubocop/cop/rails/uniq_before_pluck.rb index bcbe1477e9..91caf5506d 100644 --- a/lib/rubocop/cop/rails/uniq_before_pluck.rb +++ b/lib/rubocop/cop/rails/uniq_before_pluck.rb @@ -54,11 +54,9 @@ class UniqBeforePluck < Base NEWLINE = "\n" PATTERN = '[!^block (send (send %s :pluck ...) :uniq ...)]' - def_node_matcher :conservative_node_match, - format(PATTERN, type: 'const') + def_node_matcher :conservative_node_match, format(PATTERN, type: 'const') - def_node_matcher :aggressive_node_match, - format(PATTERN, type: '_') + def_node_matcher :aggressive_node_match, format(PATTERN, type: '_') def on_send(node) uniq = if style == :conservative @@ -80,8 +78,7 @@ def on_send(node) private def dot_method_with_whitespace(method, node) - range_between(dot_method_begin_pos(method, node), - node.loc.selector.end_pos) + range_between(dot_method_begin_pos(method, node), node.loc.selector.end_pos) end def dot_method_begin_pos(method, node) diff --git a/lib/rubocop/cop/rails/unique_validation_without_index.rb b/lib/rubocop/cop/rails/unique_validation_without_index.rb index f2aa63db34..6c7cc3d222 100644 --- a/lib/rubocop/cop/rails/unique_validation_without_index.rb +++ b/lib/rubocop/cop/rails/unique_validation_without_index.rb @@ -59,9 +59,7 @@ def with_index?(klass, table, names) add_indices = schema.add_indices_by(table_name: table_name(klass)) (table.indices + add_indices).any? do |index| - index.unique && - (index.columns.to_set == names || - include_column_names_in_expression_index?(index, names)) + index.unique && (index.columns.to_set == names || include_column_names_in_expression_index?(index, names)) end end diff --git a/lib/rubocop/cop/rails/unknown_env.rb b/lib/rubocop/cop/rails/unknown_env.rb index 42020b4155..c3d8d467eb 100644 --- a/lib/rubocop/cop/rails/unknown_env.rb +++ b/lib/rubocop/cop/rails/unknown_env.rb @@ -19,8 +19,7 @@ module Rails # Rails.env == 'production' class UnknownEnv < Base MSG = 'Unknown environment `%s`.' - MSG_SIMILAR = 'Unknown environment `%s`. ' \ - 'Did you mean `%s`?' + MSG_SIMILAR = 'Unknown environment `%s`. Did you mean `%s`?' def_node_matcher :rails_env?, <<~PATTERN (send @@ -79,8 +78,7 @@ def message(name) def unknown_env_predicate?(name) name = name.to_s - name.end_with?('?') && - !environments.include?(name[0..-2]) + name.end_with?('?') && !environments.include?(name[0..-2]) end def unknown_env_name?(name) diff --git a/lib/rubocop/cop/rails/validation.rb b/lib/rubocop/cop/rails/validation.rb index 29f5f33629..2958e14552 100644 --- a/lib/rubocop/cop/rails/validation.rb +++ b/lib/rubocop/cop/rails/validation.rb @@ -35,8 +35,7 @@ module Rails class Validation < Base extend AutoCorrector - MSG = 'Prefer the new style validations `%s` over ' \ - '`%s`.' + MSG = 'Prefer the new style validations `%s` over `%s`.' TYPES = %w[ acceptance @@ -62,8 +61,7 @@ def on_send(node) add_offense(range, message: message(node)) do |corrector| last_argument = node.arguments.last - return if !last_argument.literal? && !last_argument.splat_type? && - !frozen_array_argument?(last_argument) + return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument) corrector.replace(range, 'validates') correct_validate_type(corrector, node) @@ -104,10 +102,7 @@ def correct_validate_type(corrector, node) end def correct_validate_type_for_hash(corrector, node, arguments) - corrector.replace( - arguments.loc.expression, - "#{validate_type(node)}: #{braced_options(arguments)}" - ) + corrector.replace(arguments.loc.expression, "#{validate_type(node)}: #{braced_options(arguments)}") end def correct_validate_type_for_array(corrector, node, arguments, loc) @@ -121,10 +116,7 @@ def correct_validate_type_for_array(corrector, node, arguments, loc) end end - corrector.replace( - loc.expression, - "#{attributes.join(', ')}, #{validate_type(node)}: true" - ) + corrector.replace(loc.expression, "#{attributes.join(', ')}, #{validate_type(node)}: true") end def validate_type(node) diff --git a/rubocop-rails.gemspec b/rubocop-rails.gemspec index 96c9a49885..1757750915 100644 --- a/rubocop-rails.gemspec +++ b/rubocop-rails.gemspec @@ -16,8 +16,7 @@ Gem::Specification.new do |s| DESCRIPTION s.email = 'rubocop@googlegroups.com' - s.files = `git ls-files bin config lib LICENSE.txt README.md` - .split($RS) + s.files = `git ls-files bin config lib LICENSE.txt README.md`.split($RS) s.extra_rdoc_files = ['LICENSE.txt', 'README.md'] s.homepage = 'https://github.com/rubocop/rubocop-rails' s.licenses = ['MIT'] diff --git a/spec/project_spec.rb b/spec/project_spec.rb index 4dc2f871fd..8df47b61d7 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -33,10 +33,8 @@ it 'requires a nicely formatted `VersionAdded` metadata for all cops' do cop_names.each do |name| version = config.dig(name, 'VersionAdded') - expect(version.nil?).to(be(false), - "`VersionAdded` configuration is required for `#{name}`.") - expect(version).to(match(version_regexp), - "#{version} should be format ('X.Y' or '<>') for #{name}.") + expect(version.nil?).to(be(false), "`VersionAdded` configuration is required for `#{name}`.") + expect(version).to(match(version_regexp), "#{version} should be format ('X.Y' or '<>') for #{name}.") end end @@ -46,8 +44,7 @@ version = config.dig(name, version_type) next unless version - expect(version).to(match(version_regexp), - "#{version} should be format ('X.Y' or '<>') for #{name}.") + expect(version).to(match(version_regexp), "#{version} should be format ('X.Y' or '<>') for #{name}.") end end end @@ -91,8 +88,7 @@ fname = File.expand_path('../config/default.yml', __dir__) content = File.read(fname) RuboCop::YAMLDuplicationChecker.check(content, fname) do |key1, key2| - raise "#{fname} has duplication of #{key1.value} " \ - "on line #{key1.start_line} and line #{key2.start_line}" + raise "#{fname} has duplication of #{key1.value} on line #{key1.start_line} and line #{key2.start_line}" end end @@ -177,10 +173,7 @@ describe 'body' do let(:bodies) do entries.map do |entry| - entry - .gsub(/`[^`]+`/, '``') - .sub(/^\*\s*(?:\[.+?\):\s*)?/, '') - .sub(/\s*\([^)]+\)$/, '') + entry.gsub(/`[^`]+`/, '``').sub(/^\*\s*(?:\[.+?\):\s*)?/, '').sub(/\s*\([^)]+\)$/, '') end end diff --git a/spec/rubocop/cop/rails/blank_spec.rb b/spec/rubocop/cop/rails/blank_spec.rb index cd4b0134ad..6b53515624 100644 --- a/spec/rubocop/cop/rails/blank_spec.rb +++ b/spec/rubocop/cop/rails/blank_spec.rb @@ -49,18 +49,14 @@ it_behaves_like 'offense', 'foo.nil? || foo.empty?', 'foo.blank?', 'Use `foo.blank?` instead of `foo.nil? || foo.empty?`.' - it_behaves_like 'offense', 'nil? || empty?', - 'blank?', - 'Use `blank?` instead of `nil? || empty?`.' + it_behaves_like 'offense', 'nil? || empty?', 'blank?', 'Use `blank?` instead of `nil? || empty?`.' it_behaves_like 'offense', 'foo == nil || foo.empty?', 'foo.blank?', 'Use `foo.blank?` instead of `foo == nil || foo.empty?`.' it_behaves_like 'offense', 'nil == foo || foo.empty?', 'foo.blank?', 'Use `foo.blank?` instead of `nil == foo || foo.empty?`.' - it_behaves_like 'offense', '!foo || foo.empty?', - 'foo.blank?', - 'Use `foo.blank?` instead of `!foo || foo.empty?`.' + it_behaves_like 'offense', '!foo || foo.empty?', 'foo.blank?', 'Use `foo.blank?` instead of `!foo || foo.empty?`.' it_behaves_like 'offense', 'foo.nil? || !!foo.empty?', 'foo.blank?', @@ -116,15 +112,9 @@ { 'NotPresent' => true } end - it_behaves_like 'offense', '!foo.present?', - 'foo.blank?', - 'Use `foo.blank?` instead of `!foo.present?`.' - it_behaves_like 'offense', 'not foo.present?', - 'foo.blank?', - 'Use `foo.blank?` instead of `not foo.present?`.' - it_behaves_like 'offense', '!present?', - 'blank?', - 'Use `blank?` instead of `!present?`.' + it_behaves_like 'offense', '!foo.present?', 'foo.blank?', 'Use `foo.blank?` instead of `!foo.present?`.' + it_behaves_like 'offense', 'not foo.present?', 'foo.blank?', 'Use `foo.blank?` instead of `not foo.present?`.' + it_behaves_like 'offense', '!present?', 'blank?', 'Use `blank?` instead of `!present?`.' it 'accepts !present? if its in the body of a `blank?` method' do expect_no_offenses('def blank?; !present? end') diff --git a/spec/rubocop/cop/rails/bulk_change_table_spec.rb b/spec/rubocop/cop/rails/bulk_change_table_spec.rb index 321c1f5784..3941884c2b 100644 --- a/spec/rubocop/cop/rails/bulk_change_table_spec.rb +++ b/spec/rubocop/cop/rails/bulk_change_table_spec.rb @@ -26,8 +26,7 @@ def change end shared_examples 'no offense' do - it 'does not register an offense' \ - 'when including combinable transformations' do + it 'does not register an offensewhen including combinable transformations' do expect_no_offenses(<<~RUBY) def change change_table :users do |t| @@ -38,8 +37,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including combinable alter methods' do + it 'does not register an offensewhen including combinable alter methods' do expect_no_offenses(<<~RUBY) def change add_column :users, :name, :string, null: false @@ -74,8 +72,7 @@ def change end shared_examples 'no offense for mysql' do - it 'does not register an offense' \ - 'when including combinable transformations' do + it 'does not register an offensewhen including combinable transformations' do expect_no_offenses(<<~RUBY) def change change_table :users do |t| @@ -86,8 +83,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including combinable alter methods' do + it 'does not register an offensewhen including combinable alter methods' do expect_no_offenses(<<~RUBY) def change remove_index :users, :name @@ -131,8 +127,7 @@ def change end shared_examples 'no offense for postgresql' do - it 'does not register an offense' \ - 'when including combinable transformations' do + it 'does not register an offensewhen including combinable transformations' do expect_no_offenses(<<~RUBY) def change change_table :users do |t| @@ -143,8 +138,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including combinable alter methods' do + it 'does not register an offensewhen including combinable alter methods' do expect_no_offenses(<<~RUBY) def change change_column_default :users, :name, false @@ -199,8 +193,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including combinable transformations with `bulk: true`' do + it 'does not register an offensewhen including combinable transformations with `bulk: true`' do expect_no_offenses(<<~RUBY) def change change_table :users, bulk: true do |t| @@ -211,8 +204,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including combinable transformations with `bulk: false`' do + it 'does not register an offensewhen including combinable transformations with `bulk: false`' do expect_no_offenses(<<~RUBY) def change change_table :users, bulk: false do |t| @@ -223,8 +215,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including a combinable transformation' do + it 'does not register an offensewhen including a combinable transformation' do expect_no_offenses(<<~RUBY) def change change_table :users do |t| @@ -261,8 +252,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including transformations with block' do + it 'does not register an offensewhen including transformations with block' do expect_no_offenses(<<~RUBY) def change reversible do |dir| @@ -282,8 +272,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when the target of the alter method is another table' do + it 'does not register an offensewhen the target of the alter method is another table' do expect_no_offenses(<<~RUBY) def change add_reference :users, :team @@ -293,8 +282,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including non-combinable alter method between' do + it 'does not register an offensewhen including non-combinable alter method between' do expect_no_offenses(<<~RUBY) def change add_column :users, :name, :string, null: false @@ -316,8 +304,7 @@ def change RUBY end - it 'does not register an offense' \ - 'when including a combinable alter method' do + it 'does not register an offensewhen including a combinable alter method' do expect_no_offenses(<<~RUBY) def change add_reference :users, :team diff --git a/spec/rubocop/cop/rails/create_table_with_timestamps_spec.rb b/spec/rubocop/cop/rails/create_table_with_timestamps_spec.rb index 75dd986e31..327450b273 100644 --- a/spec/rubocop/cop/rails/create_table_with_timestamps_spec.rb +++ b/spec/rubocop/cop/rails/create_table_with_timestamps_spec.rb @@ -35,8 +35,7 @@ RUBY end - it 'registers an offense when not including timestamps' \ - 'with `to_proc` syntax' do + it 'registers an offense when not including timestampswith `to_proc` syntax' do expect_offense <<~RUBY create_table :users, &:extension_columns ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add timestamps when creating a new table. @@ -54,15 +53,13 @@ RUBY end - it 'does not register an offense when including timestamps' \ - 'with `to_proc` syntax' do + it 'does not register an offense when including timestampswith `to_proc` syntax' do expect_no_offenses <<~RUBY create_table :users, &:timestamps RUBY end - it 'does not register an offense when including timestamps' \ - 'with options and `to_proc` syntax' do + it 'does not register an offense when including timestampswith options and `to_proc` syntax' do expect_no_offenses <<~RUBY create_table :users, id: :string, limit: 42, &:timestamps RUBY diff --git a/spec/rubocop/cop/rails/delegate_spec.rb b/spec/rubocop/cop/rails/delegate_spec.rb index 5d7728aade..66bf76de7c 100644 --- a/spec/rubocop/cop/rails/delegate_spec.rb +++ b/spec/rubocop/cop/rails/delegate_spec.rb @@ -3,8 +3,7 @@ RSpec.describe RuboCop::Cop::Rails::Delegate, :config do let(:cop_config) { { 'EnforceForPrefixed' => true } } let(:config) do - merged = RuboCop::ConfigLoader - .default_configuration['Rails/Delegate'].merge(cop_config) + merged = RuboCop::ConfigLoader.default_configuration['Rails/Delegate'].merge(cop_config) RuboCop::Config.new('Rails/Delegate' => merged) end diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 24a8c74383..524ee754db 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -70,8 +70,7 @@ end end - context 'when concat Rails.root and file separator ' \ - 'using string interpolation' do + context 'when concat Rails.root and file separator using string interpolation' do it 'registers an offense' do expect_offense(<<~'RUBY') system "rm -rf #{Rails.root}/foo/bar" @@ -80,8 +79,7 @@ end end - context 'when concat Rails.root.join and extension ' \ - 'using string interpolation' do + context 'when concat Rails.root.join and extension using string interpolation' do it 'registers an offense' do expect_offense(<<~'RUBY') "#{Rails.root.join('tmp', user.id, 'icon')}.png" @@ -200,8 +198,7 @@ end end - context 'when concat Rails.root and file separator ' \ - 'using string interpolation' do + context 'when concat Rails.root and file separator using string interpolation' do it 'registers an offense' do expect_offense(<<~'RUBY') system "rm -rf #{Rails.root}/foo/bar" @@ -210,8 +207,7 @@ end end - context 'when concat Rails.root.join and extension ' \ - 'using string interpolation' do + context 'when concat Rails.root.join and extension using string interpolation' do it 'registers an offense' do expect_offense(<<~'RUBY') "#{Rails.root.join('tmp', user.id, 'icon')}.png" diff --git a/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb b/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb index 81d649924a..9f639a755a 100644 --- a/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb +++ b/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb @@ -202,8 +202,7 @@ def bar end context 'Multiple associations' do - it "doesn't register an offense for " \ - '`with_options dependent: :destroy`' do + it "doesn't register an offense for `with_options dependent: :destroy`" do expect_no_offenses(<<~RUBY) class Person < ApplicationRecord with_options dependent: :destroy do @@ -242,8 +241,7 @@ class Person < ActiveRecord::Base RUBY end - it 'registers an offense when using mix-in module that has ' \ - 'an association of Active Record' do + it 'registers an offense when using mix-in module that has an association of Active Record' do expect_offense(<<~RUBY) module Foo extend ActiveSupport::Concern @@ -256,8 +254,7 @@ module Foo RUBY end - it 'does not register an offense when using associations of ' \ - 'Active Resource' do + it 'does not register an offense when using associations of Active Resource' do expect_no_offenses(<<~RUBY) class User < ActiveResource::Base has_many :projects, class_name: 'API::Project' diff --git a/spec/rubocop/cop/rails/inverse_of_spec.rb b/spec/rubocop/cop/rails/inverse_of_spec.rb index d0e38df333..20a2959241 100644 --- a/spec/rubocop/cop/rails/inverse_of_spec.rb +++ b/spec/rubocop/cop/rails/inverse_of_spec.rb @@ -12,9 +12,7 @@ class Person end it 'does not register an offense when specifying `inverse_of: false`' do - expect_no_offenses( - 'has_many :foo, -> () { where(bar: true) }, inverse_of: false' - ) + expect_no_offenses('has_many :foo, -> () { where(bar: true) }, inverse_of: false') end it 'registers an offense when specifying `inverse_of: nil`' do @@ -32,9 +30,7 @@ class Person end it 'does not register an offense when not specifying `:inverse_of`' do - expect_no_offenses( - 'has_many :foo, -> () { where(bar: true) }' - ) + expect_no_offenses('has_many :foo, -> () { where(bar: true) }') end end end @@ -50,15 +46,11 @@ class Person end it 'does not register an offense when specifying `:inverse_of`' do - expect_no_offenses( - "has_one :foo, foreign_key: 'foo_id', inverse_of: :bar" - ) + expect_no_offenses("has_one :foo, foreign_key: 'foo_id', inverse_of: :bar") end it 'does not register an offense with `:inverse_of` as first option' do - expect_no_offenses( - "has_one :foo, inverse_of: :bar, foreign_key: 'foo_id'" - ) + expect_no_offenses("has_one :foo, inverse_of: :bar, foreign_key: 'foo_id'") end it 'registers an offense with other option and `:inverse_of` unset' do @@ -91,9 +83,7 @@ class Person end it 'does not register an offense when specifying `:inverse_of`' do - expect_no_offenses( - "has_many :foo, -> { group 'x' }, dependent: :destroy, inverse_of: :baz" - ) + expect_no_offenses("has_many :foo, -> { group 'x' }, dependent: :destroy, inverse_of: :baz") end end @@ -111,9 +101,7 @@ class Person context 'Rails >= 5.2', :rails52 do it 'does not register an offense when not specifying `:inverse_of`' do - expect_no_offenses( - 'has_many :pictures, as: :imageable' - ) + expect_no_offenses('has_many :pictures, as: :imageable') end end end diff --git a/spec/rubocop/cop/rails/link_to_blank_spec.rb b/spec/rubocop/cop/rails/link_to_blank_spec.rb index adc32a43c5..83e71c8f59 100644 --- a/spec/rubocop/cop/rails/link_to_blank_spec.rb +++ b/spec/rubocop/cop/rails/link_to_blank_spec.rb @@ -65,8 +65,7 @@ RUBY end - it 'autocorrects with a new rel when using the block syntax ' \ - 'with parenthesis' do + it 'autocorrects with a new rel when using the block syntax with parenthesis' do new_source = autocorrect_source(<<~RUBY) link_to('https://www.example.com', target: '_blank') do "Click here" diff --git a/spec/rubocop/cop/rails/presence_spec.rb b/spec/rubocop/cop/rails/presence_spec.rb index cc8f019df0..3d4d1a8a67 100644 --- a/spec/rubocop/cop/rails/presence_spec.rb +++ b/spec/rubocop/cop/rails/presence_spec.rb @@ -6,17 +6,8 @@ offenses = inspect_source(source) expect(offenses.count).to eq 1 - expect(offenses).to all( - have_attributes( - first_line: first_line, - last_line: end_line - ) - ) - expect(offenses).to all( - have_attributes( - message: "Use `#{correction}` instead of `#{source}`." - ) - ) + expect(offenses).to all(have_attributes(first_line: first_line, last_line: end_line)) + expect(offenses).to all(have_attributes(message: "Use `#{correction}` instead of `#{source}`.")) end it 'auto correct' do @@ -40,10 +31,7 @@ 'a(:bar).map(&:baz).presence', 1, 1 - it_behaves_like 'offense', - 'a.present? ? a : b[:c]', - 'a.presence || b[:c]', - 1, 1 + it_behaves_like 'offense', 'a.present? ? a : b[:c]', 'a.presence || b[:c]', 1, 1 it_behaves_like 'offense', <<~RUBY.chomp, 'a.presence', 1, 5 if a.present? @@ -76,8 +64,7 @@ .map { |num| num + 2 }.presence || b FIXED - context 'when a method argument of `else` branch ' \ - 'is enclosed in parentheses' do + context 'when a method argument of `else` branch is enclosed in parentheses' do it_behaves_like 'offense', <<~SOURCE.chomp, <<~CORRECTION.chomp, 1, 5 if value.present? value @@ -89,8 +76,7 @@ CORRECTION end - context 'when a method argument of `else` branch ' \ - 'is not enclosed in parentheses' do + context 'when a method argument of `else` branch is not enclosed in parentheses' do it_behaves_like 'offense', <<~SOURCE.chomp, <<~CORRECTION.chomp, 1, 5 if value.present? value @@ -102,8 +88,7 @@ CORRECTION end - context 'when multiple method arguments of `else` branch ' \ - 'is not enclosed in parentheses' do + context 'when multiple method arguments of `else` branch is not enclosed in parentheses' do it_behaves_like 'offense', <<~SOURCE.chomp, <<~CORRECTION.chomp, 1, 5 if value.present? value @@ -115,8 +100,7 @@ CORRECTION end - context 'when a method argument with a receiver of `else` branch ' \ - 'is not enclosed in parentheses' do + context 'when a method argument with a receiver of `else` branch is not enclosed in parentheses' do it_behaves_like 'offense', <<~SOURCE.chomp, <<~CORRECTION.chomp, 1, 5 if value.present? value @@ -134,8 +118,7 @@ RUBY end - it 'does not register an offense when the expression does not ' \ - 'return the receiver of `#present?`' do + it 'does not register an offense when the expression does not return the receiver of `#present?`' do expect_no_offenses(<<~RUBY) a.present? ? b : nil RUBY @@ -146,8 +129,7 @@ RUBY end - it 'does not register an offense when the expression does not ' \ - 'return the receiver of `#blank?`' do + it 'does not register an offense when the expression does not return the receiver of `#blank?`' do expect_no_offenses(<<~RUBY) a.blank? ? nil : b RUBY @@ -177,8 +159,7 @@ RUBY end - it 'does not register an offense when the else block has multiple ' \ - 'statements' do + it 'does not register an offense when the else block has multiple statements' do expect_no_offenses(<<~RUBY) if a.present? a diff --git a/spec/rubocop/cop/rails/present_spec.rb b/spec/rubocop/cop/rails/present_spec.rb index 2ab15acec4..a547de99a6 100644 --- a/spec/rubocop/cop/rails/present_spec.rb +++ b/spec/rubocop/cop/rails/present_spec.rb @@ -43,9 +43,7 @@ it_behaves_like 'offense', '!foo.nil? && !foo.empty?', 'foo.present?', 'Use `foo.present?` instead of `!foo.nil? && !foo.empty?`.' - it_behaves_like 'offense', '!nil? && !empty?', - 'present?', - 'Use `present?` instead of `!nil? && !empty?`.' + it_behaves_like 'offense', '!nil? && !empty?', 'present?', 'Use `present?` instead of `!nil? && !empty?`.' it_behaves_like 'offense', 'foo != nil && !foo.empty?', 'foo.present?', 'Use `foo.present?` instead of `foo != nil && !foo.empty?`.' @@ -104,16 +102,10 @@ 'UnlessBlank' => false } end - it_behaves_like 'offense', '!foo.blank?', - 'foo.present?', - 'Use `foo.present?` instead of `!foo.blank?`.' + it_behaves_like 'offense', '!foo.blank?', 'foo.present?', 'Use `foo.present?` instead of `!foo.blank?`.' - it_behaves_like 'offense', 'not foo.blank?', - 'foo.present?', - 'Use `foo.present?` instead of `not foo.blank?`.' - it_behaves_like 'offense', '!blank?', - 'present?', - 'Use `present?` instead of `!blank?`.' + it_behaves_like 'offense', 'not foo.blank?', 'foo.present?', 'Use `foo.present?` instead of `not foo.blank?`.' + it_behaves_like 'offense', '!blank?', 'present?', 'Use `present?` instead of `!blank?`.' end context 'UnlessBlank set to true' do diff --git a/spec/rubocop/cop/rails/rake_environment_spec.rb b/spec/rubocop/cop/rails/rake_environment_spec.rb index ed18eecb0a..1692a72ac3 100644 --- a/spec/rubocop/cop/rails/rake_environment_spec.rb +++ b/spec/rubocop/cop/rails/rake_environment_spec.rb @@ -40,8 +40,7 @@ RUBY end - it 'does not register an offense to task with :environment ' \ - 'but it has other dependency before it' do + it 'does not register an offense to task with :environment but it has other dependency before it' do expect_no_offenses(<<~RUBY) task foo: [:bar, `:environment`] do end @@ -62,8 +61,7 @@ RUBY end - it 'does not register an offense to task with a dependency ' \ - 'as an array literal element method call' do + it 'does not register an offense to task with a dependency as an array literal element method call' do expect_no_offenses(<<~RUBY) task foo: [:bar, dep] RUBY @@ -76,8 +74,7 @@ RUBY end - it 'does not register an offense to task with :environment ' \ - 'and other dependencies' do + it 'does not register an offense to task with :environment and other dependencies' do expect_no_offenses(<<~RUBY) task foo: [`:environment`, :bar] do end @@ -91,15 +88,13 @@ RUBY end - it 'does not register an offense to task with a dependency ' \ - 'as a method call' do + it 'does not register an offense to task with a dependency as a method call' do expect_no_offenses(<<~RUBY) task foo: dep RUBY end - it 'does not register an offense to task with dependencies ' \ - 'as a method call' do + it 'does not register an offense to task with dependencies as a method call' do expect_no_offenses(<<~RUBY) task foo: [dep, :bar] RUBY diff --git a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb index dd6495b562..377465c879 100644 --- a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb +++ b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb @@ -29,8 +29,7 @@ class Account < ApplicationRecord RUBY end - it 'does not register an offense when using implicit receiver ' \ - 'in `with_options`' do + it 'does not register an offense when using implicit receiver in `with_options`' do expect_no_offenses(<<~RUBY) class Account < ApplicationRecord with_options dependent: :destroy do @@ -43,8 +42,7 @@ class Account < ApplicationRecord RUBY end - it 'registers an offense and corrects when including multiple ' \ - 'redundant receivers in single line' do + it 'registers an offense and corrects when including multiple redundant receivers in single line' do expect_offense(<<~RUBY) with_options options: false do |merger| merger.invoke(merger.something) @@ -60,8 +58,7 @@ class Account < ApplicationRecord RUBY end - it 'does not register an offense when including method invocations ' \ - 'to different receivers' do + it 'does not register an offense when including method invocations to different receivers' do expect_no_offenses(<<~RUBY) client = ApplicationClient.new with_options options: false do |merger| @@ -70,8 +67,7 @@ class Account < ApplicationRecord RUBY end - it 'does not register an offense when including block node' \ - 'in `with_options`' do + it 'does not register an offense when including block nodein `with_options`' do expect_no_offenses(<<~RUBY) with_options options: false do |merger| merger.invoke diff --git a/spec/rubocop/cop/rails/refute_methods_spec.rb b/spec/rubocop/cop/rails/refute_methods_spec.rb index 56743014dc..1ccb78a2a2 100644 --- a/spec/rubocop/cop/rails/refute_methods_spec.rb +++ b/spec/rubocop/cop/rails/refute_methods_spec.rb @@ -15,8 +15,7 @@ context 'when EnforcedStyle is `assert_not`' do let(:enforced_style) { 'assert_not' } - it 'registers an offense and correct using `refute` ' \ - 'with a single argument' do + it 'registers an offense and correct using `refute` with a single argument' do expect_offense(<<~RUBY) refute foo ^^^^^^ Prefer `assert_not` over `refute`. @@ -27,8 +26,7 @@ RUBY end - it 'registers an offense and corrects using `refute` ' \ - 'with multiple arguments' do + it 'registers an offense and corrects using `refute` with multiple arguments' do expect_offense(<<~RUBY) refute foo, bar, baz ^^^^^^ Prefer `assert_not` over `refute`. @@ -50,15 +48,13 @@ RUBY end - it 'does not register an offense when using `assert_not` ' \ - 'with a single argument' do + it 'does not register an offense when using `assert_not` with a single argument' do expect_no_offenses(<<~RUBY) assert_not foo RUBY end - it 'does not register an offense when using `assert_not` ' \ - 'with a multiple arguments' do + it 'does not register an offense when using `assert_not` with a multiple arguments' do expect_no_offenses(<<~RUBY) assert_not foo, bar, baz RUBY @@ -68,8 +64,7 @@ context 'when EnforcedStyle is `refute`' do let(:enforced_style) { 'refute' } - it 'registers an offense and correct using `assert_not` ' \ - 'with a single argument' do + it 'registers an offense and correct using `assert_not` with a single argument' do expect_offense(<<~RUBY) assert_not foo ^^^^^^^^^^ Prefer `refute` over `assert_not`. @@ -80,8 +75,7 @@ RUBY end - it 'registers an offense and corrects using `assert_not` ' \ - 'with multiple arguments' do + it 'registers an offense and corrects using `assert_not` with multiple arguments' do expect_offense(<<~RUBY) assert_not foo, bar, baz ^^^^^^^^^^ Prefer `refute` over `assert_not`. @@ -103,15 +97,13 @@ RUBY end - it 'does not register an offense when using `refute` ' \ - 'with a single argument' do + it 'does not register an offense when using `refute` with a single argument' do expect_no_offenses(<<~RUBY) refute foo RUBY end - it 'does not register an offense when using `refute` ' \ - 'with a multiple arguments' do + it 'does not register an offense when using `refute` with a multiple arguments' do expect_no_offenses(<<~RUBY) refute foo, bar, baz RUBY diff --git a/spec/rubocop/cop/rails/safe_navigation_spec.rb b/spec/rubocop/cop/rails/safe_navigation_spec.rb index ff23db4a05..190ece7993 100644 --- a/spec/rubocop/cop/rails/safe_navigation_spec.rb +++ b/spec/rubocop/cop/rails/safe_navigation_spec.rb @@ -11,8 +11,7 @@ it "registers an offense for #{name}" do offenses = inspect_source("[1, 2].#{method}#{params}") - expect(offenses.first.message) - .to eq(format('Use safe navigation (`&.`) instead of `%s`.', method)) + expect(offenses.first.message).to eq(format('Use safe navigation (`&.`) instead of `%s`.', method)) end end @@ -31,10 +30,8 @@ context 'target_ruby_version < 2.3', :ruby22 do it_behaves_like 'accepts', 'try! with a single parameter', 'try!(:join)' - it_behaves_like 'accepts', 'try! with a multiple parameters', - 'try!(:join, ",")' - it_behaves_like 'accepts', 'try! with a block', - 'try!(:map) { |e| e.some_method }' + it_behaves_like 'accepts', 'try! with a multiple parameters', 'try!(:join, ",")' + it_behaves_like 'accepts', 'try! with a block', 'try!(:map) { |e| e.some_method }' it_behaves_like 'accepts', 'try! with params and a block', ['try!(:each_with_object, []) do |e, acc|', ' acc << e.some_method', @@ -43,23 +40,17 @@ context 'target_ruby_version > 2.3', :ruby23 do context 'try!' do - it_behaves_like 'offense', 'try! with a single parameter', 'try!', - '(:join)' - it_behaves_like 'offense', 'try! with a multiple parameters', 'try!', - '(:join, ",")' - it_behaves_like 'offense', 'try! with a block', 'try!', - '(:map) { |e| e.some_method }' + it_behaves_like 'offense', 'try! with a single parameter', 'try!', '(:join)' + it_behaves_like 'offense', 'try! with a multiple parameters', 'try!', '(:join, ",")' + it_behaves_like 'offense', 'try! with a block', 'try!', '(:map) { |e| e.some_method }' it_behaves_like 'offense', 'try! with params and a block', 'try!', ['(:each_with_object, []) do |e, acc|', ' acc << e.some_method', 'end'].join("\n") - it_behaves_like 'offense', 'try! with a question method', 'try!', - '(:something?)' - it_behaves_like 'offense', 'try! with a bang method', 'try!', - '(:something!)' + it_behaves_like 'offense', 'try! with a question method', 'try!', '(:something?)' + it_behaves_like 'offense', 'try! with a bang method', 'try!', '(:something!)' - it_behaves_like 'accepts', 'try! used to call an enumerable accessor', - 'foo.try!(:[], :bar)' + it_behaves_like 'accepts', 'try! used to call an enumerable accessor', 'foo.try!(:[], :bar)' it_behaves_like 'accepts', 'try! with ==', 'foo.try!(:==, bar)' it_behaves_like 'accepts', 'try! with an operator', 'foo.try!(:+, bar)' it_behaves_like 'accepts', 'try! with a method stored as a variable', @@ -70,22 +61,17 @@ context 'try' do it_behaves_like 'accepts', 'try with a single parameter', 'try(:join)' - it_behaves_like 'accepts', 'try with a multiple parameters', - 'try(:join, ",")' - it_behaves_like 'accepts', 'try with a block', - 'try(:map) { |e| e.some_method }' + it_behaves_like 'accepts', 'try with a multiple parameters', 'try(:join, ",")' + it_behaves_like 'accepts', 'try with a block', 'try(:map) { |e| e.some_method }' it_behaves_like 'accepts', 'try with params and a block', ['try(:each_with_object, []) do |e, acc|', ' acc << e.some_method', 'end'].join("\n") end - it_behaves_like 'autocorrect', 'try! a single parameter', - 'foo.try!(:thing=, bar)', 'foo&.thing = bar' - it_behaves_like 'autocorrect', 'try! a single parameter', - '[1, 2].try!(:join)', '[1, 2]&.join' - it_behaves_like 'autocorrect', 'try! with 2 parameters', - '[1, 2].try!(:join, ",")', '[1, 2]&.join(",")' + it_behaves_like 'autocorrect', 'try! a single parameter', 'foo.try!(:thing=, bar)', 'foo&.thing = bar' + it_behaves_like 'autocorrect', 'try! a single parameter', '[1, 2].try!(:join)', '[1, 2]&.join' + it_behaves_like 'autocorrect', 'try! with 2 parameters', '[1, 2].try!(:join, ",")', '[1, 2]&.join(",")' it_behaves_like 'autocorrect', 'try! with multiple parameters', '[1, 2].try!(:join, bar, baz)', '[1, 2]&.join(bar, baz)' it_behaves_like 'autocorrect', 'try! with a block', @@ -109,10 +95,8 @@ context 'target_ruby_version < 2.3', :ruby22 do it_behaves_like 'accepts', 'try! with a single parameter', 'try!(:join)' - it_behaves_like 'accepts', 'try! with a multiple parameters', - 'try!(:join, ",")' - it_behaves_like 'accepts', 'try! with a block', - 'try!(:map) { |e| e.some_method }' + it_behaves_like 'accepts', 'try! with a multiple parameters', 'try!(:join, ",")' + it_behaves_like 'accepts', 'try! with a block', 'try!(:map) { |e| e.some_method }' it_behaves_like 'accepts', 'try! with params and a block', ['try!(:each_with_object, []) do |e, acc|', ' acc << e.some_method', @@ -121,28 +105,21 @@ context 'target_ruby_version > 2.3', :ruby23 do context 'try!' do - it_behaves_like 'offense', 'try! with a single parameter', 'try!', - '(:join)' - it_behaves_like 'offense', 'try! with a multiple parameters', 'try!', - '(:join, ",")' - it_behaves_like 'offense', 'try! with a block', 'try!', - '(:map) { |e| e.some_method }' + it_behaves_like 'offense', 'try! with a single parameter', 'try!', '(:join)' + it_behaves_like 'offense', 'try! with a multiple parameters', 'try!', '(:join, ",")' + it_behaves_like 'offense', 'try! with a block', 'try!', '(:map) { |e| e.some_method }' it_behaves_like 'offense', 'try! with params and a block', 'try!', ['(:each_with_object, []) do |e, acc|', ' acc << e.some_method', 'end'].join("\n") - it_behaves_like 'accepts', 'try! used to call an enumerable accessor', - 'foo.try!(:[], :bar)' + it_behaves_like 'accepts', 'try! used to call an enumerable accessor', 'foo.try!(:[], :bar)' - it_behaves_like 'autocorrect', 'try! a single parameter', - '[1, 2].try!(:join)', '[1, 2]&.join' - it_behaves_like 'autocorrect', 'try! with 2 parameters', - '[1, 2].try!(:join, ",")', '[1, 2]&.join(",")' + it_behaves_like 'autocorrect', 'try! a single parameter', '[1, 2].try!(:join)', '[1, 2]&.join' + it_behaves_like 'autocorrect', 'try! with 2 parameters', '[1, 2].try!(:join, ",")', '[1, 2]&.join(",")' it_behaves_like 'autocorrect', 'try! with multiple parameters', '[1, 2].try!(:join, bar, baz)', '[1, 2]&.join(bar, baz)' - it_behaves_like 'autocorrect', 'try! without receiver', - 'try!(:join)', 'self&.join' + it_behaves_like 'autocorrect', 'try! without receiver', 'try!(:join)', 'self&.join' it_behaves_like 'autocorrect', 'try! with a block', ['[foo, bar].try!(:map) do |e|', ' e.some_method', @@ -160,24 +137,18 @@ end context 'try' do - it_behaves_like 'offense', 'try with a single parameter', 'try', - '(:join)' - it_behaves_like 'offense', 'try with a multiple parameters', 'try', - '(:join, ",")' - it_behaves_like 'offense', 'try with a block', 'try', - '(:map) { |e| e.some_method }' + it_behaves_like 'offense', 'try with a single parameter', 'try', '(:join)' + it_behaves_like 'offense', 'try with a multiple parameters', 'try', '(:join, ",")' + it_behaves_like 'offense', 'try with a block', 'try', '(:map) { |e| e.some_method }' it_behaves_like 'offense', 'try with params and a block', 'try', ['(:each_with_object, []) do |e, acc|', ' acc << e.some_method', 'end'].join("\n") - it_behaves_like 'accepts', 'try! used to call an enumerable accessor', - 'foo.try!(:[], :bar)' + it_behaves_like 'accepts', 'try! used to call an enumerable accessor', 'foo.try!(:[], :bar)' - it_behaves_like 'autocorrect', 'try a single parameter', - '[1, 2].try(:join)', '[1, 2]&.join' - it_behaves_like 'autocorrect', 'try with 2 parameters', - '[1, 2].try(:join, ",")', '[1, 2]&.join(",")' + it_behaves_like 'autocorrect', 'try a single parameter', '[1, 2].try(:join)', '[1, 2]&.join' + it_behaves_like 'autocorrect', 'try with 2 parameters', '[1, 2].try(:join, ",")', '[1, 2]&.join(",")' it_behaves_like 'autocorrect', 'try with multiple parameters', '[1, 2].try(:join, bar, baz)', '[1, 2]&.join(bar, baz)' it_behaves_like 'autocorrect', 'try with a block', diff --git a/spec/rubocop/cop/rails/scope_args_spec.rb b/spec/rubocop/cop/rails/scope_args_spec.rb index 62f619aee4..d6c3cdf523 100644 --- a/spec/rubocop/cop/rails/scope_args_spec.rb +++ b/spec/rubocop/cop/rails/scope_args_spec.rb @@ -21,9 +21,7 @@ end it 'accepts a stabby lambda with arguments' do - expect_no_offenses( - 'scope :active, ->(active) { where(active: active) }' - ) + expect_no_offenses('scope :active, ->(active) { where(active: active) }') end it 'accepts a lambda' do @@ -31,9 +29,7 @@ end it 'accepts a lambda with a block argument' do - expect_no_offenses( - 'scope :active, lambda { |active| where(active: active) }' - ) + expect_no_offenses('scope :active, lambda { |active| where(active: active) }') end it 'accepts a lambda with a multiline block' do diff --git a/spec/rubocop/cop/rails/validation_spec.rb b/spec/rubocop/cop/rails/validation_spec.rb index f81bb5aa16..602b100111 100644 --- a/spec/rubocop/cop/rails/validation_spec.rb +++ b/spec/rubocop/cop/rails/validation_spec.rb @@ -38,8 +38,7 @@ include_examples 'autocorrects' end - context "with validates_#{type}_of " \ - 'when method arguments are enclosed in parentheses' do + context "with validates_#{type}_of when method arguments are enclosed in parentheses" do let(:autocorrected_source) do "validates(:full_name, :birth_date, #{type}: true)" end @@ -51,8 +50,7 @@ include_examples 'autocorrects' end - context "with validates_#{type}_of when " \ - 'attributes are specified with array literal' do + context "with validates_#{type}_of when attributes are specified with array literal" do let(:autocorrected_source) do "validates :full_name, :birth_date, #{type}: true" end @@ -64,8 +62,7 @@ include_examples 'autocorrects' end - context "with validates_#{type}_of when " \ - 'attributes are specified with frozen array literal' do + context "with validates_#{type}_of when attributes are specified with frozen array literal" do let(:autocorrected_source) do "validates :full_name, :birth_date, #{type}: true" end @@ -77,8 +74,7 @@ include_examples 'autocorrects' end - context "with validates_#{type}_of when " \ - 'attributes are specified with symbol array literal' do + context "with validates_#{type}_of when attributes are specified with symbol array literal" do let(:autocorrected_source) do "validates :full_name, :birth_date, #{type}: true" end @@ -90,8 +86,7 @@ include_examples 'autocorrects' end - context "with validates_#{type}_of when " \ - 'attributes are specified with frozen symbol array literal' do + context "with validates_#{type}_of when attributes are specified with frozen symbol array literal" do let(:autocorrected_source) do "validates :full_name, :birth_date, #{type}: true" end diff --git a/spec/rubocop/rails/schema_loader_spec.rb b/spec/rubocop/rails/schema_loader_spec.rb index 1d4b4db250..adaaf906ee 100644 --- a/spec/rubocop/rails/schema_loader_spec.rb +++ b/spec/rubocop/rails/schema_loader_spec.rb @@ -119,8 +119,7 @@ context 'when the current directory is a sub-directory of Rails.root' do before do - allow(Pathname).to receive(:pwd) - .and_return(rails_root.join('app/models')) + allow(Pathname).to receive(:pwd).and_return(rails_root.join('app/models')) end it_behaves_like 'returns a schema' diff --git a/spec/support/schema_loader.rb b/spec/support/schema_loader.rb index a50df3d499..d6ea844015 100644 --- a/spec/support/schema_loader.rb +++ b/spec/support/schema_loader.rb @@ -10,8 +10,7 @@ before do RuboCop::Rails::SchemaLoader.reset! schema_path.write(schema) - allow(RuboCop::Rails::SchemaLoader).to receive(:db_schema_path) - .and_return(schema_path) + allow(RuboCop::Rails::SchemaLoader).to receive(:db_schema_path).and_return(schema_path) end after do diff --git a/tasks/cut_release.rake b/tasks/cut_release.rake index 8afc413df2..b28b3fce20 100644 --- a/tasks/cut_release.rake +++ b/tasks/cut_release.rake @@ -38,10 +38,7 @@ namespace :cut_release do antora_metadata = File.read('docs/antora.yml') File.open('docs/antora.yml', 'w') do |f| - f << antora_metadata.sub( - 'version: ~', - "version: '#{version_sans_patch(new_version)}'" - ) + f << antora_metadata.sub('version: ~', "version: '#{version_sans_patch(new_version)}'") end end @@ -52,8 +49,7 @@ namespace :cut_release do # Replace `<>` (and variations) with version being cut. def update_cop_versions(_old_version, new_version) update_file('config/default.yml') do |default| - default.gsub(/['"]?<<\s*next\s*>>['"]?/i, - "'#{version_sans_patch(new_version)}'") + default.gsub(/['"]?<<\s*next\s*>>['"]?/i, "'#{version_sans_patch(new_version)}'") end end @@ -70,8 +66,7 @@ namespace :cut_release do def user_links(text) names = text.scan(/\[@(\S+)\]\[\]/).map(&:first).uniq - names.map { |name| "[@#{name}]: https://github.com/#{name}" } - .join("\n") + names.map { |name| "[@#{name}]: https://github.com/#{name}" }.join("\n") end def run(release_type)