From 0336235c74d201767c28454ceda12c517e577818 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 23 Dec 2024 08:44:29 +0100 Subject: [PATCH] DEV: Use object spread instead of Object.assign (#30407) --- .../discourse-common/addon/lib/object.js | 5 +---- .../discourse/app/lib/formatter.js | 2 +- .../discourse/app/lib/keyboard-shortcuts.js | 4 +--- .../javascripts/discourse/app/models/group.js | 2 +- .../tests/acceptance/composer-test.js | 20 ++++++++----------- .../discourse/components/chat-channel.gjs | 10 +++------- .../discourse/components/chat-thread.gjs | 17 ++++++++-------- .../javascripts/discourse/lib/fabricators.js | 2 +- .../javascripts/discourse/components/poll.gjs | 4 ++-- 9 files changed, 26 insertions(+), 40 deletions(-) diff --git a/app/assets/javascripts/discourse-common/addon/lib/object.js b/app/assets/javascripts/discourse-common/addon/lib/object.js index bea2109a1f8..ef51d669301 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/object.js +++ b/app/assets/javascripts/discourse-common/addon/lib/object.js @@ -14,10 +14,7 @@ export function deepMerge(...objects) { if (Array.isArray(targetValue) && Array.isArray(sourceValue)) { target[key] = targetValue.concat(sourceValue); } else if (isObject(targetValue) && isObject(sourceValue)) { - target[key] = deepMergeInner( - Object.assign({}, targetValue), - sourceValue - ); + target[key] = deepMergeInner({ ...targetValue }, sourceValue); } else { target[key] = sourceValue; } diff --git a/app/assets/javascripts/discourse/app/lib/formatter.js b/app/assets/javascripts/discourse/app/lib/formatter.js index f0a88bdf1c4..148e0c7f111 100644 --- a/app/assets/javascripts/discourse/app/lib/formatter.js +++ b/app/assets/javascripts/discourse/app/lib/formatter.js @@ -208,7 +208,7 @@ export function duration(distance, ageOpts) { } export function durationTiny(distance, ageOpts) { - return duration(distance, Object.assign({ format: "tiny" }, ageOpts)); + return duration(distance, { format: "tiny", ...ageOpts }); } function relativeAgeTiny(date, ageOpts) { diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js index f8cd9280289..1256dcc1467 100644 --- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js +++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js @@ -275,9 +275,7 @@ export default { addShortcut(shortcut, callback, opts = {}) { // we trim but leave whitespace between characters, as shortcuts // like `z z` are valid for ItsATrap - shortcut = shortcut.trim(); - let newBinding = Object.assign({ handler: callback }, opts); - this.bindKey(shortcut, newBinding); + this.bindKey(shortcut.trim(), { handler: callback, ...opts }); if (opts.help) { addExtraKeyboardShortcutHelp(opts.help); } diff --git a/app/assets/javascripts/discourse/app/models/group.js b/app/assets/javascripts/discourse/app/models/group.js index 2fc65d3b2b1..b8b36e11ec8 100644 --- a/app/assets/javascripts/discourse/app/models/group.js +++ b/app/assets/javascripts/discourse/app/models/group.js @@ -429,7 +429,7 @@ export default class Group extends RestModel { save(opts = {}) { return ajax(`/groups/${this.id}`, { type: "PUT", - data: Object.assign({ group: this.asJSON() }, opts), + data: { group: this.asJSON(), ...opts }, }); } diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js index c8d4233c25b..3e81e655bb5 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js @@ -1347,12 +1347,10 @@ acceptance("composer buttons API", function (needs) { const editor = document.querySelector(".d-editor-input"); editor.setSelectionRange(6, 9); // select the text input in the composer - await triggerKeyEvent( - ".d-editor-input", - "keydown", - "B", - Object.assign({ altKey: true }, metaModifier) - ); + await triggerKeyEvent(".d-editor-input", "keydown", "B", { + altKey: true, + ...metaModifier, + }); assert .dom(".d-editor-input") @@ -1401,12 +1399,10 @@ acceptance("composer buttons API", function (needs) { await click(".post-controls button.reply"); const editor = document.querySelector(".d-editor-input"); - await triggerKeyEvent( - ".d-editor-input", - "keydown", - "S", - Object.assign({ altKey: true }, metaModifier) - ); + await triggerKeyEvent(".d-editor-input", "keydown", "S", { + altKey: true, + ...metaModifier, + }); assert.dom(editor).hasValue(":smile: from keyboard"); }); diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs index bdaab6a9d9d..1cb2efe7895 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs @@ -558,17 +558,13 @@ export default class ChatChannel extends Component { } try { - const params = { + await this.chatApi.sendMessage(this.args.channel.id, { message: message.message, in_reply_to_id: message.inReplyTo?.id, staged_id: message.id, upload_ids: message.uploads.map((upload) => upload.id), - }; - - await this.chatApi.sendMessage( - this.args.channel.id, - Object.assign({}, params, extractCurrentTopicInfo(this)) - ); + ...extractCurrentTopicInfo(this), + }); if (!this.capabilities.isIOS) { this.scrollToLatestMessage(); diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs index b5020270b96..b5bdc774a0f 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs @@ -442,17 +442,16 @@ export default class ChatThread extends Component { } try { - const params = { - message: message.message, - in_reply_to_id: null, - staged_id: message.id, - upload_ids: message.uploads.map((upload) => upload.id), - thread_id: message.thread.id, - }; - const response = await this.chatApi.sendMessage( this.args.thread.channel.id, - Object.assign({}, params, extractCurrentTopicInfo(this)) + { + message: message.message, + in_reply_to_id: null, + staged_id: message.id, + upload_ids: message.uploads.map((upload) => upload.id), + thread_id: message.thread.id, + ...extractCurrentTopicInfo(this), + } ); this.args.thread.currentUserMembership ??= diff --git a/plugins/chat/assets/javascripts/discourse/lib/fabricators.js b/plugins/chat/assets/javascripts/discourse/lib/fabricators.js index ed5e8a9006a..655efe9392e 100644 --- a/plugins/chat/assets/javascripts/discourse/lib/fabricators.js +++ b/plugins/chat/assets/javascripts/discourse/lib/fabricators.js @@ -81,7 +81,7 @@ export default class ChatFabricators { status: args.status || CHANNEL_STATUSES.open, slug: chatable?.slug || chatable instanceof Category ? chatable.slug : null, - meta: Object.assign({ can_delete_self: true }, args.meta || {}), + meta: { can_delete_self: true, ...(args.meta || {}) }, archive_failed: args.archive_failed ?? false, memberships_count: args.memberships_count ?? 0, }); diff --git a/plugins/poll/assets/javascripts/discourse/components/poll.gjs b/plugins/poll/assets/javascripts/discourse/components/poll.gjs index 85b48724c1e..e326ee4d99c 100644 --- a/plugins/poll/assets/javascripts/discourse/components/poll.gjs +++ b/plugins/poll/assets/javascripts/discourse/components/poll.gjs @@ -479,7 +479,7 @@ export default class PollComponent extends Component { voters = preloadedVoters; } - this.preloadedVoters = Object.assign({}, preloadedVoters); + this.preloadedVoters = { ...preloadedVoters }; const votersCount = voters?.length; return ajax("/polls/voters.json", { @@ -535,7 +535,7 @@ export default class PollComponent extends Component { if (optionId) { preloadedVoters[optionId].loading = false; } - this.preloadedVoters = Object.assign({}, preloadedVoters); + this.preloadedVoters = { ...preloadedVoters }; }); }