From b80ecd9cce3abaa3c7b3b180418fb981a0c76c7f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 18 Jan 2025 15:46:58 +0900 Subject: [PATCH 01/10] Switch back docs version to master --- docs/antora.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/antora.yml b/docs/antora.yml index 5a69a42487..9e0ff48acb 100644 --- a/docs/antora.yml +++ b/docs/antora.yml @@ -2,6 +2,6 @@ name: rubocop-rails title: RuboCop Rails # We always provide version without patch here (e.g. 1.1), # as patch versions should not appear in the docs. -version: '2.29' +version: ~ nav: - modules/ROOT/nav.adoc From efc6dc22d5dff37cba6796f1daffcd9fe7ef296c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 19 Jan 2025 02:35:34 +0900 Subject: [PATCH 02/10] [Fix #1417] Fix an incorrect autocorrect for `Rails/StrongParametersExpect` This PR fixes an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `params.require(:user).permit(:name, :age)`. Fixes #1417. --- ...tocorrect_for_rails_strong_parameters_expect.md | 1 + lib/rubocop/cop/rails/strong_parameters_expect.rb | 2 +- .../cop/rails/strong_parameters_expect_spec.rb | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md diff --git a/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md b/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md new file mode 100644 index 0000000000..426ad18c94 --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md @@ -0,0 +1 @@ +* [#1417](https://github.com/rubocop/rubocop-rails/issues/1417): Fix an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `require` with `permit`. ([@koic][]) diff --git a/lib/rubocop/cop/rails/strong_parameters_expect.rb b/lib/rubocop/cop/rails/strong_parameters_expect.rb index 842523f629..54b108ab6a 100644 --- a/lib/rubocop/cop/rails/strong_parameters_expect.rb +++ b/lib/rubocop/cop/rails/strong_parameters_expect.rb @@ -59,7 +59,7 @@ def on_send(node) end add_offense(range, message: format(MSG, prefer: prefer)) do |corrector| - corrector.remove(require_method.loc.dot.join(require_method.source_range.end)) + corrector.remove(require_method.receiver.source_range.end.join(require_method.source_range.end)) corrector.replace(permit_method.loc.selector, 'expect') if replace_argument corrector.insert_before(permit_method.first_argument, "#{require_key(require_method)}[") diff --git a/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb b/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb index 036822f9ea..f7758327ca 100644 --- a/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb +++ b/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb @@ -98,6 +98,20 @@ RUBY end + it 'registers an offense when using a leading dot multiline call to `params.require(:user).permit(:name, :age)`' do + expect_offense(<<~RUBY) + params + .require(:user) + ^^^^^^^^^^^^^^ Use `expect(user: [:name, :age])` instead. + .permit(:name, :age) + RUBY + + expect_correction(<<~RUBY) + params + .expect(user: [:name, :age]) + RUBY + end + it 'does not register an offense when using `params.expect(user: [:name, :age])`' do expect_no_offenses(<<~RUBY) params.expect(user: [:name, :age]) From 48233de765dbf7836a0dd293be4bfda10e6d5c9b Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Sun, 19 Jan 2025 02:05:03 +0400 Subject: [PATCH 03/10] [Fix rubocop#1228] Enhance Rails/SaveBang to properly handle instance variables --- ..._save_bang_to_handle_instance_variables.md | 1 + lib/rubocop/cop/rails/save_bang.rb | 5 +- spec/rubocop/cop/rails/save_bang_spec.rb | 50 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_rails_save_bang_to_handle_instance_variables.md diff --git a/changelog/fix_rails_save_bang_to_handle_instance_variables.md b/changelog/fix_rails_save_bang_to_handle_instance_variables.md new file mode 100644 index 0000000000..6da9b2c2df --- /dev/null +++ b/changelog/fix_rails_save_bang_to_handle_instance_variables.md @@ -0,0 +1 @@ +* [#1228](https://github.com/rubocop/rubocop-rails/issues/1228): Enhance `Rails/SaveBang` to properly handle instance variables. ([@ydakuka][]) diff --git a/lib/rubocop/cop/rails/save_bang.rb b/lib/rubocop/cop/rails/save_bang.rb index 2d3ab932f1..fae2e90bc3 100644 --- a/lib/rubocop/cop/rails/save_bang.rb +++ b/lib/rubocop/cop/rails/save_bang.rb @@ -328,8 +328,9 @@ def explicit_return?(node) end def return_value_assigned?(node) - assignment = assignable_node(node).parent - assignment&.lvasgn_type? + return false unless (assignment = assignable_node(node).parent) + + assignment.assignment? end def persist_method?(node, methods = RESTRICT_ON_SEND) diff --git a/spec/rubocop/cop/rails/save_bang_spec.rb b/spec/rubocop/cop/rails/save_bang_spec.rb index 383ba34570..dfa9ca071f 100644 --- a/spec/rubocop/cop/rails/save_bang_spec.rb +++ b/spec/rubocop/cop/rails/save_bang_spec.rb @@ -668,6 +668,56 @@ def whatever RUBY end + it "does not register an offense when using persisted? after #{method} to a local variable" do + expect_no_offenses(<<~RUBY) + user = User.#{method} + + if user.persisted? + foo + end + RUBY + end + + it "does not register an offense when using persisted? after #{method} to an instance variable" do + expect_no_offenses(<<~RUBY) + @user = User.#{method} + + if @user.persisted? + foo + end + RUBY + end + + it "does not register an offense when using persisted? after #{method} to a global variable" do + expect_no_offenses(<<~RUBY) + $user = User.#{method} + + if $user.persisted? + foo + end + RUBY + end + + it "does not register an offense when using persisted? after #{method} for multiple assignments" do + expect_no_offenses(<<~RUBY) + a, b = User.#{method}, User.new + + if a.persisted? + foo + end + RUBY + end + + it "does not register an offense when using persisted? after #{method} for conditional assignments" do + expect_no_offenses(<<~RUBY) + user ||= User.#{method} + + if user.persisted? + foo + end + RUBY + end + it "when using #{method} with `||`" do expect_no_offenses(<<~RUBY) def find_or_create(**opts) From 334e5cc394afdac3bc36ad24ceac577fbbe557c2 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Sat, 18 Jan 2025 17:04:16 +0400 Subject: [PATCH 04/10] [Fix rubocop#1389] Handle TypeError caused by an array in Rails/RootPathname cop --- ...to_handle_type_error_caused_by_an_array.md | 1 + .../cop/rails/root_pathname_methods.rb | 7 +- .../cop/rails/root_pathname_methods_spec.rb | 66 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md diff --git a/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md b/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md new file mode 100644 index 0000000000..8c77d4b0b3 --- /dev/null +++ b/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md @@ -0,0 +1 @@ +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop. ([@ydakuka][]) diff --git a/lib/rubocop/cop/rails/root_pathname_methods.rb b/lib/rubocop/cop/rails/root_pathname_methods.rb index 5898d0b421..1678d51c47 100644 --- a/lib/rubocop/cop/rails/root_pathname_methods.rb +++ b/lib/rubocop/cop/rails/root_pathname_methods.rb @@ -237,7 +237,12 @@ def build_path_replacement(path, method, args) end replacement = "#{path_replacement}.#{method}" - replacement += "(#{args.map(&:source).join(', ')})" unless args.empty? + + if args.any? + formatted_args = args.map { |arg| arg.array_type? ? "*#{arg.source}" : arg.source } + replacement += "(#{formatted_args.join(', ')})" + end + replacement end diff --git a/spec/rubocop/cop/rails/root_pathname_methods_spec.rb b/spec/rubocop/cop/rails/root_pathname_methods_spec.rb index 82f00ea5da..77cb1b1218 100644 --- a/spec/rubocop/cop/rails/root_pathname_methods_spec.rb +++ b/spec/rubocop/cop/rails/root_pathname_methods_spec.rb @@ -210,4 +210,70 @@ file = Rails.root.join('docs', 'invoice.pdf').open RUBY end + + it 'registers an offense when using only [] syntax' do + expect_offense(<<~RUBY) + File.join(Rails.root, ['app', 'models']) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*['app', 'models'])`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*['app', 'models']) + RUBY + end + + it 'registers an offense when using a leading string and an array using [] syntax' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", ["models", "goober"]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *["models", "goober"])`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *["models", "goober"]) + RUBY + end + + it 'registers an offense when using an array using [] syntax and a trailing string' do + expect_offense(<<~RUBY) + File.join(Rails.root, ["app", "models"], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*["app", "models"], "goober")`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*["app", "models"], "goober") + RUBY + end + + it 'registers an offense when using only %w[] syntax' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models])`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models]) + RUBY + end + + it 'registers an offense when using a leading string and an array using %w[] syntax' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", %w[models goober]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *%w[models goober])`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *%w[models goober]) + RUBY + end + + it 'registers an offense when using an array using %w[] syntax and a trailing string' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models], "goober")`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models], "goober") + RUBY + end end From e48fec2660ef4e098faa3cdd2dc7ac0a68b512d0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 20 Jan 2025 18:54:40 +0900 Subject: [PATCH 05/10] [Fix #1423] Fix an error for `Rails/StrongParametersExpect` This PR fixes an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. Fixes #1423. --- .../fix_an_error_for_rails_strong_parameters_expect.md | 1 + lib/rubocop/cop/rails/strong_parameters_expect.rb | 2 +- spec/rubocop/cop/rails/strong_parameters_expect_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_error_for_rails_strong_parameters_expect.md diff --git a/changelog/fix_an_error_for_rails_strong_parameters_expect.md b/changelog/fix_an_error_for_rails_strong_parameters_expect.md new file mode 100644 index 0000000000..4db69500b6 --- /dev/null +++ b/changelog/fix_an_error_for_rails_strong_parameters_expect.md @@ -0,0 +1 @@ +* [#1423](https://github.com/rubocop/rubocop-rails/issues/1423): Fix an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. ([@koic][]) diff --git a/lib/rubocop/cop/rails/strong_parameters_expect.rb b/lib/rubocop/cop/rails/strong_parameters_expect.rb index 54b108ab6a..921c13940c 100644 --- a/lib/rubocop/cop/rails/strong_parameters_expect.rb +++ b/lib/rubocop/cop/rails/strong_parameters_expect.rb @@ -32,7 +32,7 @@ class StrongParametersExpect < Base def_node_matcher :params_require_permit, <<~PATTERN $(call $(call - (send nil? :params) :require _) :permit ...) + (send nil? :params) :require _) :permit _+) PATTERN def_node_matcher :params_permit_require, <<~PATTERN diff --git a/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb b/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb index f7758327ca..0a0bc30d24 100644 --- a/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb +++ b/spec/rubocop/cop/rails/strong_parameters_expect_spec.rb @@ -136,6 +136,12 @@ RUBY end + it 'does not register an offense when using `params.require(:target).permit`' do + expect_no_offenses(<<~RUBY) + params.require(:target).permit + RUBY + end + it 'does not register an offense when using `params[:name]`' do expect_no_offenses(<<~RUBY) params[:name] From 3677f7b533537fd6cfe759e556de07998e278d37 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Sat, 18 Jan 2025 17:04:16 +0400 Subject: [PATCH 06/10] [Fix rubocop#1389] Handle TypeError caused by an array in Rails/FilePath cop --- .rubocop_todo.yml | 2 +- ...to_handle_type_error_caused_by_an_array.md | 1 + lib/rubocop/cop/rails/file_path.rb | 24 ++- spec/rubocop/cop/rails/file_path_spec.rb | 156 ++++++++++++++++++ 4 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index acbcd95faa..fd8c1e3101 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -25,7 +25,7 @@ InternalAffairs/NodeDestructuring: # Offense count: 10 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 163 + Max: 179 # Offense count: 41 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. diff --git a/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md b/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md new file mode 100644 index 0000000000..6dc7e30608 --- /dev/null +++ b/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md @@ -0,0 +1 @@ +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/FilePath` cop. ([@ydakuka][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 9c19c5e525..375f43e7b0 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -173,7 +173,17 @@ def autocorrect_extension_after_rails_root_join_in_dstr(corrector, node, rails_r end def autocorrect_file_join(corrector, node) + replace_receiver_with_rails_root(corrector, node) + remove_first_argument_with_comma(corrector, node) + process_arguments(corrector, node.arguments) + append_to_string_conversion(corrector, node) + end + + def replace_receiver_with_rails_root(corrector, node) corrector.replace(node.receiver, 'Rails.root') + end + + def remove_first_argument_with_comma(corrector, node) corrector.remove( range_with_surrounding_space( range_with_surrounding_comma( @@ -183,9 +193,19 @@ def autocorrect_file_join(corrector, node) side: :right ) ) - node.arguments.filter(&:str_type?).each do |argument| - corrector.replace(argument, argument.value.delete_prefix('/').inspect) + end + + def process_arguments(corrector, arguments) + arguments.each do |argument| + if argument.str_type? + corrector.replace(argument, argument.value.delete_prefix('/').inspect) + elsif argument.array_type? + corrector.replace(argument, "*#{argument.source}") + end end + end + + def append_to_string_conversion(corrector, node) corrector.insert_after(node, '.to_s') end diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 04d4cfd4e9..6bcdd88dea 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -244,6 +244,84 @@ RUBY end end + + context 'when using only [] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, ['app', 'models']) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*['app', 'models']).to_s + RUBY + end + end + + context 'with a leading string and an array using [] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", ["models", "goober"]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *["models", "goober"]).to_s + RUBY + end + end + + context 'with an array using [] syntax and a trailing string' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, ["app", "models"], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*["app", "models"], "goober").to_s + RUBY + end + end + + context 'when using only %w[] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models]).to_s + RUBY + end + end + + context 'with a leading string and an array using %w[] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", %w[models goober]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *%w[models goober]).to_s + RUBY + end + end + + context 'with an array using %w[] syntax and a trailing string' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models], "goober").to_s + RUBY + end + end end context 'when EnforcedStyle is `arguments`' do @@ -436,5 +514,83 @@ RUBY end end + + context 'when using only [] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, ['app', 'models']) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*['app', 'models']).to_s + RUBY + end + end + + context 'with a leading string and an array using [] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", ["models", "goober"]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *["models", "goober"]).to_s + RUBY + end + end + + context 'with an array using [] syntax and a trailing string' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, ["app", "models"], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*["app", "models"], "goober").to_s + RUBY + end + end + + context 'when using only %w[] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models]).to_s + RUBY + end + end + + context 'with a leading string and an array using %w[] syntax' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, "app", %w[models goober]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join("app", *%w[models goober]).to_s + RUBY + end + end + + context 'with an array using %w[] syntax and a trailing string' do + it 'registers an offense once' do + expect_offense(<<~RUBY) + File.join(Rails.root, %w[app models], "goober") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. + RUBY + + expect_correction(<<~RUBY) + Rails.root.join(*%w[app models], "goober").to_s + RUBY + end + end end end From 10829d34ee786cf005ae1ddc6262a92d6168a707 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Sat, 18 Jan 2025 21:48:12 +0400 Subject: [PATCH 07/10] [Fix rubocop#1356] Enhance Rails/DuplicateAssociation to handle alias --- ...s_duplicate_association_to_handle_alias.md | 1 + .../cop/rails/duplicate_association.rb | 12 ++++-- .../cop/rails/duplicate_association_spec.rb | 39 ++++++++++++++----- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 changelog/fix_rails_duplicate_association_to_handle_alias.md diff --git a/changelog/fix_rails_duplicate_association_to_handle_alias.md b/changelog/fix_rails_duplicate_association_to_handle_alias.md new file mode 100644 index 0000000000..6386d925f5 --- /dev/null +++ b/changelog/fix_rails_duplicate_association_to_handle_alias.md @@ -0,0 +1 @@ +* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][]) diff --git a/lib/rubocop/cop/rails/duplicate_association.rb b/lib/rubocop/cop/rails/duplicate_association.rb index 0373fa4530..19b5d567fb 100644 --- a/lib/rubocop/cop/rails/duplicate_association.rb +++ b/lib/rubocop/cop/rails/duplicate_association.rb @@ -63,11 +63,15 @@ def on_class(class_node) private def register_offense(name, nodes, message_template) - nodes.each do |node| - add_offense(node, message: format(message_template, name: name)) do |corrector| - next if same_line?(nodes.last, node) + last_node = nodes.last - corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) + nodes.each_with_index do |node, index| + add_offense(node, message: format(message_template, name: name)) do |corrector| + if index.zero? + corrector.replace(node, last_node.source) + else + corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) + end end end end diff --git a/spec/rubocop/cop/rails/duplicate_association_spec.rb b/spec/rubocop/cop/rails/duplicate_association_spec.rb index 3fbe151ce2..72b6315c89 100644 --- a/spec/rubocop/cop/rails/duplicate_association_spec.rb +++ b/spec/rubocop/cop/rails/duplicate_association_spec.rb @@ -16,8 +16,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - belongs_to :bar belongs_to :foo + belongs_to :bar belongs_to :blah end RUBY @@ -37,8 +37,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - belongs_to :bar belongs_to :foo, -> { active } + belongs_to :bar belongs_to :blah end RUBY @@ -60,8 +60,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_many :bars has_many :foos + has_many :bars has_many :blahs end RUBY @@ -81,12 +81,31 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_many :bars has_many 'foos', -> { active } + has_many :bars has_many :blahs end RUBY end + + it 'registers an offense with alias' do + expect_offense(<<~RUBY) + class Post < ApplicationRecord + belongs_to :foos, -> { active } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. + alias bars foos + belongs_to :foos + ^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. + end + RUBY + + expect_correction(<<~RUBY) + class Post < ApplicationRecord + belongs_to :foos + alias bars foos + end + RUBY + end end describe 'has_one' do @@ -104,8 +123,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_one :bar has_one :foo + has_one :bar has_one :blah end RUBY @@ -125,8 +144,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_one :bar has_one :foo, -> { active } + has_one :bar has_one :blah end RUBY @@ -148,8 +167,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_and_belongs_to_many :bars has_and_belongs_to_many :foos + has_and_belongs_to_many :bars has_and_belongs_to_many :blahs end RUBY @@ -169,8 +188,8 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_and_belongs_to_many :bars has_and_belongs_to_many :foos, -> { active } + has_and_belongs_to_many :bars has_and_belongs_to_many :blahs end RUBY @@ -200,12 +219,12 @@ class Post < ApplicationRecord expect_correction(<<~RUBY) class Post < ApplicationRecord - has_and_belongs_to_many :bars has_and_belongs_to_many :foos, -> { active } + has_and_belongs_to_many :bars has_and_belongs_to_many :blahs - has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment' belongs_to :author + has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment' end RUBY end From c0f086f4e3a0984023a3ed17913e06f83b3ee751 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 23 Jan 2025 00:33:48 +0900 Subject: [PATCH 08/10] Apply `bundle exec rubocop --regenerate-todo` --- .rubocop_todo.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fd8c1e3101..098642cde0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-12-13 20:32:15 UTC using RuboCop version 1.69.2. +# on 2025-01-22 15:33:45 UTC using RuboCop version 1.70.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -22,6 +22,10 @@ InternalAffairs/NodeDestructuring: - 'lib/rubocop/cop/rails/relative_date_constant.rb' - 'lib/rubocop/cop/rails/time_zone.rb' +# Offense count: 84 +InternalAffairs/OnSendWithoutOnCSend: + Enabled: false + # Offense count: 10 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: @@ -32,17 +36,17 @@ Metrics/ClassLength: Metrics/MethodLength: Max: 14 -# Offense count: 178 +# Offense count: 183 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without RSpec/ContextWording: Enabled: false -# Offense count: 1035 +# Offense count: 1091 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 108 -# Offense count: 688 +# Offense count: 733 RSpec/MultipleExpectations: Max: 5 From 18f54841be51ba717d79ed6ebed14864a45c3322 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 25 Jan 2025 12:40:37 +0900 Subject: [PATCH 09/10] Update Changelog --- CHANGELOG.md | 10 ++++++++++ .../fix_an_error_for_rails_strong_parameters_expect.md | 1 - ...t_autocorrect_for_rails_strong_parameters_expect.md | 1 - .../fix_rails_duplicate_association_to_handle_alias.md | 1 - ...ile_path_to_handle_type_error_caused_by_an_array.md | 1 - ...pathname_to_handle_type_error_caused_by_an_array.md | 1 - ...fix_rails_save_bang_to_handle_instance_variables.md | 1 - 7 files changed, 10 insertions(+), 6 deletions(-) delete mode 100644 changelog/fix_an_error_for_rails_strong_parameters_expect.md delete mode 100644 changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md delete mode 100644 changelog/fix_rails_duplicate_association_to_handle_alias.md delete mode 100644 changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md delete mode 100644 changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md delete mode 100644 changelog/fix_rails_save_bang_to_handle_instance_variables.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed798e23f..e41782a9ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,15 @@ ## master (unreleased) +### Bug fixes + +* [#1423](https://github.com/rubocop/rubocop-rails/issues/1423): Fix an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. ([@koic][]) +* [#1417](https://github.com/rubocop/rubocop-rails/issues/1417): Fix an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `require` with `permit`. ([@koic][]) +* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][]) +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/FilePath` cop. ([@ydakuka][]) +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop. ([@ydakuka][]) +* [#1228](https://github.com/rubocop/rubocop-rails/issues/1228): Enhance `Rails/SaveBang` to properly handle instance variables. ([@ydakuka][]) + ## 2.29.0 (2025-01-18) ### New features @@ -1207,3 +1216,4 @@ [@viralpraxis]: https://github.com/viralpraxis [@mterada1228]: https://github.com/mterada1228 [@franzliedke]: https://github.com/franzliedke +[@ydakuka]: https://github.com/ydakuka diff --git a/changelog/fix_an_error_for_rails_strong_parameters_expect.md b/changelog/fix_an_error_for_rails_strong_parameters_expect.md deleted file mode 100644 index 4db69500b6..0000000000 --- a/changelog/fix_an_error_for_rails_strong_parameters_expect.md +++ /dev/null @@ -1 +0,0 @@ -* [#1423](https://github.com/rubocop/rubocop-rails/issues/1423): Fix an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. ([@koic][]) diff --git a/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md b/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md deleted file mode 100644 index 426ad18c94..0000000000 --- a/changelog/fix_an_incorrect_autocorrect_for_rails_strong_parameters_expect.md +++ /dev/null @@ -1 +0,0 @@ -* [#1417](https://github.com/rubocop/rubocop-rails/issues/1417): Fix an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `require` with `permit`. ([@koic][]) diff --git a/changelog/fix_rails_duplicate_association_to_handle_alias.md b/changelog/fix_rails_duplicate_association_to_handle_alias.md deleted file mode 100644 index 6386d925f5..0000000000 --- a/changelog/fix_rails_duplicate_association_to_handle_alias.md +++ /dev/null @@ -1 +0,0 @@ -* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][]) diff --git a/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md b/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md deleted file mode 100644 index 6dc7e30608..0000000000 --- a/changelog/fix_rails_file_path_to_handle_type_error_caused_by_an_array.md +++ /dev/null @@ -1 +0,0 @@ -* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/FilePath` cop. ([@ydakuka][]) diff --git a/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md b/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md deleted file mode 100644 index 8c77d4b0b3..0000000000 --- a/changelog/fix_rails_root_pathname_to_handle_type_error_caused_by_an_array.md +++ /dev/null @@ -1 +0,0 @@ -* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop. ([@ydakuka][]) diff --git a/changelog/fix_rails_save_bang_to_handle_instance_variables.md b/changelog/fix_rails_save_bang_to_handle_instance_variables.md deleted file mode 100644 index 6da9b2c2df..0000000000 --- a/changelog/fix_rails_save_bang_to_handle_instance_variables.md +++ /dev/null @@ -1 +0,0 @@ -* [#1228](https://github.com/rubocop/rubocop-rails/issues/1228): Enhance `Rails/SaveBang` to properly handle instance variables. ([@ydakuka][]) From a54b4084b3ea67edf17590149f45577063c270da Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 25 Jan 2025 12:40:52 +0900 Subject: [PATCH 10/10] Cut 2.29.1 --- CHANGELOG.md | 2 ++ docs/antora.yml | 2 +- lib/rubocop/rails/version.rb | 2 +- relnotes/v2.29.1.md | 11 +++++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 relnotes/v2.29.1.md diff --git a/CHANGELOG.md b/CHANGELOG.md index e41782a9ad..7ea73d5e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ## master (unreleased) +## 2.29.1 (2025-01-25) + ### Bug fixes * [#1423](https://github.com/rubocop/rubocop-rails/issues/1423): Fix an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. ([@koic][]) diff --git a/docs/antora.yml b/docs/antora.yml index 9e0ff48acb..5a69a42487 100644 --- a/docs/antora.yml +++ b/docs/antora.yml @@ -2,6 +2,6 @@ name: rubocop-rails title: RuboCop Rails # We always provide version without patch here (e.g. 1.1), # as patch versions should not appear in the docs. -version: ~ +version: '2.29' nav: - modules/ROOT/nav.adoc diff --git a/lib/rubocop/rails/version.rb b/lib/rubocop/rails/version.rb index 16caa42d21..981881947b 100644 --- a/lib/rubocop/rails/version.rb +++ b/lib/rubocop/rails/version.rb @@ -4,7 +4,7 @@ module RuboCop module Rails # This module holds the RuboCop Rails version information. module Version - STRING = '2.29.0' + STRING = '2.29.1' def self.document_version STRING.match('\d+\.\d+').to_s diff --git a/relnotes/v2.29.1.md b/relnotes/v2.29.1.md new file mode 100644 index 0000000000..bc4a60efd1 --- /dev/null +++ b/relnotes/v2.29.1.md @@ -0,0 +1,11 @@ +### Bug fixes + +* [#1423](https://github.com/rubocop/rubocop-rails/issues/1423): Fix an error for `Rails/StrongParametersExpect` when using `permit` with no arguments. ([@koic][]) +* [#1417](https://github.com/rubocop/rubocop-rails/issues/1417): Fix an incorrect autocorrect for `Rails/StrongParametersExpect` when using a leading dot multiline call to `require` with `permit`. ([@koic][]) +* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][]) +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/FilePath` cop. ([@ydakuka][]) +* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop. ([@ydakuka][]) +* [#1228](https://github.com/rubocop/rubocop-rails/issues/1228): Enhance `Rails/SaveBang` to properly handle instance variables. ([@ydakuka][]) + +[@koic]: https://github.com/koic +[@ydakuka]: https://github.com/ydakuka pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy