diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /rubocop/cop/migration | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'rubocop/cop/migration')
31 files changed, 69 insertions, 81 deletions
diff --git a/rubocop/cop/migration/add_column_with_default.rb b/rubocop/cop/migration/add_column_with_default.rb index afd7d93cd47..36603e09263 100644 --- a/rubocop/cop/migration/add_column_with_default.rb +++ b/rubocop/cop/migration/add_column_with_default.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class AddColumnWithDefault < RuboCop::Cop::Cop + class AddColumnWithDefault < RuboCop::Cop::Base include MigrationHelpers MSG = '`add_column_with_default` is deprecated, use `add_column` instead' @@ -15,7 +15,7 @@ module RuboCop name = node.children[1] - add_offense(node, location: :selector) if name == :add_column_with_default + add_offense(node.loc.selector) if name == :add_column_with_default end end end diff --git a/rubocop/cop/migration/add_columns_to_wide_tables.rb b/rubocop/cop/migration/add_columns_to_wide_tables.rb index 41056379515..98dd605faef 100644 --- a/rubocop/cop/migration/add_columns_to_wide_tables.rb +++ b/rubocop/cop/migration/add_columns_to_wide_tables.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that prevents adding columns to wide tables. - class AddColumnsToWideTables < RuboCop::Cop::Cop + class AddColumnsToWideTables < RuboCop::Cop::Base include MigrationHelpers MSG = '`%s` is a wide table with several columns, adding more should be avoided unless absolutely necessary.' \ @@ -26,7 +26,7 @@ module RuboCop return unless offense?(method_name, table_name) - add_offense(node, location: :selector, message: format(MSG, table_name.value)) + add_offense(node.loc.selector, message: format(MSG, table_name.value)) end private diff --git a/rubocop/cop/migration/add_concurrent_foreign_key.rb b/rubocop/cop/migration/add_concurrent_foreign_key.rb index ebab6aa653e..f538207d336 100644 --- a/rubocop/cop/migration/add_concurrent_foreign_key.rb +++ b/rubocop/cop/migration/add_concurrent_foreign_key.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if `add_concurrent_foreign_key` is used instead of # `add_foreign_key`. - class AddConcurrentForeignKey < RuboCop::Cop::Cop + class AddConcurrentForeignKey < RuboCop::Cop::Base include MigrationHelpers MSG = '`add_foreign_key` requires downtime, use `add_concurrent_foreign_key` instead' @@ -29,7 +29,7 @@ module RuboCop return if in_with_lock_retries?(node) return if not_valid_fk?(node) - add_offense(node, location: :selector) + add_offense(node.loc.selector) end def method_name(node) diff --git a/rubocop/cop/migration/add_concurrent_index.rb b/rubocop/cop/migration/add_concurrent_index.rb index bfe7c15bfdf..77b8ced4778 100644 --- a/rubocop/cop/migration/add_concurrent_index.rb +++ b/rubocop/cop/migration/add_concurrent_index.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if `add_concurrent_index` is used with `up`/`down` methods # and not `change`. - class AddConcurrentIndex < RuboCop::Cop::Cop + class AddConcurrentIndex < RuboCop::Cop::Base include MigrationHelpers MSG = '`add_concurrent_index` is not reversible so you must manually define ' \ @@ -21,15 +21,11 @@ module RuboCop return unless name == :add_concurrent_index node.each_ancestor(:def) do |def_node| - next unless method_name(def_node) == :change + next unless def_node.method_name == :change - add_offense(def_node, location: :name) + add_offense(def_node.loc.name) end end - - def method_name(node) - node.children.first - end end end end diff --git a/rubocop/cop/migration/add_index.rb b/rubocop/cop/migration/add_index.rb index 327e89fb040..3920fd19c98 100644 --- a/rubocop/cop/migration/add_index.rb +++ b/rubocop/cop/migration/add_index.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that checks if indexes are added in a concurrent manner. - class AddIndex < RuboCop::Cop::Cop + class AddIndex < RuboCop::Cop::Base include MigrationHelpers MSG = '`add_index` requires downtime, use `add_concurrent_index` instead' @@ -29,7 +29,7 @@ module RuboCop # data in these tables yet. next if new_tables.include?(first_arg) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end diff --git a/rubocop/cop/migration/add_limit_to_text_columns.rb b/rubocop/cop/migration/add_limit_to_text_columns.rb index a47fbe0bf16..5c71fbbfaa2 100644 --- a/rubocop/cop/migration/add_limit_to_text_columns.rb +++ b/rubocop/cop/migration/add_limit_to_text_columns.rb @@ -10,7 +10,7 @@ module RuboCop # Text columns starting with `encrypted_` are very likely used # by `attr_encrypted` which controls the text length. Those columns # should not add a text limit. - class AddLimitToTextColumns < RuboCop::Cop::Cop + class AddLimitToTextColumns < RuboCop::Cop::Base include MigrationHelpers TEXT_LIMIT_ATTRIBUTE_ALLOWED_SINCE = 2021_09_10_00_00_00 @@ -43,11 +43,11 @@ module RuboCop next unless text_operation?(send_node) if text_operation_with_limit?(send_node) - add_offense(send_node, location: :selector, message: TEXT_LIMIT_ATTRIBUTE_NOT_ALLOWED) if version(node) < TEXT_LIMIT_ATTRIBUTE_ALLOWED_SINCE + add_offense(send_node.loc.selector, message: TEXT_LIMIT_ATTRIBUTE_NOT_ALLOWED) if version(node) < TEXT_LIMIT_ATTRIBUTE_ALLOWED_SINCE else # We require a limit for the same table and attribute name if text_limit_missing?(node, *table_and_attribute_name(send_node)) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end end diff --git a/rubocop/cop/migration/add_reference.rb b/rubocop/cop/migration/add_reference.rb index 33840fc7caf..02a0ef899b4 100644 --- a/rubocop/cop/migration/add_reference.rb +++ b/rubocop/cop/migration/add_reference.rb @@ -6,7 +6,7 @@ module RuboCop module Migration # add_reference can only be used with newly created tables. # Additionally, the cop here checks that we create an index for the foreign key, too. - class AddReference < RuboCop::Cop::Cop + class AddReference < RuboCop::Cop::Base include MigrationHelpers MSG = '`add_reference` requires downtime for existing tables, use `add_concurrent_foreign_key` instead. When used for new tables, `index: true` or `index: { options... } is required.`' @@ -28,12 +28,12 @@ module RuboCop # Using "add_reference" is fine for newly created tables as there's no # data in these tables yet. if existing_table?(new_tables, first_arg) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end # We require an index on the foreign key column. if index_missing?(node) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end end diff --git a/rubocop/cop/migration/add_timestamps.rb b/rubocop/cop/migration/add_timestamps.rb index 01d3f01ef4f..8b09d730cbc 100644 --- a/rubocop/cop/migration/add_timestamps.rb +++ b/rubocop/cop/migration/add_timestamps.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that checks if 'add_timestamps' method is called with timezone information. - class AddTimestamps < RuboCop::Cop::Cop + class AddTimestamps < RuboCop::Cop::Base include MigrationHelpers MSG = 'Do not use `add_timestamps`, use `add_timestamps_with_timezone` instead' @@ -15,7 +15,7 @@ module RuboCop def on_send(node) return unless in_migration?(node) - add_offense(node, location: :selector) if method_name(node) == :add_timestamps + add_offense(node.loc.selector) if method_name(node) == :add_timestamps end def method_name(node) diff --git a/rubocop/cop/migration/background_migration_base_class.rb b/rubocop/cop/migration/background_migration_base_class.rb index 50cbe3a69c3..9ec8403a607 100644 --- a/rubocop/cop/migration/background_migration_base_class.rb +++ b/rubocop/cop/migration/background_migration_base_class.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Migration - class BackgroundMigrationBaseClass < RuboCop::Cop::Cop + class BackgroundMigrationBaseClass < RuboCop::Cop::Base MSG = 'Batched background migration jobs should inherit from Gitlab::BackgroundMigration::BatchedMigrationJob' def_node_search :top_level_module?, <<~PATTERN @@ -25,7 +25,7 @@ module RuboCop return if top_level_class_node.nil? || inherits_batched_migration_job?(top_level_class_node) - add_offense(top_level_class_node, location: :expression) + add_offense(top_level_class_node) end end end diff --git a/rubocop/cop/migration/background_migration_record.rb b/rubocop/cop/migration/background_migration_record.rb index 2ee6b9f7b42..ee6f3fd140b 100644 --- a/rubocop/cop/migration/background_migration_record.rb +++ b/rubocop/cop/migration/background_migration_record.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class BackgroundMigrationRecord < RuboCop::Cop::Cop + class BackgroundMigrationRecord < RuboCop::Cop::Base include MigrationHelpers MSG = <<~MSG @@ -26,14 +26,14 @@ module RuboCop return unless in_background_migration?(node) return unless inherits_from_active_record_base?(node) - add_offense(node, location: :expression) + add_offense(node) end def on_send(node) return unless in_background_migration?(node) return unless class_new_active_record_base?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/migration/background_migrations.rb b/rubocop/cop/migration/background_migrations.rb index 39c5496e4d3..7dcc2101fb1 100644 --- a/rubocop/cop/migration/background_migrations.rb +++ b/rubocop/cop/migration/background_migrations.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class BackgroundMigrations < RuboCop::Cop::Cop + class BackgroundMigrations < RuboCop::Cop::Base include MigrationHelpers MSG = 'Background migrations are deprecated. Please use a Batched Background Migration instead.'\ @@ -20,7 +20,7 @@ module RuboCop migrate_in ) - add_offense(node, location: :selector) if disabled_methods.include? name + add_offense(node.loc.selector) if disabled_methods.include? name end end end diff --git a/rubocop/cop/migration/complex_indexes_require_name.rb b/rubocop/cop/migration/complex_indexes_require_name.rb index 82deb36716d..771537f2576 100644 --- a/rubocop/cop/migration/complex_indexes_require_name.rb +++ b/rubocop/cop/migration/complex_indexes_require_name.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class ComplexIndexesRequireName < RuboCop::Cop::Cop + class ComplexIndexesRequireName < RuboCop::Cop::Base include MigrationHelpers MSG = 'indexes added with custom options must be explicitly named' @@ -32,7 +32,7 @@ module RuboCop node.each_descendant(:send) do |send_node| next unless create_table_with_index_offense?(send_node) || add_index_offense?(send_node) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end diff --git a/rubocop/cop/migration/create_table_with_foreign_keys.rb b/rubocop/cop/migration/create_table_with_foreign_keys.rb index 382a2d6f65b..e3039ac76a9 100644 --- a/rubocop/cop/migration/create_table_with_foreign_keys.rb +++ b/rubocop/cop/migration/create_table_with_foreign_keys.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class CreateTableWithForeignKeys < RuboCop::Cop::Cop + class CreateTableWithForeignKeys < RuboCop::Cop::Base include MigrationHelpers MSG = 'Creating a table with more than one foreign key at once violates our migration style guide. ' \ diff --git a/rubocop/cop/migration/datetime.rb b/rubocop/cop/migration/datetime.rb index c605c8e1b6e..9cdb2d83ab5 100644 --- a/rubocop/cop/migration/datetime.rb +++ b/rubocop/cop/migration/datetime.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that checks if datetime data type is added with timezone information. - class Datetime < RuboCop::Cop::Cop + class Datetime < RuboCop::Cop::Base include MigrationHelpers MSG = 'Do not use the `%s` data type, use `datetime_with_timezone` instead' @@ -19,7 +19,7 @@ module RuboCop method_name = send_node.children[1] if method_name == :datetime || method_name == :timestamp - add_offense(send_node, location: :selector, message: format(MSG, method_name)) + add_offense(send_node.loc.selector, message: format(MSG, method_name)) end end end @@ -34,7 +34,7 @@ module RuboCop last_argument = descendant.children.last if last_argument == :datetime || last_argument == :timestamp - add_offense(node, location: :expression, message: format(MSG, last_argument)) + add_offense(node, message: format(MSG, last_argument)) end end end diff --git a/rubocop/cop/migration/drop_table.rb b/rubocop/cop/migration/drop_table.rb index 531cbb14021..62dea79cbe6 100644 --- a/rubocop/cop/migration/drop_table.rb +++ b/rubocop/cop/migration/drop_table.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if `drop_table` is called in deployment migrations. # Calling it in deployment migrations can cause downtimes as there still may be code using the target tables. - class DropTable < RuboCop::Cop::Cop + class DropTable < RuboCop::Cop::Base include MigrationHelpers MSG = <<-MESSAGE.delete("\n").squeeze @@ -22,7 +22,7 @@ module RuboCop node.each_descendant(:send) do |send_node| next unless offensible?(send_node) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end diff --git a/rubocop/cop/migration/migration_record.rb b/rubocop/cop/migration/migration_record.rb index 291644f10e3..1543d2fff08 100644 --- a/rubocop/cop/migration/migration_record.rb +++ b/rubocop/cop/migration/migration_record.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class MigrationRecord < RuboCop::Cop::Cop + class MigrationRecord < RuboCop::Cop::Base include MigrationHelpers ENFORCED_SINCE = 2022_04_26_00_00_00 @@ -27,7 +27,7 @@ module RuboCop return unless relevant_migration?(node) return unless inherits_from_active_record_base?(node) || inherits_from_application_record?(node) - add_offense(node, location: :expression) + add_offense(node) end private diff --git a/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb index f5343f973e1..9556a16dc01 100644 --- a/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb +++ b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that prevents usage of `enable_lock_retries!` within the `disable_ddl_transaction!` method. - class PreventGlobalEnableLockRetriesWithDisableDdlTransaction < RuboCop::Cop::Cop + class PreventGlobalEnableLockRetriesWithDisableDdlTransaction < RuboCop::Cop::Base include MigrationHelpers MSG = '`enable_lock_retries!` cannot be used with `disable_ddl_transaction!`. Use the `with_lock_retries` helper method to define retriable code blocks.' diff --git a/rubocop/cop/migration/prevent_index_creation.rb b/rubocop/cop/migration/prevent_index_creation.rb index c383466f73b..185f36cb24b 100644 --- a/rubocop/cop/migration/prevent_index_creation.rb +++ b/rubocop/cop/migration/prevent_index_creation.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module Migration # Cop that checks if new indexes are introduced to forbidden tables. - class PreventIndexCreation < RuboCop::Cop::Cop + class PreventIndexCreation < RuboCop::Cop::Base include MigrationHelpers FORBIDDEN_TABLES = %i[ci_builds].freeze @@ -41,7 +41,7 @@ module RuboCop return unless in_migration?(node) node.each_descendant(:send) do |send_node| - add_offense(send_node, location: :selector) if offense?(send_node) + add_offense(send_node.loc.selector) if offense?(send_node) end end diff --git a/rubocop/cop/migration/prevent_strings.rb b/rubocop/cop/migration/prevent_strings.rb index 57e29bf74ae..89659050f64 100644 --- a/rubocop/cop/migration/prevent_strings.rb +++ b/rubocop/cop/migration/prevent_strings.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that enforces using text instead of the string data type - class PreventStrings < RuboCop::Cop::Cop + class PreventStrings < RuboCop::Cop::Base include MigrationHelpers MSG = 'Do not use the `string` data type, use `text` instead. ' \ @@ -26,7 +26,7 @@ module RuboCop node.each_descendant(:send) do |send_node| next unless string_operation?(send_node) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end diff --git a/rubocop/cop/migration/refer_to_index_by_name.rb b/rubocop/cop/migration/refer_to_index_by_name.rb index fbf3c0d7030..78619472487 100644 --- a/rubocop/cop/migration/refer_to_index_by_name.rb +++ b/rubocop/cop/migration/refer_to_index_by_name.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class ReferToIndexByName < RuboCop::Cop::Cop + class ReferToIndexByName < RuboCop::Cop::Base include MigrationHelpers MSG = 'migration methods that refer to existing indexes must do so by name' @@ -32,7 +32,7 @@ module RuboCop node.each_descendant(:send) do |send_node| next unless index_exists_offense?(send_node) || removing_index_offense?(send_node) - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end diff --git a/rubocop/cop/migration/remove_column.rb b/rubocop/cop/migration/remove_column.rb index 6a171ac948f..e8477a211e4 100644 --- a/rubocop/cop/migration/remove_column.rb +++ b/rubocop/cop/migration/remove_column.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if remove_column is used in a regular (not # post-deployment) migration. - class RemoveColumn < RuboCop::Cop::Cop + class RemoveColumn < RuboCop::Cop::Base include MigrationHelpers MSG = '`remove_column` must only be used in post-deployment migrations' @@ -22,7 +22,7 @@ module RuboCop send_method = send_node.children[1] if send_method == :remove_column - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end end diff --git a/rubocop/cop/migration/remove_concurrent_index.rb b/rubocop/cop/migration/remove_concurrent_index.rb index 30dd59d97bc..459f474d185 100644 --- a/rubocop/cop/migration/remove_concurrent_index.rb +++ b/rubocop/cop/migration/remove_concurrent_index.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if `remove_concurrent_index` is used with `up`/`down` methods # and not `change`. - class RemoveConcurrentIndex < RuboCop::Cop::Cop + class RemoveConcurrentIndex < RuboCop::Cop::Base include MigrationHelpers MSG = '`remove_concurrent_index` is not reversible so you must manually define ' \ @@ -18,13 +18,9 @@ module RuboCop return unless node.children[1] == :remove_concurrent_index node.each_ancestor(:def) do |def_node| - add_offense(def_node, location: :name) if method_name(def_node) == :change + add_offense(def_node.loc.name) if def_node.method_name == :change end end - - def method_name(node) - node.children[0] - end end end end diff --git a/rubocop/cop/migration/remove_index.rb b/rubocop/cop/migration/remove_index.rb index ca5d4af1520..26d271207dc 100644 --- a/rubocop/cop/migration/remove_index.rb +++ b/rubocop/cop/migration/remove_index.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that checks if indexes are removed in a concurrent manner. - class RemoveIndex < RuboCop::Cop::Cop + class RemoveIndex < RuboCop::Cop::Base include MigrationHelpers MSG = '`remove_index` requires downtime, use `remove_concurrent_index` instead' @@ -15,7 +15,7 @@ module RuboCop return unless in_migration?(node) node.each_descendant(:send) do |send_node| - add_offense(send_node, location: :selector) if method_name(send_node) == :remove_index + add_offense(send_node.loc.selector) if method_name(send_node) == :remove_index end end diff --git a/rubocop/cop/migration/safer_boolean_column.rb b/rubocop/cop/migration/safer_boolean_column.rb index 1d780d96afa..d3d77b16357 100644 --- a/rubocop/cop/migration/safer_boolean_column.rb +++ b/rubocop/cop/migration/safer_boolean_column.rb @@ -18,7 +18,7 @@ module RuboCop # # See https://gitlab.com/gitlab-org/gitlab/issues/2750 for more # information. - class SaferBooleanColumn < RuboCop::Cop::Cop + class SaferBooleanColumn < RuboCop::Cop::Base include MigrationHelpers DEFAULT_OFFENSE = 'Boolean columns on the `%s` table should have a default. You may wish to use `add_column_with_default`.' @@ -52,7 +52,7 @@ module RuboCop NULL_OFFENSE end - add_offense(node, location: :expression, message: format(offense, table)) if offense + add_offense(node, message: format(offense, table)) if offense end def no_default?(opts) diff --git a/rubocop/cop/migration/schedule_async.rb b/rubocop/cop/migration/schedule_async.rb index 4fdedecdf64..bc98d45b9c6 100644 --- a/rubocop/cop/migration/schedule_async.rb +++ b/rubocop/cop/migration/schedule_async.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class ScheduleAsync < RuboCop::Cop::Cop + class ScheduleAsync < RuboCop::Cop::Base include MigrationHelpers ENFORCED_SINCE = 2020_02_12_00_00_00 @@ -32,7 +32,7 @@ module RuboCop return if version(node) < ENFORCED_SINCE return unless calls_background_migration_worker?(node) || calls_ci_database_worker?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/migration/sidekiq_queue_migrate.rb b/rubocop/cop/migration/sidekiq_queue_migrate.rb index 134bda590da..4a7aaa353a3 100644 --- a/rubocop/cop/migration/sidekiq_queue_migrate.rb +++ b/rubocop/cop/migration/sidekiq_queue_migrate.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if sidekiq_queue_migrate is used in a regular # (not post-deployment) migration. - class SidekiqQueueMigrate < RuboCop::Cop::Cop + class SidekiqQueueMigrate < RuboCop::Cop::Base include MigrationHelpers MSG = '`sidekiq_queue_migrate` must only be used in post-deployment migrations' @@ -19,7 +19,7 @@ module RuboCop send_method = send_node.children[1] if send_method == :sidekiq_queue_migrate - add_offense(send_node, location: :selector) + add_offense(send_node.loc.selector) end end end diff --git a/rubocop/cop/migration/timestamps.rb b/rubocop/cop/migration/timestamps.rb index 44baf17d968..5cc78a8821a 100644 --- a/rubocop/cop/migration/timestamps.rb +++ b/rubocop/cop/migration/timestamps.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that checks if 'timestamps' method is called with timezone information. - class Timestamps < RuboCop::Cop::Cop + class Timestamps < RuboCop::Cop::Base include MigrationHelpers MSG = 'Do not use `timestamps`, use `timestamps_with_timezone` instead' @@ -16,7 +16,7 @@ module RuboCop return unless in_migration?(node) node.each_descendant(:send) do |send_node| - add_offense(send_node, location: :selector) if method_name(send_node) == :timestamps + add_offense(send_node.loc.selector) if method_name(send_node) == :timestamps end end diff --git a/rubocop/cop/migration/update_column_in_batches.rb b/rubocop/cop/migration/update_column_in_batches.rb index e23042e1b9f..7f4479c62e3 100644 --- a/rubocop/cop/migration/update_column_in_batches.rb +++ b/rubocop/cop/migration/update_column_in_batches.rb @@ -7,7 +7,7 @@ module RuboCop module Migration # Cop that checks if a spec file exists for any migration using # `update_column_in_batches`. - class UpdateColumnInBatches < RuboCop::Cop::Cop + class UpdateColumnInBatches < RuboCop::Cop::Base include MigrationHelpers MSG = 'Migration running `update_column_in_batches` must have a spec file at' \ @@ -19,9 +19,9 @@ module RuboCop spec_path = spec_filename(node) - unless File.exist?(File.expand_path(spec_path, rails_root)) - add_offense(node, location: :expression, message: format(MSG, spec_path)) - end + return if File.exist?(File.expand_path(spec_path, rails_root)) + + add_offense(node, message: format(MSG, spec_path)) end private diff --git a/rubocop/cop/migration/versioned_migration_class.rb b/rubocop/cop/migration/versioned_migration_class.rb index f2e4550c691..648782f1735 100644 --- a/rubocop/cop/migration/versioned_migration_class.rb +++ b/rubocop/cop/migration/versioned_migration_class.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class VersionedMigrationClass < RuboCop::Cop::Cop + class VersionedMigrationClass < RuboCop::Cop::Base include MigrationHelpers ENFORCED_SINCE = 2021_09_02_00_00_00 @@ -26,13 +26,13 @@ module RuboCop return unless relevant_migration?(node) return unless activerecord_migration_class?(node) - add_offense(node, location: :expression, message: MSG_INHERIT) + add_offense(node, message: MSG_INHERIT) end def on_send(node) return unless relevant_migration?(node) - add_offense(node, location: :expression, message: MSG_INCLUDE) if includes_helpers?(node) + add_offense(node, message: MSG_INCLUDE) if includes_helpers?(node) end private diff --git a/rubocop/cop/migration/with_lock_retries_disallowed_method.rb b/rubocop/cop/migration/with_lock_retries_disallowed_method.rb index b3d05ad1a6d..5c96b38dcdc 100644 --- a/rubocop/cop/migration/with_lock_retries_disallowed_method.rb +++ b/rubocop/cop/migration/with_lock_retries_disallowed_method.rb @@ -5,7 +5,7 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - class WithLockRetriesDisallowedMethod < RuboCop::Cop::Cop + class WithLockRetriesDisallowedMethod < RuboCop::Cop::Base include MigrationHelpers ALLOWED_MIGRATION_METHODS = %i[ @@ -60,8 +60,8 @@ module RuboCop return unless send_node?(node) name = node.children[1] - add_offense(node, location: :expression) unless ALLOWED_MIGRATION_METHODS.include?(name) - add_offense(node, location: :selector, message: MSG_ONLY_ONE_FK_ALLOWED) if multiple_fks?(node) + add_offense(node) unless ALLOWED_MIGRATION_METHODS.include?(name) + add_offense(node.loc.selector, message: MSG_ONLY_ONE_FK_ALLOWED) if multiple_fks?(node) end def multiple_fks?(node) diff --git a/rubocop/cop/migration/with_lock_retries_with_change.rb b/rubocop/cop/migration/with_lock_retries_with_change.rb index 9d11edcb6a1..59dbd6f22d2 100644 --- a/rubocop/cop/migration/with_lock_retries_with_change.rb +++ b/rubocop/cop/migration/with_lock_retries_with_change.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module Migration # Cop that prevents usage of `with_lock_retries` within the `change` method. - class WithLockRetriesWithChange < RuboCop::Cop::Cop + class WithLockRetriesWithChange < RuboCop::Cop::Base include MigrationHelpers MSG = '`with_lock_retries` cannot be used within `change` so you must manually define ' \ @@ -17,13 +17,9 @@ module RuboCop return unless node.children[1] == :with_lock_retries node.each_ancestor(:def) do |def_node| - add_offense(def_node, location: :name) if method_name(def_node) == :change + add_offense(def_node.loc.name) if def_node.method_name == :change end end - - def method_name(node) - node.children.first - end end end end |