From af55bb106b2a4310ff34270f7b11a3009127a978 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 6 Nov 2023 01:24:27 +0900 Subject: [PATCH] Require RuboCop AST 1.30.0+ Follow up https://github.com/rubocop/rubocop/pull/12305. --- changelog/change_require_rubocop_ast_1_30.md | 1 + lib/rubocop/cop/rails/after_commit_override.rb | 2 +- lib/rubocop/cop/rails/bulk_change_table.rb | 6 +++--- lib/rubocop/cop/rails/dangerous_column_names.rb | 2 +- lib/rubocop/cop/rails/file_path.rb | 10 +++++----- lib/rubocop/cop/rails/inverse_of.rb | 2 +- lib/rubocop/cop/rails/rake_environment.rb | 4 ++-- .../cop/rails/redundant_receiver_in_with_options.rb | 2 +- lib/rubocop/cop/rails/reversible_migration.rb | 4 ++-- .../cop/rails/unique_validation_without_index.rb | 2 +- lib/rubocop/cop/rails/validation.rb | 2 +- lib/rubocop/rails/schema_loader/schema.rb | 4 ++-- rubocop-rails.gemspec | 1 + 13 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 changelog/change_require_rubocop_ast_1_30.md diff --git a/changelog/change_require_rubocop_ast_1_30.md b/changelog/change_require_rubocop_ast_1_30.md new file mode 100644 index 0000000000..5e6ec97533 --- /dev/null +++ b/changelog/change_require_rubocop_ast_1_30.md @@ -0,0 +1 @@ +* [#1178](https://github.com/rubocop/rubocop-rails/pull/1178): Require RuboCop AST 1.30.0+. ([@koic][]) diff --git a/lib/rubocop/cop/rails/after_commit_override.rb b/lib/rubocop/cop/rails/after_commit_override.rb index 070941de77..7fb1fa0fa2 100644 --- a/lib/rubocop/cop/rails/after_commit_override.rb +++ b/lib/rubocop/cop/rails/after_commit_override.rb @@ -48,7 +48,7 @@ def on_class(class_node) seen_callback_names = {} each_after_commit_callback(class_node) do |node| - callback_name = node.arguments[0].value + callback_name = node.first_argument.value if seen_callback_names.key?(callback_name) add_offense(node, message: format(MSG, name: callback_name)) else diff --git a/lib/rubocop/cop/rails/bulk_change_table.rb b/lib/rubocop/cop/rails/bulk_change_table.rb index f7dcc99848..ad77628047 100644 --- a/lib/rubocop/cop/rails/bulk_change_table.rb +++ b/lib/rubocop/cop/rails/bulk_change_table.rb @@ -212,7 +212,7 @@ def combinable_transformations # @param node [RuboCop::AST::SendNode] def add_offense_for_alter_methods(node) # arguments: [{(sym :table)(str "table")} ...] - table_node = node.arguments[0] + table_node = node.first_argument return unless table_node.is_a? RuboCop::AST::BasicLiteralNode message = format(MSG_FOR_ALTER_METHODS, table: table_node.value) @@ -234,10 +234,10 @@ def initialize # @param new_node [RuboCop::AST::SendNode] def process(new_node) # arguments: [{(sym :table)(str "table")} ...] - table_node = new_node.arguments[0] + table_node = new_node.first_argument if table_node.is_a? RuboCop::AST::BasicLiteralNode flush unless @nodes.all? do |node| - node.arguments[0].value.to_s == table_node.value.to_s + node.first_argument.value.to_s == table_node.value.to_s end @nodes << new_node else diff --git a/lib/rubocop/cop/rails/dangerous_column_names.rb b/lib/rubocop/cop/rails/dangerous_column_names.rb index ff14fe3ec3..a1c1bcc1bb 100644 --- a/lib/rubocop/cop/rails/dangerous_column_names.rb +++ b/lib/rubocop/cop/rails/dangerous_column_names.rb @@ -428,7 +428,7 @@ def column_name_node_from(node) when :rename_column node.arguments[2] when *COLUMN_TYPE_METHOD_NAMES - node.arguments[0] + node.first_argument end end diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index ee9ef97f71..f98f37076f 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -163,9 +163,9 @@ def autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index def autocorrect_extension_after_rails_root_join_in_dstr(corrector, node, rails_root_index, extension_node) rails_root_node = node.children[rails_root_index].children.first - return unless rails_root_node.arguments.last.str_type? + return unless rails_root_node.last_argument.str_type? - corrector.insert_before(rails_root_node.arguments.last.location.end, extension_node.source) + corrector.insert_before(rails_root_node.last_argument.location.end, extension_node.source) corrector.remove(extension_node) end @@ -174,7 +174,7 @@ def autocorrect_file_join(corrector, node) corrector.remove( range_with_surrounding_space( range_with_surrounding_comma( - node.arguments.first.source_range, + node.first_argument.source_range, :right ), side: :right @@ -187,7 +187,7 @@ def autocorrect_file_join(corrector, node) end def autocorrect_rails_root_join_with_string_arguments(corrector, node) - corrector.replace(node.arguments.first, %("#{node.arguments.map(&:value).join('/')}")) + corrector.replace(node.first_argument, %("#{node.arguments.map(&:value).join('/')}")) node.arguments[1..].each do |argument| corrector.remove( range_with_surrounding_comma( @@ -221,7 +221,7 @@ def find_rails_root_index(node) end def append_argument(corrector, node, argument_source) - corrector.insert_after(node.arguments.last, %(, "#{argument_source}")) + corrector.insert_after(node.last_argument, %(, "#{argument_source}")) end def replace_with_rails_root_join(corrector, node, argument_source) diff --git a/lib/rubocop/cop/rails/inverse_of.rb b/lib/rubocop/cop/rails/inverse_of.rb index c4dbd0e8eb..7b04522714 100644 --- a/lib/rubocop/cop/rails/inverse_of.rb +++ b/lib/rubocop/cop/rails/inverse_of.rb @@ -222,7 +222,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.first_argument, recv) end blocks.flat_map { |n| n.send_node.arguments } end diff --git a/lib/rubocop/cop/rails/rake_environment.rb b/lib/rubocop/cop/rails/rake_environment.rb index 78274ebf1c..bf8a285d4e 100644 --- a/lib/rubocop/cop/rails/rake_environment.rb +++ b/lib/rubocop/cop/rails/rake_environment.rb @@ -72,7 +72,7 @@ def correct_task_dependency(task_name) end def task_name(node) - first_arg = node.arguments[0] + first_arg = node.first_argument case first_arg&.type when :sym, :str first_arg.value.to_sym @@ -97,7 +97,7 @@ def with_arguments?(node) end def with_dependencies?(node) - first_arg = node.arguments[0] + first_arg = node.first_argument return false unless first_arg if first_arg.hash_type? diff --git a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb index ec6699e96d..62d52786cf 100644 --- a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +++ b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb @@ -100,7 +100,7 @@ def redundant_receiver?(send_nodes, node) else return false if node.arguments.empty? - arg = node.arguments.first + arg = node.first_argument ->(n) { same_value?(arg, n.receiver) } end diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index edafc95201..c881797eae 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -290,10 +290,10 @@ def reversible_change_table_call?(node) when :change false when :remove - target_rails_version >= 6.1 && all_hash_key?(node.arguments.last, :type) + target_rails_version >= 6.1 && all_hash_key?(node.last_argument, :type) when :change_default, :change_column_default, :change_table_comment, :change_column_comment - all_hash_key?(node.arguments.last, :from, :to) + all_hash_key?(node.last_argument, :from, :to) else true end diff --git a/lib/rubocop/cop/rails/unique_validation_without_index.rb b/lib/rubocop/cop/rails/unique_validation_without_index.rb index 15aaa7758b..f355c2c6e8 100644 --- a/lib/rubocop/cop/rails/unique_validation_without_index.rb +++ b/lib/rubocop/cop/rails/unique_validation_without_index.rb @@ -124,7 +124,7 @@ def class_node(node) end def uniqueness_part(node) - pairs = node.arguments.last + pairs = node.last_argument return unless pairs&.hash_type? pairs.each_pair.find do |pair| diff --git a/lib/rubocop/cop/rails/validation.rb b/lib/rubocop/cop/rails/validation.rb index f250d5d8a4..2bef7913cb 100644 --- a/lib/rubocop/cop/rails/validation.rb +++ b/lib/rubocop/cop/rails/validation.rb @@ -60,7 +60,7 @@ def on_send(node) range = node.loc.selector add_offense(range, message: message(node)) do |corrector| - last_argument = node.arguments.last + last_argument = node.last_argument return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument) corrector.replace(range, 'validates') diff --git a/lib/rubocop/rails/schema_loader/schema.rb b/lib/rubocop/rails/schema_loader/schema.rb index 424441d975..7bb511d5a7 100644 --- a/lib/rubocop/rails/schema_loader/schema.rb +++ b/lib/rubocop/rails/schema_loader/schema.rb @@ -127,7 +127,7 @@ def initialize(node) private def analyze_keywords!(node) - pairs = node.arguments.last + pairs = node.last_argument return unless pairs.hash_type? pairs.each_pair do |k, v| @@ -158,7 +158,7 @@ def build_columns_or_expr(columns) end def analyze_keywords!(node) - pairs = node.arguments.last + pairs = node.last_argument return unless pairs.hash_type? pairs.each_pair do |k, v| diff --git a/rubocop-rails.gemspec b/rubocop-rails.gemspec index 5467c0db16..2bccd94c71 100644 --- a/rubocop-rails.gemspec +++ b/rubocop-rails.gemspec @@ -36,4 +36,5 @@ Gem::Specification.new do |s| # introduced in rack 1.1 s.add_runtime_dependency 'rack', '>= 1.1' s.add_runtime_dependency 'rubocop', '>= 1.33.0', '< 2.0' + s.add_runtime_dependency 'rubocop-ast', '>= 1.30.0', '< 2.0' end