From b399a354108e84fa40c93ec7f61f455f2655a3c6 Mon Sep 17 00:00:00 2001 From: Vitali Tatarintev Date: Tue, 29 Mar 2022 10:34:32 +0200 Subject: [PATCH] Fix Style/EachWithObject offenses Fixes Style/EachWithObject offenses --- .rubocop_todo.yml | 10 ---------- lib/expand_variables.rb | 3 +-- lib/gitlab/ci/ansi2html.rb | 3 +-- lib/gitlab/hook_data/issuable_builder.rb | 5 +---- lib/gitlab/i18n/po_linter.rb | 3 +-- lib/gitlab/import_export/members_mapper.rb | 4 +--- 6 files changed, 5 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 697d07bd20cbbe..b1757ff87317a1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -309,16 +309,6 @@ Style/BarePercentLiterals: Style/CaseLikeIf: Enabled: false -# Offense count: 5 -# Cop supports --auto-correct. -Style/EachWithObject: - Exclude: - - 'lib/expand_variables.rb' - - 'lib/gitlab/ci/ansi2html.rb' - - 'lib/gitlab/hook_data/issuable_builder.rb' - - 'lib/gitlab/i18n/po_linter.rb' - - 'lib/gitlab/import_export/members_mapper.rb' - # Offense count: 55 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. diff --git a/lib/expand_variables.rb b/lib/expand_variables.rb index d172df4920f8b8..06160b55f5c79c 100644 --- a/lib/expand_variables.rb +++ b/lib/expand_variables.rb @@ -50,9 +50,8 @@ def transform_variables(variables) # Convert hash array to variables if variables.is_a?(Array) - variables = variables.reduce({}) do |hash, variable| + variables = variables.each_with_object({}) do |variable, hash| hash[variable[:key]] = variable[:value] - hash end end diff --git a/lib/gitlab/ci/ansi2html.rb b/lib/gitlab/ci/ansi2html.rb index ef936581c1046e..10233cf422882a 100644 --- a/lib/gitlab/ci/ansi2html.rb +++ b/lib/gitlab/ci/ansi2html.rb @@ -447,9 +447,8 @@ def reset_state end def state - state = STATE_PARAMS.inject({}) do |h, param| + state = STATE_PARAMS.each_with_object({}) do |param, h| h[param] = send(param) # rubocop:disable GitlabSecurity/PublicSend - h end Base64.urlsafe_encode64(state.to_json) end diff --git a/lib/gitlab/hook_data/issuable_builder.rb b/lib/gitlab/hook_data/issuable_builder.rb index c4e27bf424f8e9..add9e8804753b9 100644 --- a/lib/gitlab/hook_data/issuable_builder.rb +++ b/lib/gitlab/hook_data/issuable_builder.rb @@ -53,10 +53,7 @@ def issuable_builder end def final_changes(changes_hash) - changes_hash.reduce({}) do |hash, (key, changes_array)| - hash[key] = Hash[CHANGES_KEYS.zip(changes_array)] - hash - end + changes_hash.transform_values { |changes_array| Hash[CHANGES_KEYS.zip(changes_array)] } end end end diff --git a/lib/gitlab/i18n/po_linter.rb b/lib/gitlab/i18n/po_linter.rb index 3bb34ab28113d7..74be56df221c79 100644 --- a/lib/gitlab/i18n/po_linter.rb +++ b/lib/gitlab/i18n/po_linter.rb @@ -248,10 +248,9 @@ def fill_in_variables(variables) variable == '%d' ? Random.rand(1000) : Gitlab::Utils.random_string end else - variables.inject({}) do |hash, variable| + variables.each_with_object({}) do |variable, hash| variable_name = variable[/\w+/] hash[variable_name] = Gitlab::Utils.random_string - hash end end end diff --git a/lib/gitlab/import_export/members_mapper.rb b/lib/gitlab/import_export/members_mapper.rb index d3b1bb6a57dc6b..b1f2a17d4b7dbd 100644 --- a/lib/gitlab/import_export/members_mapper.rb +++ b/lib/gitlab/import_export/members_mapper.rb @@ -16,7 +16,7 @@ def initialize(exported_members:, user:, importable:) def map @map ||= begin - @exported_members.inject(missing_keys_tracking_hash) do |hash, member| + @exported_members.each_with_object(missing_keys_tracking_hash) do |member, hash| if member['user'] old_user_id = member['user']['id'] existing_user_id = existing_users_email_map[get_email(member)] @@ -24,8 +24,6 @@ def map else add_team_member(member) end - - hash end end end -- GitLab