Skip to content

refactor: remove dead code #5188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/common/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/**
* Split a string up to the delimiter. If the delimiter doesn't exist the first
* item will have all the text and the second item will be an empty string.
*/
export const split = (str: string, delimiter: string): [string, string] => {
const index = str.indexOf(delimiter)
return index !== -1 ? [str.substring(0, index).trim(), str.substring(index + 1)] : [str, ""]
}

/**
* Appends an 's' to the provided string if count is greater than one;
* otherwise the string is returned
Expand Down Expand Up @@ -34,27 +25,6 @@ export const normalize = (url: string, keepTrailing = false): string => {
return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
}

/**
* Remove leading and trailing slashes.
*/
export const trimSlashes = (url: string): string => {
return url.replace(/^\/+|\/+$/g, "")
}

/**
* Wrap the value in an array if it's not already an array. If the value is
* undefined return an empty array.
*/
export const arrayify = <T>(value?: T | T[]): T[] => {
if (Array.isArray(value)) {
return value
}
if (typeof value === "undefined") {
return []
}
return [value]
}

// TODO: Might make sense to add Error handling to the logger itself.
export function logError(logger: { error: (msg: string) => void }, prefix: string, err: unknown): void {
if (err instanceof Error) {
Expand Down
23 changes: 0 additions & 23 deletions src/node/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,6 @@ export const ensureAddress = (server: http.Server, protocol: string): URL | stri
return addr
}

/**
* Handles error events from the server.
*
* If the outlying Promise didn't resolve
* then we reject with the error.
*
* Otherwise, we log the error.
*
* We extracted into a function so that we could
* test this logic more easily.
*/
export const handleServerError = (resolved: boolean, err: Error, reject: (err: Error) => void) => {
// Promise didn't resolve earlier so this means it's an error
// that occurs before the server can successfully listen.
// Possibly triggered by listening on an invalid port or socket.
if (!resolved) {
reject(err)
} else {
// Promise resolved earlier so this is an unrelated error.
util.logError(logger, "http server error", err)
}
}

/**
* Handles the error that occurs in the catch block
* after we try fs.unlink(args.socket).
Expand Down
3 changes: 0 additions & 3 deletions src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package"
import * as os from "os"
import * as path from "path"

export const WORKBENCH_WEB_CONFIG_ID = "vscode-workbench-web-configuration"

export function getPackageJson(relativePath: string): JSONSchemaForNPMPackageJsonFiles {
let pkg = {}
try {
Expand All @@ -21,7 +19,6 @@ export const vsRootPath = path.join(rootPath, "lib/vscode")
const PACKAGE_JSON = "package.json"
const pkg = getPackageJson(`${rootPath}/${PACKAGE_JSON}`)
const codePkg = getPackageJson(`${vsRootPath}/${PACKAGE_JSON}`) || { version: "0.0.0" }
export const pkgName = pkg.name || "code-server"
export const version = pkg.version || "development"
export const commit = pkg.commit || "development"
export const codeVersion = codePkg.version || "development"
Expand Down
9 changes: 0 additions & 9 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,6 @@ export const enumToArray = (t: any): string[] => {
return values
}

/**
* For displaying all allowed options in an enum.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const buildAllowedMessage = (t: any): string => {
const values = enumToArray(t)
return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(", ")}`
}

/**
* Return a promise that resolves with whether the socket path is active.
*/
Expand Down
47 changes: 0 additions & 47 deletions test/unit/common/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ describe("util", () => {
})
})

describe("split", () => {
it("should split at a comma", () => {
expect(util.split("Hello,world", ",")).toStrictEqual(["Hello", "world"])
})

it("shouldn't split if the delimiter doesn't exist", () => {
expect(util.split("Hello world", ",")).toStrictEqual(["Hello world", ""])
})
})

describe("plural", () => {
it("should add an s if count is greater than 1", () => {
expect(util.plural(2, "dog")).toBe("dogs")
Expand All @@ -57,43 +47,6 @@ describe("util", () => {
})
})

describe("trimSlashes", () => {
it("should remove leading slashes", () => {
expect(util.trimSlashes("/hello-world")).toBe("hello-world")
})

it("should remove trailing slashes", () => {
expect(util.trimSlashes("hello-world/")).toBe("hello-world")
})

it("should remove both leading and trailing slashes", () => {
expect(util.trimSlashes("/hello-world/")).toBe("hello-world")
})

it("should remove multiple leading and trailing slashes", () => {
expect(util.trimSlashes("///hello-world////")).toBe("hello-world")
})
})

describe("arrayify", () => {
it("should return value it's already an array", () => {
expect(util.arrayify(["hello", "world"])).toStrictEqual(["hello", "world"])
})

it("should wrap the value in an array if not an array", () => {
expect(
util.arrayify({
name: "Coder",
version: "3.8",
}),
).toStrictEqual([{ name: "Coder", version: "3.8" }])
})

it("should return an empty array if the value is undefined", () => {
expect(util.arrayify(undefined)).toStrictEqual([])
})
})

describe("logError", () => {
beforeAll(() => {
mockLogger()
Expand Down
34 changes: 1 addition & 33 deletions test/unit/node/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promises } from "fs"
import * as http from "http"
import * as https from "https"
import * as path from "path"
import { createApp, ensureAddress, handleArgsSocketCatchError, handleServerError, listen } from "../../../src/node/app"
import { createApp, ensureAddress, handleArgsSocketCatchError, listen } from "../../../src/node/app"
import { OptionalString, setDefaults } from "../../../src/node/cli"
import { generateCertificate } from "../../../src/node/util"
import { clean, mockLogger, getAvailablePort, tmpdir } from "../../utils/helpers"
Expand Down Expand Up @@ -169,38 +169,6 @@ describe("ensureAddress", () => {
})
})

describe("handleServerError", () => {
beforeAll(() => {
mockLogger()
})

afterEach(() => {
jest.clearAllMocks()
})

it("should call reject if resolved is false", async () => {
const resolved = false
const reject = jest.fn((err: Error) => undefined)
const error = new Error("handleServerError Error")

handleServerError(resolved, error, reject)

expect(reject).toHaveBeenCalledTimes(1)
expect(reject).toHaveBeenCalledWith(error)
})

it("should log an error if resolved is true", async () => {
const resolved = true
const reject = jest.fn((err: Error) => undefined)
const error = new Error("handleServerError Error")

handleServerError(resolved, error, reject)

expect(logger.error).toHaveBeenCalledTimes(1)
expect(logger.error).toHaveBeenCalledWith(`http server error: ${error.message} ${error.stack}`)
})
})

describe("handleArgsSocketCatchError", () => {
beforeAll(() => {
mockLogger()
Expand Down
4 changes: 0 additions & 4 deletions test/unit/node/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ describe("constants", () => {
jest.resetModules()
})

it("should provide the package name", () => {
expect(constants.pkgName).toBe(mockPackageJson.name)
})

it("should provide the commit", () => {
expect(constants.commit).toBe(mockPackageJson.commit)
})
Expand Down