From 71113e403e81438451003fb6acf98b7c28a24d7a Mon Sep 17 00:00:00 2001 From: Hardikkumar Nai <80053762+hardiknai-techm@users.noreply.github.com> Date: Tue, 13 Jul 2021 09:08:16 +0530 Subject: [PATCH] Resolve ESlint Errors (#932) --- .eslintignore | 10 --- src/Common/HeadersUtility.test.ts | 4 +- src/Explorer/Explorer.tsx | 2 +- src/Explorer/Tree/Collection.test.ts | 25 +++----- src/Shared/ExplorerSettings.ts | 31 ++++------ src/Shared/LocalStorageUtility.ts | 22 +++++++ src/Shared/PriceEstimateCalculator.ts | 12 ++-- src/Shared/SessionStorageUtility.ts | 20 ++++++ src/Shared/StorageUtility.ts | 72 +--------------------- src/TokenProviders/PortalTokenProvider.ts | 13 ---- src/TokenProviders/TokenProviderFactory.ts | 20 ------ src/Utils/PricingUtils.test.ts | 21 +------ src/Utils/QueryUtils.test.ts | 8 +-- 13 files changed, 81 insertions(+), 179 deletions(-) create mode 100644 src/Shared/LocalStorageUtility.ts create mode 100644 src/Shared/SessionStorageUtility.ts delete mode 100644 src/TokenProviders/PortalTokenProvider.ts delete mode 100644 src/TokenProviders/TokenProviderFactory.ts diff --git a/.eslintignore b/.eslintignore index bc1bab872..cd598d58b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -135,7 +135,6 @@ src/Explorer/Tabs/TabsBase.ts src/Explorer/Tabs/TriggerTab.ts src/Explorer/Tabs/UserDefinedFunctionTab.ts src/Explorer/Tree/AccessibleVerticalList.ts -src/Explorer/Tree/Collection.test.ts src/Explorer/Tree/Collection.ts src/Explorer/Tree/ConflictId.ts src/Explorer/Tree/DocumentId.ts @@ -161,20 +160,11 @@ src/ReactDevTools.ts src/Shared/Constants.ts src/Shared/DefaultExperienceUtility.test.ts src/Shared/DefaultExperienceUtility.ts -src/Shared/ExplorerSettings.ts -src/Shared/PriceEstimateCalculator.ts -src/Shared/StorageUtility.test.ts -src/Shared/StorageUtility.ts src/Shared/appInsights.ts src/SparkClusterManager/ArcadiaResourceManager.ts src/SparkClusterManager/SparkClusterManager.ts src/Terminal/JupyterLabAppFactory.ts src/Terminal/NotebookAppContracts.d.ts -src/Terminal/index.ts -src/TokenProviders/PortalTokenProvider.ts -src/TokenProviders/TokenProviderFactory.ts -src/Utils/PricingUtils.test.ts -src/Utils/QueryUtils.test.ts src/applyExplorerBindings.ts src/global.d.ts src/setupTests.ts diff --git a/src/Common/HeadersUtility.test.ts b/src/Common/HeadersUtility.test.ts index 5f46c420f..5432227fa 100644 --- a/src/Common/HeadersUtility.test.ts +++ b/src/Common/HeadersUtility.test.ts @@ -1,6 +1,6 @@ -import * as HeadersUtility from "./HeadersUtility"; -import { ExplorerSettings } from "../Shared/ExplorerSettings"; +import * as ExplorerSettings from "../Shared/ExplorerSettings"; import { LocalStorageUtility, StorageKey } from "../Shared/StorageUtility"; +import * as HeadersUtility from "./HeadersUtility"; describe("Headers Utility", () => { describe("shouldEnableCrossPartitionKeyForResourceWithPartitionKey()", () => { diff --git a/src/Explorer/Explorer.tsx b/src/Explorer/Explorer.tsx index 32e2e5828..cd9fe0800 100644 --- a/src/Explorer/Explorer.tsx +++ b/src/Explorer/Explorer.tsx @@ -18,7 +18,7 @@ import { GitHubOAuthService } from "../GitHub/GitHubOAuthService"; import { useSidePanel } from "../hooks/useSidePanel"; import { useTabs } from "../hooks/useTabs"; import { IGalleryItem } from "../Juno/JunoClient"; -import { ExplorerSettings } from "../Shared/ExplorerSettings"; +import * as ExplorerSettings from "../Shared/ExplorerSettings"; import { Action, ActionModifiers } from "../Shared/Telemetry/TelemetryConstants"; import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor"; import { userContext } from "../UserContext"; diff --git a/src/Explorer/Tree/Collection.test.ts b/src/Explorer/Tree/Collection.test.ts index 07987e43d..1c701798e 100644 --- a/src/Explorer/Tree/Collection.test.ts +++ b/src/Explorer/Tree/Collection.test.ts @@ -4,18 +4,12 @@ import Collection from "./Collection"; jest.mock("monaco-editor"); describe("Collection", () => { - function generateCollection( - container: Explorer, - databaseId: string, - data: DataModels.Collection, - offer: DataModels.Offer - ): Collection { - return new Collection(container, databaseId, data); - } + const generateCollection = (container: Explorer, databaseId: string, data: DataModels.Collection): Collection => + new Collection(container, databaseId, data); - function generateMockCollectionsDataModelWithPartitionKey( + const generateMockCollectionsDataModelWithPartitionKey = ( partitionKey: DataModels.PartitionKey - ): DataModels.Collection { + ): DataModels.Collection => { return { defaultTtl: 1, indexingPolicy: {} as DataModels.IndexingPolicy, @@ -26,13 +20,12 @@ describe("Collection", () => { _ts: 1, id: "", }; - } + }; - function generateMockCollectionWithDataModel(data: DataModels.Collection): Collection { + const generateMockCollectionWithDataModel = (data: DataModels.Collection): Collection => { const mockContainer = {} as Explorer; - - return generateCollection(mockContainer, "abc", data, {} as DataModels.Offer); - } + return generateCollection(mockContainer, "abc", data); + }; describe("Partition key path parsing", () => { let collection: Collection; @@ -88,7 +81,7 @@ describe("Collection", () => { kind: "Hash", }); collection = generateMockCollectionWithDataModel(collectionsDataModel); - expect(collection.partitionKeyPropertyHeader).toBeNull; + expect(collection.partitionKeyPropertyHeader).toBeNull(); }); }); }); diff --git a/src/Shared/ExplorerSettings.ts b/src/Shared/ExplorerSettings.ts index b697dac83..3c6f9e3d2 100644 --- a/src/Shared/ExplorerSettings.ts +++ b/src/Shared/ExplorerSettings.ts @@ -1,22 +1,17 @@ import * as Constants from "../Common/Constants"; import { LocalStorageUtility, StorageKey } from "./StorageUtility"; -export class ExplorerSettings { - public static createDefaultSettings() { - LocalStorageUtility.setEntryNumber(StorageKey.ActualItemPerPage, Constants.Queries.itemsPerPage); - LocalStorageUtility.setEntryNumber(StorageKey.CustomItemPerPage, Constants.Queries.itemsPerPage); - LocalStorageUtility.setEntryString(StorageKey.IsCrossPartitionQueryEnabled, "true"); - LocalStorageUtility.setEntryNumber( - StorageKey.MaxDegreeOfParellism, - Constants.Queries.DefaultMaxDegreeOfParallelism - ); - } +export const createDefaultSettings = () => { + LocalStorageUtility.setEntryNumber(StorageKey.ActualItemPerPage, Constants.Queries.itemsPerPage); + LocalStorageUtility.setEntryNumber(StorageKey.CustomItemPerPage, Constants.Queries.itemsPerPage); + LocalStorageUtility.setEntryString(StorageKey.IsCrossPartitionQueryEnabled, "true"); + LocalStorageUtility.setEntryNumber(StorageKey.MaxDegreeOfParellism, Constants.Queries.DefaultMaxDegreeOfParallelism); +}; - public static hasSettingsDefined(): boolean { - return ( - LocalStorageUtility.hasItem(StorageKey.ActualItemPerPage) && - LocalStorageUtility.hasItem(StorageKey.IsCrossPartitionQueryEnabled) && - LocalStorageUtility.hasItem(StorageKey.MaxDegreeOfParellism) - ); - } -} +export const hasSettingsDefined = (): boolean => { + return ( + LocalStorageUtility.hasItem(StorageKey.ActualItemPerPage) && + LocalStorageUtility.hasItem(StorageKey.IsCrossPartitionQueryEnabled) && + LocalStorageUtility.hasItem(StorageKey.MaxDegreeOfParellism) + ); +}; diff --git a/src/Shared/LocalStorageUtility.ts b/src/Shared/LocalStorageUtility.ts new file mode 100644 index 000000000..9fc2f4f7c --- /dev/null +++ b/src/Shared/LocalStorageUtility.ts @@ -0,0 +1,22 @@ +import { StorageKey } from "./StorageUtility"; +import * as StringUtility from "./StringUtility"; + +export const hasItem = (key: StorageKey): boolean => !!localStorage.getItem(StorageKey[key]); + +export const getEntryString = (key: StorageKey): string | null => localStorage.getItem(StorageKey[key]); + +export const getEntryNumber = (key: StorageKey): number => + StringUtility.toNumber(localStorage.getItem(StorageKey[key])); + +export const getEntryBoolean = (key: StorageKey): boolean => + StringUtility.toBoolean(localStorage.getItem(StorageKey[key])); + +export const setEntryString = (key: StorageKey, value: string): void => localStorage.setItem(StorageKey[key], value); + +export const removeEntry = (key: StorageKey): void => localStorage.removeItem(StorageKey[key]); + +export const setEntryNumber = (key: StorageKey, value: number): void => + localStorage.setItem(StorageKey[key], value.toString()); + +export const setEntryBoolean = (key: StorageKey, value: boolean): void => + localStorage.setItem(StorageKey[key], value.toString()); diff --git a/src/Shared/PriceEstimateCalculator.ts b/src/Shared/PriceEstimateCalculator.ts index 2e897ea66..a7f08d230 100644 --- a/src/Shared/PriceEstimateCalculator.ts +++ b/src/Shared/PriceEstimateCalculator.ts @@ -2,26 +2,26 @@ import * as Constants from "./Constants"; export function computeRUUsagePrice(serverId: string, requestUnits: number): string { if (serverId === "mooncake") { - let ruCharge = requestUnits * Constants.OfferPricing.HourlyPricing.mooncake.Standard.PricePerRU; + const ruCharge = requestUnits * Constants.OfferPricing.HourlyPricing.mooncake.Standard.PricePerRU; return calculateEstimateNumber(ruCharge) + " " + Constants.OfferPricing.HourlyPricing.mooncake.Currency; } - let ruCharge = requestUnits * Constants.OfferPricing.HourlyPricing.default.Standard.PricePerRU; + const ruCharge = requestUnits * Constants.OfferPricing.HourlyPricing.default.Standard.PricePerRU; return calculateEstimateNumber(ruCharge) + " " + Constants.OfferPricing.HourlyPricing.default.Currency; } export function computeStorageUsagePrice(serverId: string, storageUsedRoundUpToGB: number): string { if (serverId === "mooncake") { - let storageCharge = storageUsedRoundUpToGB * Constants.OfferPricing.HourlyPricing.mooncake.Standard.PricePerGB; + const storageCharge = storageUsedRoundUpToGB * Constants.OfferPricing.HourlyPricing.mooncake.Standard.PricePerGB; return calculateEstimateNumber(storageCharge) + " " + Constants.OfferPricing.HourlyPricing.mooncake.Currency; } - let storageCharge = storageUsedRoundUpToGB * Constants.OfferPricing.HourlyPricing.default.Standard.PricePerGB; + const storageCharge = storageUsedRoundUpToGB * Constants.OfferPricing.HourlyPricing.default.Standard.PricePerGB; return calculateEstimateNumber(storageCharge) + " " + Constants.OfferPricing.HourlyPricing.default.Currency; } export function computeDisplayUsageString(usageInKB: number): string { - let usageInMB = usageInKB / 1024, + const usageInMB = usageInKB / 1024, usageInGB = usageInMB / 1024, displayUsageString = usageInGB > 0.1 @@ -33,7 +33,7 @@ export function computeDisplayUsageString(usageInKB: number): string { } export function usageInGB(usageInKB: number): number { - let usageInMB = usageInKB / 1024, + const usageInMB = usageInKB / 1024, usageInGB = usageInMB / 1024; return Math.ceil(usageInGB); } diff --git a/src/Shared/SessionStorageUtility.ts b/src/Shared/SessionStorageUtility.ts new file mode 100644 index 000000000..f3fb4abae --- /dev/null +++ b/src/Shared/SessionStorageUtility.ts @@ -0,0 +1,20 @@ +import { StorageKey } from "./StorageUtility"; +import * as StringUtility from "./StringUtility"; + +export const hasItem = (key: StorageKey): boolean => !!sessionStorage.getItem(StorageKey[key]); + +export const getEntryString = (key: StorageKey): string | null => sessionStorage.getItem(StorageKey[key]); + +export const getEntryNumber = (key: StorageKey): number => + StringUtility.toNumber(sessionStorage.getItem(StorageKey[key])); + +export const getEntry = (key: string): string | null => sessionStorage.getItem(key); + +export const removeEntry = (key: StorageKey): void => sessionStorage.removeItem(StorageKey[key]); + +export const setEntryString = (key: StorageKey, value: string): void => sessionStorage.setItem(StorageKey[key], value); + +export const setEntry = (key: string, value: string): void => sessionStorage.setItem(key, value); + +export const setEntryNumber = (key: StorageKey, value: number): void => + sessionStorage.setItem(StorageKey[key], value.toString()); diff --git a/src/Shared/StorageUtility.ts b/src/Shared/StorageUtility.ts index adf271cec..9c16467d2 100644 --- a/src/Shared/StorageUtility.ts +++ b/src/Shared/StorageUtility.ts @@ -1,73 +1,7 @@ -import * as StringUtility from "./StringUtility"; - -export class LocalStorageUtility { - public static hasItem(key: StorageKey): boolean { - return !!localStorage.getItem(StorageKey[key]); - } - - public static getEntryString(key: StorageKey): string | null { - return localStorage.getItem(StorageKey[key]); - } - - public static getEntryNumber(key: StorageKey): number { - return StringUtility.toNumber(localStorage.getItem(StorageKey[key])); - } - - public static getEntryBoolean(key: StorageKey): boolean { - return StringUtility.toBoolean(localStorage.getItem(StorageKey[key])); - } - - public static setEntryString(key: StorageKey, value: string): void { - localStorage.setItem(StorageKey[key], value); - } - - public static removeEntry(key: StorageKey): void { - return localStorage.removeItem(StorageKey[key]); - } - - public static setEntryNumber(key: StorageKey, value: number): void { - localStorage.setItem(StorageKey[key], value.toString()); - } - - public static setEntryBoolean(key: StorageKey, value: boolean): void { - localStorage.setItem(StorageKey[key], value.toString()); - } -} - -export class SessionStorageUtility { - public static hasItem(key: StorageKey): boolean { - return !!sessionStorage.getItem(StorageKey[key]); - } - - public static getEntryString(key: StorageKey): string | null { - return sessionStorage.getItem(StorageKey[key]); - } - - public static getEntryNumber(key: StorageKey): number { - return StringUtility.toNumber(sessionStorage.getItem(StorageKey[key])); - } - - public static getEntry(key: string): string | null { - return sessionStorage.getItem(key); - } - - public static removeEntry(key: StorageKey): void { - return sessionStorage.removeItem(StorageKey[key]); - } - - public static setEntryString(key: StorageKey, value: string): void { - sessionStorage.setItem(StorageKey[key], value); - } - - public static setEntry(key: string, value: string): void { - sessionStorage.setItem(key, value); - } - - public static setEntryNumber(key: StorageKey, value: number): void { - sessionStorage.setItem(StorageKey[key], value.toString()); - } -} +import * as LocalStorageUtility from "./LocalStorageUtility"; +import * as SessionStorageUtility from "./SessionStorageUtility"; +export { LocalStorageUtility, SessionStorageUtility }; export enum StorageKey { ActualItemPerPage, CustomItemPerPage, diff --git a/src/TokenProviders/PortalTokenProvider.ts b/src/TokenProviders/PortalTokenProvider.ts deleted file mode 100644 index c7b1480ad..000000000 --- a/src/TokenProviders/PortalTokenProvider.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as ViewModels from "../Contracts/ViewModels"; -import { userContext } from "../UserContext"; - -export class PortalTokenProvider implements ViewModels.TokenProvider { - constructor() {} - - public async getAuthHeader(): Promise { - const bearerToken = userContext.authorizationToken; - let fetchHeaders = new Headers(); - fetchHeaders.append("authorization", bearerToken); - return fetchHeaders; - } -} diff --git a/src/TokenProviders/TokenProviderFactory.ts b/src/TokenProviders/TokenProviderFactory.ts deleted file mode 100644 index 342661c3b..000000000 --- a/src/TokenProviders/TokenProviderFactory.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { configContext, Platform } from "../ConfigContext"; -import * as ViewModels from "../Contracts/ViewModels"; -import { PortalTokenProvider } from "./PortalTokenProvider"; - -export class TokenProviderFactory { - private constructor() {} - - public static create(): ViewModels.TokenProvider { - const platformType = configContext.platform; - switch (platformType) { - case Platform.Portal: - case Platform.Hosted: - return new PortalTokenProvider(); - case Platform.Emulator: - default: - // should never get into this state - throw new Error(`Unknown platform ${platformType}`); - } - } -} diff --git a/src/Utils/PricingUtils.test.ts b/src/Utils/PricingUtils.test.ts index 582c40a37..de4efa90c 100644 --- a/src/Utils/PricingUtils.test.ts +++ b/src/Utils/PricingUtils.test.ts @@ -18,7 +18,7 @@ describe("PricingUtils Tests", () => { }); it("should return false if passed number is not number", () => { - const value = PricingUtils.isLargerThanDefaultMinRU(null); + const value = PricingUtils.isLargerThanDefaultMinRU(undefined); expect(value).toBe(false); }); }); @@ -28,7 +28,7 @@ describe("PricingUtils Tests", () => { const value = PricingUtils.computeRUUsagePriceHourly({ serverId: "default", requestUnits: 1, - numberOfRegions: null, + numberOfRegions: undefined, multimasterEnabled: false, isAutoscale: false, }); @@ -38,7 +38,7 @@ describe("PricingUtils Tests", () => { const value = PricingUtils.computeRUUsagePriceHourly({ serverId: "default", requestUnits: 1, - numberOfRegions: null, + numberOfRegions: undefined, multimasterEnabled: false, isAutoscale: true, }); @@ -264,11 +264,6 @@ describe("PricingUtils Tests", () => { describe("getRegionMultiplier()", () => { describe("without multimaster", () => { - it("should return 0 for null", () => { - const value = PricingUtils.getRegionMultiplier(null, false); - expect(value).toBe(0); - }); - it("should return 0 for undefined", () => { const value = PricingUtils.getRegionMultiplier(undefined, false); expect(value).toBe(0); @@ -296,11 +291,6 @@ describe("PricingUtils Tests", () => { }); describe("with multimaster", () => { - it("should return 0 for null", () => { - const value = PricingUtils.getRegionMultiplier(null, true); - expect(value).toBe(0); - }); - it("should return 0 for undefined", () => { const value = PricingUtils.getRegionMultiplier(undefined, true); expect(value).toBe(0); @@ -450,11 +440,6 @@ describe("PricingUtils Tests", () => { }); describe("normalizeNumberOfRegions()", () => { - it("should return 0 for null", () => { - const value = PricingUtils.normalizeNumber(null); - expect(value).toBe(0); - }); - it("should return 0 for undefined", () => { const value = PricingUtils.normalizeNumber(undefined); expect(value).toBe(0); diff --git a/src/Utils/QueryUtils.test.ts b/src/Utils/QueryUtils.test.ts index 80ed18620..72a568a3a 100644 --- a/src/Utils/QueryUtils.test.ts +++ b/src/Utils/QueryUtils.test.ts @@ -5,23 +5,19 @@ import * as ViewModels from "../Contracts/ViewModels"; import * as QueryUtils from "./QueryUtils"; describe("Query Utils", () => { - function generatePartitionKeyForPath(path: string): DataModels.PartitionKey { + const generatePartitionKeyForPath = (path: string): DataModels.PartitionKey => { return { paths: [path], kind: "Hash", version: 2, }; - } + }; describe("buildDocumentsQueryPartitionProjections()", () => { it("should return empty string if partition key is undefined", () => { expect(QueryUtils.buildDocumentsQueryPartitionProjections("c", undefined)).toBe(""); }); - it("should return empty string if partition key is null", () => { - expect(QueryUtils.buildDocumentsQueryPartitionProjections("c", null)).toBe(""); - }); - it("should replace slashes and embed projection in square braces", () => { const partitionKey: DataModels.PartitionKey = generatePartitionKeyForPath("/a"); const partitionProjection: string = QueryUtils.buildDocumentsQueryPartitionProjections("c", partitionKey);