Explorar o código

decaf cleanp: consider shorter variations of null checks

GitOrigin-RevId: 393297eb7d674e2c801701db87e5d7eb876186f0
Tim Alby %!s(int64=4) %!d(string=hai) anos
pai
achega
efc131175c

+ 0 - 1
services/chat/app.js

@@ -1,7 +1,6 @@
 /*
  * decaffeinate suggestions:
  * 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
  */
 const logger = require('@overleaf/logger')

+ 5 - 6
services/chat/app/js/Features/Messages/MessageFormatter.js

@@ -3,13 +3,12 @@
 /*
  * decaffeinate suggestions:
  * DS101: Remove unnecessary use of Array.from
- * DS207: Consider shorter variations of null checks
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
 let MessageFormatter
 module.exports = MessageFormatter = {
   formatMessageForClientSide(message) {
-    if (message._id != null) {
+    if (message._id) {
       message.id = message._id.toString()
       delete message._id
     }
@@ -19,7 +18,7 @@ module.exports = MessageFormatter = {
       timestamp: message.timestamp,
       user_id: message.user_id,
     }
-    if (message.edited_at != null) {
+    if (message.edited_at) {
       formattedMessage.edited_at = message.edited_at
     }
     return formattedMessage
@@ -41,11 +40,11 @@ module.exports = MessageFormatter = {
     const threads = {}
     const getThread = function (room) {
       const threadId = room.thread_id.toString()
-      if (threads[threadId] != null) {
+      if (threads[threadId]) {
         return threads[threadId]
       } else {
         const thread = { messages: [] }
-        if (room.resolved != null) {
+        if (room.resolved) {
           thread.resolved = true
           thread.resolved_at = room.resolved.ts
           thread.resolved_by_user_id = room.resolved.user_id
@@ -57,7 +56,7 @@ module.exports = MessageFormatter = {
 
     for (const message of Array.from(messages)) {
       room = roomsById[message.room_id.toString()]
-      if (room != null) {
+      if (room) {
         thread = getThread(room)
         thread.messages.push(
           MessageFormatter.formatMessageForClientSide(message)

+ 19 - 26
services/chat/app/js/Features/Messages/MessageHttpController.js

@@ -1,10 +1,3 @@
-// TODO: This file was created by bulk-decaffeinate.
-// Fix any style issues and re-enable lint.
-/*
- * decaffeinate suggestions:
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
 let MessageHttpController
 const logger = require('@overleaf/logger')
 const MessageManager = require('./MessageManager')
@@ -42,14 +35,14 @@ module.exports = MessageHttpController = {
     const { projectId } = req.params
     logger.log({ projectId }, 'getting all threads')
     ThreadManager.findAllThreadRooms(projectId, function (error, rooms) {
-      if (error != null) {
+      if (error) {
         return next(error)
       }
       const roomIds = rooms.map(r => r._id)
       MessageManager.findAllMessagesInRooms(
         roomIds,
         function (error, messages) {
-          if (error != null) {
+          if (error) {
             return next(error)
           }
           const threads = MessageFormatter.groupMessagesByThreads(
@@ -67,7 +60,7 @@ module.exports = MessageHttpController = {
     const { user_id: userId } = req.body
     logger.log({ userId, projectId, threadId }, 'marking thread as resolved')
     ThreadManager.resolveThread(projectId, threadId, userId, function (error) {
-      if (error != null) {
+      if (error) {
         return next(error)
       }
       res.sendStatus(204)
@@ -78,7 +71,7 @@ module.exports = MessageHttpController = {
     const { projectId, threadId } = req.params
     logger.log({ projectId, threadId }, 'reopening thread')
     ThreadManager.reopenThread(projectId, threadId, function (error) {
-      if (error != null) {
+      if (error) {
         return next(error)
       }
       res.sendStatus(204)
@@ -89,11 +82,11 @@ module.exports = MessageHttpController = {
     const { projectId, threadId } = req.params
     logger.log({ projectId, threadId }, 'deleting thread')
     ThreadManager.deleteThread(projectId, threadId, function (error, roomId) {
-      if (error != null) {
+      if (error) {
         return next(error)
       }
       MessageManager.deleteAllMessagesInRoom(roomId, function (error) {
-        if (error != null) {
+        if (error) {
           return next(error)
         }
         res.sendStatus(204)
@@ -102,14 +95,14 @@ module.exports = MessageHttpController = {
   }, // No content
 
   editMessage(req, res, next) {
-    const { content } = req != null ? req.body : undefined
+    const { content } = req.body
     const { projectId, threadId, messageId } = req.params
     logger.log({ projectId, threadId, messageId, content }, 'editing message')
     ThreadManager.findOrCreateThread(
       projectId,
       threadId,
       function (error, room) {
-        if (error != null) {
+        if (error) {
           return next(error)
         }
         MessageManager.updateMessage(
@@ -118,7 +111,7 @@ module.exports = MessageHttpController = {
           content,
           Date.now(),
           function (error) {
-            if (error != null) {
+            if (error) {
               return next(error)
             }
             res.sendStatus(204)
@@ -135,14 +128,14 @@ module.exports = MessageHttpController = {
       projectId,
       threadId,
       function (error, room) {
-        if (error != null) {
+        if (error) {
           return next(error)
         }
         MessageManager.deleteMessage(
           room._id,
           messageId,
           function (error, message) {
-            if (error != null) {
+            if (error) {
               return next(error)
             }
             res.sendStatus(204)
@@ -153,12 +146,12 @@ module.exports = MessageHttpController = {
   },
 
   _sendMessage(clientThreadId, req, res, next) {
-    const { user_id: userId, content } = req != null ? req.body : undefined
+    const { user_id: userId, content } = req.body
     const { projectId } = req.params
     if (!ObjectId.isValid(userId)) {
       return res.status(400).send('Invalid userId')
     }
-    if (content == null) {
+    if (!content) {
       return res.status(400).send('No content provided')
     }
     if (content.length > this.MAX_MESSAGE_LENGTH) {
@@ -174,7 +167,7 @@ module.exports = MessageHttpController = {
       projectId,
       clientThreadId,
       function (error, thread) {
-        if (error != null) {
+        if (error) {
           return next(error)
         }
         MessageManager.createMessage(
@@ -183,7 +176,7 @@ module.exports = MessageHttpController = {
           content,
           Date.now(),
           function (error, message) {
-            if (error != null) {
+            if (error) {
               return next(error)
             }
             message = MessageFormatter.formatMessageForClientSide(message)
@@ -198,12 +191,12 @@ module.exports = MessageHttpController = {
   _getMessages(clientThreadId, req, res, next) {
     let before, limit
     const { projectId } = req.params
-    if ((req.query != null ? req.query.before : undefined) != null) {
+    if (req.query.before) {
       before = parseInt(req.query.before, 10)
     } else {
       before = null
     }
-    if ((req.query != null ? req.query.limit : undefined) != null) {
+    if (req.query.limit) {
       limit = parseInt(req.query.limit, 10)
     } else {
       limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT
@@ -216,7 +209,7 @@ module.exports = MessageHttpController = {
       projectId,
       clientThreadId,
       function (error, thread) {
-        if (error != null) {
+        if (error) {
           return next(error)
         }
         const threadObjectId = thread._id
@@ -229,7 +222,7 @@ module.exports = MessageHttpController = {
           limit,
           before,
           function (error, messages) {
-            if (error != null) {
+            if (error) {
               return next(error)
             }
             messages = MessageFormatter.formatMessagesForClientSide(messages)

+ 10 - 17
services/chat/app/js/Features/Messages/MessageManager.js

@@ -1,10 +1,3 @@
-// TODO: This file was created by bulk-decaffeinate.
-// Fix any style issues and re-enable lint.
-/*
- * decaffeinate suggestions:
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
 let MessageManager
 const { db, ObjectId } = require('../../mongodb')
 const metrics = require('@overleaf/metrics')
@@ -12,7 +5,7 @@ const logger = require('@overleaf/logger')
 
 module.exports = MessageManager = {
   createMessage(roomId, userId, content, timestamp, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     let newMessageOpts = {
@@ -32,11 +25,11 @@ module.exports = MessageManager = {
   },
 
   getMessages(roomId, limit, before, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     let query = { room_id: roomId }
-    if (before != null) {
+    if (before) {
       query.timestamp = { $lt: before }
     }
     query = this._ensureIdsAreObjectIds(query)
@@ -48,7 +41,7 @@ module.exports = MessageManager = {
   },
 
   findAllMessagesInRooms(roomIds, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     db.messages
@@ -59,7 +52,7 @@ module.exports = MessageManager = {
   },
 
   deleteAllMessagesInRoom(roomId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     db.messages.deleteMany(
@@ -71,7 +64,7 @@ module.exports = MessageManager = {
   },
 
   updateMessage(roomId, messageId, content, timestamp, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     const query = this._ensureIdsAreObjectIds({
@@ -91,7 +84,7 @@ module.exports = MessageManager = {
   },
 
   deleteMessage(roomId, messageId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     const query = this._ensureIdsAreObjectIds({
@@ -102,13 +95,13 @@ module.exports = MessageManager = {
   },
 
   _ensureIdsAreObjectIds(query) {
-    if (query.user_id != null && !(query.user_id instanceof ObjectId)) {
+    if (query.user_id && !(query.user_id instanceof ObjectId)) {
       query.user_id = ObjectId(query.user_id)
     }
-    if (query.room_id != null && !(query.room_id instanceof ObjectId)) {
+    if (query.room_id && !(query.room_id instanceof ObjectId)) {
       query.room_id = ObjectId(query.room_id)
     }
-    if (query._id != null && !(query._id instanceof ObjectId)) {
+    if (query._id && !(query._id instanceof ObjectId)) {
       query._id = ObjectId(query._id)
     }
     return query

+ 8 - 15
services/chat/app/js/Features/Threads/ThreadManager.js

@@ -1,10 +1,3 @@
-// TODO: This file was created by bulk-decaffeinate.
-// Fix any style issues and re-enable lint.
-/*
- * decaffeinate suggestions:
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
 let ThreadManager
 const { db, ObjectId } = require('../../mongodb')
 const logger = require('@overleaf/logger')
@@ -15,7 +8,7 @@ module.exports = ThreadManager = {
 
   findOrCreateThread(projectId, threadId, callback) {
     let query, update
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     projectId = ObjectId(projectId.toString())
@@ -47,7 +40,7 @@ module.exports = ThreadManager = {
       { $set: update },
       { upsert: true, returnDocument: 'after' },
       function (error, result) {
-        if (error != null) {
+        if (error) {
           return callback(error)
         }
         callback(null, result.value)
@@ -56,7 +49,7 @@ module.exports = ThreadManager = {
   },
 
   findAllThreadRooms(projectId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     db.rooms
@@ -74,7 +67,7 @@ module.exports = ThreadManager = {
   },
 
   resolveThread(projectId, threadId, userId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     db.rooms.updateOne(
@@ -95,7 +88,7 @@ module.exports = ThreadManager = {
   },
 
   reopenThread(projectId, threadId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     db.rooms.updateOne(
@@ -113,11 +106,11 @@ module.exports = ThreadManager = {
   },
 
   deleteThread(projectId, threadId, callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     this.findOrCreateThread(projectId, threadId, function (error, room) {
-      if (error != null) {
+      if (error) {
         return callback(error)
       }
       db.rooms.deleteOne(
@@ -125,7 +118,7 @@ module.exports = ThreadManager = {
           _id: room._id,
         },
         function (error) {
-          if (error != null) {
+          if (error) {
             return callback(error)
           }
           callback(null, room._id)

+ 2 - 3
services/chat/test/acceptance/js/helpers/ChatApp.js

@@ -3,7 +3,6 @@
 /*
  * decaffeinate suggestions:
  * DS101: Remove unnecessary use of Array.from
- * DS207: Consider shorter variations of null checks
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
 const { waitForDb } = require('../../../../app/js/mongodb')
@@ -14,7 +13,7 @@ module.exports = {
   initing: false,
   callbacks: [],
   ensureRunning(callback) {
-    if (callback == null) {
+    if (!callback) {
       callback = function () {}
     }
     if (this.running) {
@@ -26,7 +25,7 @@ module.exports = {
     this.callbacks.push(callback)
     waitForDb().then(() => {
       app.listen(3010, 'localhost', error => {
-        if (error != null) {
+        if (error) {
           throw error
         }
         this.running = true