From 4d8e4d54e53c4326a1fd2f1a8b070ec12b0b7b7b Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 17 Dec 2020 11:49:02 +0000 Subject: [PATCH 01/11] remove Array.from --- services/clsi/app/js/ResourceStateManager.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 07cc78571e..08a31683e3 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -6,7 +6,6 @@ // Fix any style issues and re-enable lint. /* * 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 @@ -56,12 +55,10 @@ module.exports = ResourceStateManager = { }) } else { logger.log({ state, basePath }, 'writing sync state') - const resourceList = Array.from(resources).map( - (resource) => resource.path - ) + const resourceList = resources.map((resource) => resource.path) return fs.writeFile( stateFile, - [...Array.from(resourceList), `stateHash:${state}`].join('\n'), + [...resourceList, `stateHash:${state}`].join('\n'), callback ) } @@ -104,7 +101,7 @@ module.exports = ResourceStateManager = { new Errors.FilesOutOfSyncError('invalid state for incremental update') ) } else { - const resources = Array.from(resourceList).map((path) => ({ path })) + const resources = resourceList.map((path) => ({ path })) return callback(null, resources) } }) @@ -116,7 +113,7 @@ module.exports = ResourceStateManager = { if (callback == null) { callback = function (error) {} } - for (file of Array.from(resources || [])) { + for (file of resources || []) { for (const dir of Array.from( __guard__(file != null ? file.path : undefined, (x) => x.split('/')) )) { @@ -127,10 +124,10 @@ module.exports = ResourceStateManager = { } // check if any of the input files are not present in list of files const seenFile = {} - for (file of Array.from(allFiles)) { + for (file of allFiles) { seenFile[file] = true } - const missingFiles = Array.from(resources) + const missingFiles = resources .filter((resource) => !seenFile[resource.path]) .map((resource) => resource.path) if ((missingFiles != null ? missingFiles.length : undefined) > 0) { @@ -146,7 +143,7 @@ module.exports = ResourceStateManager = { } else { return callback() } - } + }, } function __guard__(value, transform) { From 5a539b7f90d312dd340c3bdc7b4910d334a93d27 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 17 Dec 2020 11:59:15 +0000 Subject: [PATCH 02/11] remove guard function --- services/clsi/app/js/ResourceStateManager.js | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 08a31683e3..ef7fd430a7 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -7,7 +7,6 @@ /* * decaffeinate suggestions: * 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 @@ -84,10 +83,7 @@ module.exports = ResourceStateManager = { 'project state file truncated' ) } - const array = - __guard__(result != null ? result.toString() : undefined, (x) => - x.split('\n') - ) || [] + const array = result.toString().split('\n') const adjustedLength = Math.max(array.length, 1) const resourceList = array.slice(0, adjustedLength - 1) const oldState = array[adjustedLength - 1] @@ -114,10 +110,9 @@ module.exports = ResourceStateManager = { callback = function (error) {} } for (file of resources || []) { - for (const dir of Array.from( - __guard__(file != null ? file.path : undefined, (x) => x.split('/')) - )) { - if (dir === '..') { + if (file && file.path) { + const dirs = file.path.split('/') + if (dirs.indexOf('..') !== -1) { return callback(new Error('relative path in resource file list')) } } @@ -145,9 +140,3 @@ module.exports = ResourceStateManager = { } }, } - -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} From 299d7cc5fe5927525c3ce9d94886c23846eb064c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 17 Dec 2020 12:01:14 +0000 Subject: [PATCH 03/11] remove unnecessary returns --- services/clsi/app/js/ResourceStateManager.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index ef7fd430a7..c2b65be4cc 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -6,7 +6,6 @@ // Fix any style issues and re-enable lint. /* * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns * DS201: Simplify complex destructure assignments * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md @@ -45,7 +44,7 @@ module.exports = ResourceStateManager = { if (state == null) { // remove the file if no state passed in logger.log({ state, basePath }, 'clearing sync state') - return fs.unlink(stateFile, function (err) { + fs.unlink(stateFile, function (err) { if (err != null && err.code !== 'ENOENT') { return callback(err) } else { @@ -55,7 +54,7 @@ module.exports = ResourceStateManager = { } else { logger.log({ state, basePath }, 'writing sync state') const resourceList = resources.map((resource) => resource.path) - return fs.writeFile( + fs.writeFile( stateFile, [...resourceList, `stateHash:${state}`].join('\n'), callback @@ -69,7 +68,7 @@ module.exports = ResourceStateManager = { } const stateFile = Path.join(basePath, this.SYNC_STATE_FILE) const size = this.SYNC_STATE_MAX_SIZE - return SafeReader.readFile(stateFile, size, 'utf8', function ( + SafeReader.readFile(stateFile, size, 'utf8', function ( err, result, bytesRead @@ -98,7 +97,7 @@ module.exports = ResourceStateManager = { ) } else { const resources = resourceList.map((path) => ({ path })) - return callback(null, resources) + callback(null, resources) } }) }, @@ -136,7 +135,7 @@ module.exports = ResourceStateManager = { ) ) } else { - return callback() + callback() } }, } From 64ea22d259e9d69bfbae6940dafc22cdf590a5b3 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 17 Dec 2020 12:05:17 +0000 Subject: [PATCH 04/11] remove unnecessary null checks --- services/clsi/app/js/ResourceStateManager.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index c2b65be4cc..42e06d3a24 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -7,7 +7,6 @@ /* * decaffeinate suggestions: * 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 ResourceStateManager @@ -37,15 +36,12 @@ module.exports = ResourceStateManager = { SYNC_STATE_MAX_SIZE: 128 * 1024, saveProjectState(state, resources, basePath, callback) { - if (callback == null) { - callback = function (error) {} - } const stateFile = Path.join(basePath, this.SYNC_STATE_FILE) if (state == null) { // remove the file if no state passed in logger.log({ state, basePath }, 'clearing sync state') fs.unlink(stateFile, function (err) { - if (err != null && err.code !== 'ENOENT') { + if (err && err.code !== 'ENOENT') { return callback(err) } else { return callback() @@ -63,9 +59,6 @@ module.exports = ResourceStateManager = { }, checkProjectStateMatches(state, basePath, callback) { - if (callback == null) { - callback = function (error, resources) {} - } const stateFile = Path.join(basePath, this.SYNC_STATE_FILE) const size = this.SYNC_STATE_MAX_SIZE SafeReader.readFile(stateFile, size, 'utf8', function ( @@ -73,7 +66,7 @@ module.exports = ResourceStateManager = { result, bytesRead ) { - if (err != null) { + if (err) { return callback(err) } if (bytesRead === size) { @@ -105,9 +98,6 @@ module.exports = ResourceStateManager = { checkResourceFiles(resources, allFiles, basePath, callback) { // check the paths are all relative to current directory let file - if (callback == null) { - callback = function (error) {} - } for (file of resources || []) { if (file && file.path) { const dirs = file.path.split('/') @@ -124,7 +114,7 @@ module.exports = ResourceStateManager = { const missingFiles = resources .filter((resource) => !seenFile[resource.path]) .map((resource) => resource.path) - if ((missingFiles != null ? missingFiles.length : undefined) > 0) { + if (missingFiles && missingFiles.length > 0) { logger.err( { missingFiles, basePath, allFiles, resources }, 'missing input files for project' From 43b0429c289788f6e2c129808093ae427db651b6 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 14:51:46 +0000 Subject: [PATCH 05/11] clean up relative path checking --- services/clsi/app/js/ResourceStateManager.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 42e06d3a24..cd7f164a49 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -98,13 +98,12 @@ module.exports = ResourceStateManager = { checkResourceFiles(resources, allFiles, basePath, callback) { // check the paths are all relative to current directory let file - for (file of resources || []) { - if (file && file.path) { - const dirs = file.path.split('/') - if (dirs.indexOf('..') !== -1) { - return callback(new Error('relative path in resource file list')) - } - } + const containsRelativePath = (resource) => { + const dirs = resource.path.split('/') + return dirs.indexOf('..') !== -1 + } + if (resources.some(containsRelativePath)) { + return callback(new Error('relative path in resource file list')) } // check if any of the input files are not present in list of files const seenFile = {} From ca98ee5cffac62131253d631af581b5d321795f1 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 14:52:04 +0000 Subject: [PATCH 06/11] use a Set instead of an Object --- services/clsi/app/js/ResourceStateManager.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index cd7f164a49..2508302335 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -106,14 +106,11 @@ module.exports = ResourceStateManager = { return callback(new Error('relative path in resource file list')) } // check if any of the input files are not present in list of files - const seenFile = {} - for (file of allFiles) { - seenFile[file] = true - } + const seenFiles = new Set(allFiles) const missingFiles = resources - .filter((resource) => !seenFile[resource.path]) .map((resource) => resource.path) - if (missingFiles && missingFiles.length > 0) { + .filter((path) => !seenFiles.has(path)) + if (missingFiles.length > 0) { logger.err( { missingFiles, basePath, allFiles, resources }, 'missing input files for project' From df1caa14b800c5555cd75f2911c8d6a5ac680308 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 15:44:25 +0000 Subject: [PATCH 07/11] remove unused require --- services/clsi/app/js/ResourceStateManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 2508302335..76903f17df 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -13,7 +13,6 @@ let ResourceStateManager const Path = require('path') const fs = require('fs') const logger = require('logger-sharelatex') -const settings = require('settings-sharelatex') const Errors = require('./Errors') const SafeReader = require('./SafeReader') From f20a0d1a6968917549c528960897fd65dbf13f14 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 15:45:04 +0000 Subject: [PATCH 08/11] remove unused var --- services/clsi/app/js/ResourceStateManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 76903f17df..3407f04987 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -96,7 +96,6 @@ module.exports = ResourceStateManager = { checkResourceFiles(resources, allFiles, basePath, callback) { // check the paths are all relative to current directory - let file const containsRelativePath = (resource) => { const dirs = resource.path.split('/') return dirs.indexOf('..') !== -1 From 36b646bb84c3c11c3de86b95e604cb76af9a027f Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 15:45:20 +0000 Subject: [PATCH 09/11] remove comments --- services/clsi/app/js/ResourceStateManager.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 3407f04987..983672ffda 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -1,14 +1,3 @@ -/* eslint-disable - handle-callback-err, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -/* - * decaffeinate suggestions: - * DS201: Simplify complex destructure assignments - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ let ResourceStateManager const Path = require('path') const fs = require('fs') From dafe69b5e6b5ba43e657d5e9881a32efbaccdb36 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 15:46:16 +0000 Subject: [PATCH 10/11] remove unused module var --- services/clsi/app/js/ResourceStateManager.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 983672ffda..7bd9df9d8f 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -1,11 +1,10 @@ -let ResourceStateManager const Path = require('path') const fs = require('fs') const logger = require('logger-sharelatex') const Errors = require('./Errors') const SafeReader = require('./SafeReader') -module.exports = ResourceStateManager = { +module.exports = { // The sync state is an identifier which must match for an // incremental update to be allowed. // From e183fffa4bc3b63a77d83f8dc972f1f5efdae26d Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2020 15:46:36 +0000 Subject: [PATCH 11/11] format fix --- services/clsi/app/js/ResourceStateManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/clsi/app/js/ResourceStateManager.js b/services/clsi/app/js/ResourceStateManager.js index 7bd9df9d8f..c77c254b05 100644 --- a/services/clsi/app/js/ResourceStateManager.js +++ b/services/clsi/app/js/ResourceStateManager.js @@ -109,5 +109,5 @@ module.exports = { } else { callback() } - }, + } }