From 3cccbdfe8134811ed9169e14e0bf2d96bff82abd Mon Sep 17 00:00:00 2001 From: Steve Faulkner Date: Thu, 18 Mar 2021 22:19:35 -0500 Subject: [PATCH] Fix Lint errors in URLUtility (#462) --- .eslintignore | 1 - src/Common/UrlUtility.ts | 114 +++++++++--------- .../Controls/GitHub/AddRepoComponent.tsx | 2 +- src/GitHub/GitHubClient.ts | 2 +- src/GitHub/GitHubContentProvider.ts | 2 +- .../ResourceProviderClient.ts | 2 +- 6 files changed, 64 insertions(+), 59 deletions(-) diff --git a/.eslintignore b/.eslintignore index 0abcdf33e..052e75fa4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -24,7 +24,6 @@ src/Common/ObjectCache.test.ts src/Common/ObjectCache.ts src/Common/QueriesClient.ts src/Common/Splitter.ts -src/Common/UrlUtility.ts src/Config.ts src/Contracts/ActionContracts.ts src/Contracts/DataModels.ts diff --git a/src/Common/UrlUtility.ts b/src/Common/UrlUtility.ts index 4d6727956..3d990c7a7 100644 --- a/src/Common/UrlUtility.ts +++ b/src/Common/UrlUtility.ts @@ -1,55 +1,61 @@ -export default class UrlUtility { - public static parseDocumentsPath(resourcePath: string): any { - if (typeof resourcePath !== "string") { - return {}; - } - - if (resourcePath.length === 0) { - return {}; - } - - if (resourcePath[resourcePath.length - 1] !== "/") { - resourcePath = resourcePath + "/"; - } - - if (resourcePath[0] !== "/") { - resourcePath = "/" + resourcePath; - } - - var id: string; - var type: string; - var pathParts = resourcePath.split("/"); - - if (pathParts.length % 2 === 0) { - id = pathParts[pathParts.length - 2]; - type = pathParts[pathParts.length - 3]; - } else { - id = pathParts[pathParts.length - 3]; - type = pathParts[pathParts.length - 2]; - } - - var result = { - type: type, - objectBody: { - id: id, - self: resourcePath, - }, - }; - - return result; - } - - public static createUri(baseUri: string, relativeUri: string): string { - if (!baseUri) { - throw new Error("baseUri is null or empty"); - } - - var slashAtEndOfUriRegex = /\/$/, - slashAtStartOfUriRegEx = /^\//; - - var normalizedBaseUri = baseUri.replace(slashAtEndOfUriRegex, "") + "/", - normalizedRelativeUri = (relativeUri && relativeUri.replace(slashAtStartOfUriRegEx, "")) || ""; - - return normalizedBaseUri + normalizedRelativeUri; - } +interface Result { + type?: string; + objectBody?: { + id: string; + self: string; + }; +} + +export function parseDocumentsPath(resourcePath: string): Result { + if (typeof resourcePath !== "string") { + return {}; + } + + if (resourcePath.length === 0) { + return {}; + } + + if (resourcePath[resourcePath.length - 1] !== "/") { + resourcePath = resourcePath + "/"; + } + + if (resourcePath[0] !== "/") { + resourcePath = "/" + resourcePath; + } + + let id: string; + let type: string; + const pathParts = resourcePath.split("/"); + + if (pathParts.length % 2 === 0) { + id = pathParts[pathParts.length - 2]; + type = pathParts[pathParts.length - 3]; + } else { + id = pathParts[pathParts.length - 3]; + type = pathParts[pathParts.length - 2]; + } + + const result = { + type: type, + objectBody: { + id: id, + self: resourcePath, + }, + }; + + return result; +} + +export function createUri(baseUri: string, relativeUri: string): string { + if (!baseUri) { + throw new Error("baseUri is null or empty"); + } + + const slashAtEndOfUriRegex = /\/$/, + slashAtStartOfUriRegEx = /^\//; + + const normalizedBaseUri = baseUri.replace(slashAtEndOfUriRegex, "") + "/", + normalizedRelativeUri = (relativeUri && relativeUri.replace(slashAtStartOfUriRegEx, "")) || ""; + + return normalizedBaseUri + normalizedRelativeUri; } diff --git a/src/Explorer/Controls/GitHub/AddRepoComponent.tsx b/src/Explorer/Controls/GitHub/AddRepoComponent.tsx index 0c1dcb454..f07611cca 100644 --- a/src/Explorer/Controls/GitHub/AddRepoComponent.tsx +++ b/src/Explorer/Controls/GitHub/AddRepoComponent.tsx @@ -7,7 +7,7 @@ import { ChildrenMargin } from "./GitHubStyleConstants"; import * as GitHubUtils from "../../../Utils/GitHubUtils"; import { IGitHubRepo } from "../../../GitHub/GitHubClient"; import * as TelemetryProcessor from "../../../Shared/Telemetry/TelemetryProcessor"; -import UrlUtility from "../../../Common/UrlUtility"; +import * as UrlUtility from "../../../Common/UrlUtility"; import Explorer from "../../Explorer"; export interface AddRepoComponentProps { diff --git a/src/GitHub/GitHubClient.ts b/src/GitHub/GitHubClient.ts index 016bca4b6..273d7a7c4 100644 --- a/src/GitHub/GitHubClient.ts +++ b/src/GitHub/GitHubClient.ts @@ -1,7 +1,7 @@ import { Octokit } from "@octokit/rest"; import { HttpStatusCodes } from "../Common/Constants"; import * as Logger from "../Common/Logger"; -import UrlUtility from "../Common/UrlUtility"; +import * as UrlUtility from "../Common/UrlUtility"; import { NotebookUtil } from "../Explorer/Notebook/NotebookUtil"; import { getErrorMessage } from "../Common/ErrorHandlingUtils"; diff --git a/src/GitHub/GitHubContentProvider.ts b/src/GitHub/GitHubContentProvider.ts index d8f45aa97..ac45ce9de 100644 --- a/src/GitHub/GitHubContentProvider.ts +++ b/src/GitHub/GitHubContentProvider.ts @@ -8,7 +8,7 @@ import * as Logger from "../Common/Logger"; import { NotebookUtil } from "../Explorer/Notebook/NotebookUtil"; import { GitHubClient, IGitHubFile, IGitHubResponse } from "./GitHubClient"; import * as GitHubUtils from "../Utils/GitHubUtils"; -import UrlUtility from "../Common/UrlUtility"; +import * as UrlUtility from "../Common/UrlUtility"; import { getErrorMessage } from "../Common/ErrorHandlingUtils"; export interface GitHubContentProviderParams { diff --git a/src/ResourceProvider/ResourceProviderClient.ts b/src/ResourceProvider/ResourceProviderClient.ts index 4c13cb9c3..10f3c8fae 100644 --- a/src/ResourceProvider/ResourceProviderClient.ts +++ b/src/ResourceProvider/ResourceProviderClient.ts @@ -3,7 +3,7 @@ import { HttpStatusCodes } from "../Common/Constants"; import { IResourceProviderClient, IResourceProviderRequestOptions } from "./IResourceProviderClient"; import { OperationStatus } from "../Contracts/DataModels"; import { TokenProviderFactory } from "../TokenProviders/TokenProviderFactory"; -import UrlUtility from "../Common/UrlUtility"; +import * as UrlUtility from "../Common/UrlUtility"; export class ResourceProviderClient implements IResourceProviderClient { private httpClient: HttpClient;