From 9efe5a5c09c90047c78e63601089e16a4348e667 Mon Sep 17 00:00:00 2001
From: Dani Palou <dani@moodle.com>
Date: Thu, 14 Oct 2021 15:03:13 +0200
Subject: [PATCH] MOBILE-2848 eslint: Fix warnings in Site, Lang, WS

---
 src/core/classes/site.ts  | 23 +++++++++++++----------
 src/core/services/lang.ts |  6 +++---
 src/core/services/ws.ts   | 11 +++++++----
 3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts
index 5cd67966d..655b654c2 100644
--- a/src/core/classes/site.ts
+++ b/src/core/classes/site.ts
@@ -309,10 +309,7 @@ export class CoreSite {
 
         // Index function by name to speed up wsAvailable method.
         if (infos?.functions) {
-            infos.functionsByName = {};
-            infos.functions.forEach((func) => {
-                infos.functionsByName![func.name] = func;
-            });
+            infos.functionsByName = CoreUtils.arrayToObject(infos.functions, 'name');
         }
     }
 
@@ -907,7 +904,8 @@ export class CoreSite {
         preSets: CoreSiteWSPreSets,
         emergency?: boolean,
     ): Promise<T> {
-        if (!this.db || !preSets.getFromCache) {
+        const db = this.db;
+        if (!db || !preSets.getFromCache) {
             throw new CoreError('Get from cache is disabled.');
         }
 
@@ -915,11 +913,11 @@ export class CoreSite {
         let entry: CoreSiteWSCacheRecord | undefined;
 
         if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) {
-            const entries = await this.db.getRecords<CoreSiteWSCacheRecord>(CoreSite.WS_CACHE_TABLE, { key: preSets.cacheKey });
+            const entries = await db.getRecords<CoreSiteWSCacheRecord>(CoreSite.WS_CACHE_TABLE, { key: preSets.cacheKey });
 
             if (!entries.length) {
                 // Cache key not found, get by params sent.
-                entry = await this.db!.getRecord(CoreSite.WS_CACHE_TABLE, { id });
+                entry = await db.getRecord(CoreSite.WS_CACHE_TABLE, { id });
             } else {
                 if (entries.length > 1) {
                     // More than one entry found. Search the one with same ID as this call.
@@ -931,7 +929,7 @@ export class CoreSite {
                 }
             }
         } else {
-            entry = await this.db!.getRecord(CoreSite.WS_CACHE_TABLE, { id });
+            entry = await db.getRecord(CoreSite.WS_CACHE_TABLE, { id });
         }
 
         if (typeof entry == 'undefined') {
@@ -946,7 +944,7 @@ export class CoreSite {
         if (!preSets.omitExpires) {
             expirationTime = entry.expirationTime + this.getExpirationDelay(preSets.updateFrequency);
 
-            if (now > expirationTime!) {
+            if (now > expirationTime) {
                 this.logger.debug('Cached element found, but it is expired');
 
                 throw new CoreError('Cache entry is expired.');
@@ -1747,7 +1745,12 @@ export class CoreSite {
 
         if (CoreSite.MOODLE_RELEASES[data.major] === undefined) {
             // Major version not found. Use the last one.
-            data.major = Object.keys(CoreSite.MOODLE_RELEASES).pop()!;
+            const major = Object.keys(CoreSite.MOODLE_RELEASES).pop();
+            if (!major) {
+                return 0;
+            }
+
+            data.major = major;
         }
 
         return CoreSite.MOODLE_RELEASES[data.major] + data.minor;
diff --git a/src/core/services/lang.ts b/src/core/services/lang.ts
index b3eee335e..f0430b64f 100644
--- a/src/core/services/lang.ts
+++ b/src/core/services/lang.ts
@@ -403,11 +403,11 @@ export class CoreLangProvider {
 
         this.customStringsRaw = strings;
 
-        if (currentLangChanged) {
+        if (currentLangChanged && this.currentLanguage) {
             // Some lang strings have changed, emit an event to update the pipes.
             Translate.onLangChange.emit({
-                lang: this.currentLanguage!,
-                translations: Translate.translations[this.currentLanguage!],
+                lang: this.currentLanguage,
+                translations: Translate.translations[this.currentLanguage],
             });
         }
     }
diff --git a/src/core/services/ws.ts b/src/core/services/ws.ts
index bb2e63719..ff2d7e869 100644
--- a/src/core/services/ws.ts
+++ b/src/core/services/ws.ts
@@ -336,8 +336,9 @@ export class CoreWSProvider {
      * @return Promise resolved with the mimetype or '' if failure.
      */
     async getRemoteFileMimeType(url: string, ignoreCache?: boolean): Promise<string> {
-        if (this.mimeTypeCache[url] && !ignoreCache) {
-            return this.mimeTypeCache[url]!;
+        const cachedMimeType = this.mimeTypeCache[url];
+        if (cachedMimeType && !ignoreCache) {
+            return cachedMimeType;
         }
 
         try {
@@ -722,10 +723,12 @@ export class CoreWSProvider {
      */
     protected processRetryQueue(): void {
         if (this.retryCalls.length > 0 && this.retryTimeout == 0) {
-            const call = this.retryCalls.shift();
+            const call = this.retryCalls[0];
+            this.retryCalls.shift();
+
             // Add a delay between calls.
             setTimeout(() => {
-                call!.deferred.resolve(this.performPost(call!.method, call!.siteUrl, call!.data, call!.preSets));
+                call.deferred.resolve(this.performPost(call.method, call.siteUrl, call.data, call.preSets));
                 this.processRetryQueue();
             }, 200);
         } else {