From 8960e56e2096b1dc33d3fc1396163bc4cac997bc Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 28 Jul 2022 09:23:50 +0100 Subject: [PATCH] Merge pull request #9020 from overleaf/bg-decaff-guard decaff cleanup of __guard__ calls GitOrigin-RevId: 0d61e4d44a2fda19285674040ba92e500deae78d --- .../clsi/app/js/StaticServerForbidSymlinks.js | 7 ----- .../document-updater/app/js/HistoryManager.js | 28 ++----------------- .../app/js/LoggerSerializers.js | 22 ++------------- .../app/js/ProjectHistoryRedisManager.js | 12 +------- .../track-changes/app/js/UpdateCompressor.js | 14 +--------- .../track-changes/app/js/UpdateTrimmer.js | 14 +--------- .../track-changes/app/js/UpdatesManager.js | 7 +---- .../src/Features/Exports/ExportsHandler.js | 12 +------- .../Features/Project/ProjectHistoryHandler.js | 18 +----------- services/web/app/src/Features/V1/V1Handler.js | 12 +------- 10 files changed, 13 insertions(+), 133 deletions(-) diff --git a/services/clsi/app/js/StaticServerForbidSymlinks.js b/services/clsi/app/js/StaticServerForbidSymlinks.js index cde0ac3866..8468b771d1 100644 --- a/services/clsi/app/js/StaticServerForbidSymlinks.js +++ b/services/clsi/app/js/StaticServerForbidSymlinks.js @@ -10,7 +10,6 @@ * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -85,9 +84,3 @@ module.exports = ForbidSymlinks = function (staticFn, root, options) { }) } } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/document-updater/app/js/HistoryManager.js b/services/document-updater/app/js/HistoryManager.js index 97b7c6de96..ba555a02f9 100644 --- a/services/document-updater/app/js/HistoryManager.js +++ b/services/document-updater/app/js/HistoryManager.js @@ -6,7 +6,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -73,12 +72,7 @@ module.exports = HistoryManager = { // flush changes in the background flushProjectChangesAsync(project_id) { - if ( - !__guard__( - Settings.apis != null ? Settings.apis.project_history : undefined, - x => x.enabled - ) - ) { + if (!Settings.apis?.project_history?.enabled) { return } return HistoryManager.flushProjectChanges( @@ -93,12 +87,7 @@ module.exports = HistoryManager = { if (callback == null) { callback = function () {} } - if ( - !__guard__( - Settings.apis != null ? Settings.apis.project_history : undefined, - x => x.enabled - ) - ) { + if (!Settings.apis?.project_history?.enabled) { return callback() } if (options.skip_history_flush) { @@ -153,12 +142,7 @@ module.exports = HistoryManager = { } // record updates for project history - if ( - __guard__( - Settings.apis != null ? Settings.apis.project_history : undefined, - x => x.enabled - ) - ) { + if (Settings.apis?.project_history?.enabled) { if ( HistoryManager.shouldFlushHistoryOps( project_ops_length, @@ -260,9 +244,3 @@ module.exports = HistoryManager = { ) }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/document-updater/app/js/LoggerSerializers.js b/services/document-updater/app/js/LoggerSerializers.js index ac9605e757..3aa8bfc387 100644 --- a/services/document-updater/app/js/LoggerSerializers.js +++ b/services/document-updater/app/js/LoggerSerializers.js @@ -6,7 +6,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -24,22 +23,13 @@ const showUpdateLength = function (update) { if ((update != null ? update.op : undefined) instanceof Array) { const copy = _.cloneDeep(update) copy.op.forEach(function (element, index) { - if ( - __guard__(element != null ? element.i : undefined, x => x.length) != - null - ) { + if (element?.i?.length != null) { copy.op[index].i = element.i.length } - if ( - __guard__(element != null ? element.d : undefined, x1 => x1.length) != - null - ) { + if (element?.d?.length != null) { copy.op[index].d = element.d.length } - if ( - __guard__(element != null ? element.c : undefined, x2 => x2.length) != - null - ) { + if (element?.c?.length != null) { return (copy.op[index].c = element.c.length) } }) @@ -59,9 +49,3 @@ module.exports = { ranges: showLength, update: showUpdateLength, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/document-updater/app/js/ProjectHistoryRedisManager.js b/services/document-updater/app/js/ProjectHistoryRedisManager.js index 2dedb54c0b..1d1ed3eb50 100644 --- a/services/document-updater/app/js/ProjectHistoryRedisManager.js +++ b/services/document-updater/app/js/ProjectHistoryRedisManager.js @@ -7,17 +7,13 @@ * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS201: Simplify complex destructure assignments * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ let ProjectHistoryRedisManager const Settings = require('@overleaf/settings') -const projectHistoryKeys = __guard__( - Settings.redis != null ? Settings.redis.project_history : undefined, - x => x.key_schema -) +const projectHistoryKeys = Settings.redis?.project_history?.key_schema const rclient = require('@overleaf/redis-wrapper').createClient( Settings.redis.project_history ) @@ -176,9 +172,3 @@ module.exports = ProjectHistoryRedisManager = { return ProjectHistoryRedisManager.queueOps(project_id, jsonUpdate, callback) }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/track-changes/app/js/UpdateCompressor.js b/services/track-changes/app/js/UpdateCompressor.js index 343fa81f95..69d10586bb 100644 --- a/services/track-changes/app/js/UpdateCompressor.js +++ b/services/track-changes/app/js/UpdateCompressor.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS101: Remove unnecessary use of Array.from - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -93,12 +92,7 @@ module.exports = UpdateCompressor = { }, compressRawUpdates(lastPreviousUpdate, rawUpdates) { - if ( - __guard__( - lastPreviousUpdate != null ? lastPreviousUpdate.op : undefined, - x => x.length - ) > 1 - ) { + if (lastPreviousUpdate?.op?.length > 1) { // if the last previous update was an array op, don't compress onto it. // The avoids cases where array length changes but version number doesn't return [lastPreviousUpdate].concat( @@ -331,9 +325,3 @@ module.exports = UpdateCompressor = { return ops }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/track-changes/app/js/UpdateTrimmer.js b/services/track-changes/app/js/UpdateTrimmer.js index 5481b5f487..4ef953a05e 100644 --- a/services/track-changes/app/js/UpdateTrimmer.js +++ b/services/track-changes/app/js/UpdateTrimmer.js @@ -7,7 +7,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -37,12 +36,7 @@ module.exports = UpdateTrimmer = { return callback(error) } logger.debug({ project_id, details }, 'got details') - if ( - __guard__( - details != null ? details.features : undefined, - x => x.versioning - ) - ) { + if (details?.features?.versioning) { return MongoManager.setProjectMetaData( project_id, { preserveHistory: true }, @@ -71,9 +65,3 @@ module.exports = UpdateTrimmer = { ) }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/track-changes/app/js/UpdatesManager.js b/services/track-changes/app/js/UpdatesManager.js index df8ebff6cd..fae1b8f0e7 100644 --- a/services/track-changes/app/js/UpdatesManager.js +++ b/services/track-changes/app/js/UpdatesManager.js @@ -99,12 +99,7 @@ module.exports = UpdatesManager = { } if (rawUpdates[0] != null && rawUpdates[0].v !== lastVersion + 1) { - const ts = __guard__( - lastCompressedUpdate != null - ? lastCompressedUpdate.meta - : undefined, - x1 => x1.end_ts - ) + const ts = lastCompressedUpdate?.meta?.end_ts const last_timestamp = ts != null ? new Date(ts) : 'unknown time' error = new Error( `Tried to apply raw op at version ${rawUpdates[0].v} to last compressed update with version ${lastVersion} from ${last_timestamp}` diff --git a/services/web/app/src/Features/Exports/ExportsHandler.js b/services/web/app/src/Features/Exports/ExportsHandler.js index 71710268e1..4e7ac859a6 100644 --- a/services/web/app/src/Features/Exports/ExportsHandler.js +++ b/services/web/app/src/Features/Exports/ExportsHandler.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -133,10 +132,7 @@ module.exports = ExportsHandler = { project: { id: project_id, rootDocPath: rootDoc[1] != null ? rootDoc[1].fileSystem : undefined, - historyId: __guard__( - project.overleaf != null ? project.overleaf.history : undefined, - x => x.id - ), + historyId: project.overleaf?.history?.id, historyVersion, v1ProjectId: project.overleaf != null ? project.overleaf.id : undefined, @@ -287,9 +283,3 @@ module.exports = ExportsHandler = { ) }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} diff --git a/services/web/app/src/Features/Project/ProjectHistoryHandler.js b/services/web/app/src/Features/Project/ProjectHistoryHandler.js index 0d7fa05116..3ae185c668 100644 --- a/services/web/app/src/Features/Project/ProjectHistoryHandler.js +++ b/services/web/app/src/Features/Project/ProjectHistoryHandler.js @@ -9,7 +9,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -56,16 +55,7 @@ const ProjectHistoryHandler = { if (err != null) { return callback(err) } // n.b. getDetails returns an error if the project doesn't exist - return callback( - null, - __guard__( - __guard__( - project != null ? project.overleaf : undefined, - x1 => x1.history - ), - x => x.id - ) - ) + return callback(null, project?.overleaf?.history?.id) } ) }, @@ -195,11 +185,5 @@ const ProjectHistoryHandler = { }, } -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} - ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler) module.exports = ProjectHistoryHandler diff --git a/services/web/app/src/Features/V1/V1Handler.js b/services/web/app/src/Features/V1/V1Handler.js index 2fd3481fbf..8a7f682600 100644 --- a/services/web/app/src/Features/V1/V1Handler.js +++ b/services/web/app/src/Features/V1/V1Handler.js @@ -8,7 +8,6 @@ /* * decaffeinate suggestions: * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ @@ -41,10 +40,7 @@ module.exports = V1Handler = { { email, isValid, - v1UserId: __guard__( - body != null ? body.user_profile : undefined, - x => x.id - ), + v1UserId: body?.user_profile?.id, }, '[V1Handler] got response from v1 login api' ) @@ -93,9 +89,3 @@ module.exports = V1Handler = { ) }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -}