Skip to content

Commit

Permalink
fix: Better use of @ts-expect-error (#6038)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored Jun 2, 2020
1 parent 8b1b476 commit 3fe6bc1
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 145 deletions.
13 changes: 7 additions & 6 deletions cli/js/error_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ function evaluateCallSite(callSite: CallSite): CallSiteEval {
};
}

function prepareStackTrace(error: Error, callSites: CallSite[]): string {
function prepareStackTrace(
error: Error & {
__callSiteEvals: CallSiteEval[];
__formattedFrames: string[];
},
callSites: CallSite[]
): string {
const mappedCallSites = callSites.map(
(callSite): CallSite => {
const fileName = callSite.getFileName();
Expand All @@ -238,19 +244,14 @@ function prepareStackTrace(error: Error, callSites: CallSite[]): string {
__formattedFrames: { value: [], configurable: true },
});
for (const callSite of mappedCallSites) {
// @ts-expect-error
error.__callSiteEvals.push(Object.freeze(evaluateCallSite(callSite)));
const isInternal = callSite.getFileName()?.startsWith("$deno$") ?? false;
// @ts-expect-error
error.__formattedFrames.push(callSiteToString(callSite, isInternal));
}
// @ts-expect-error
Object.freeze(error.__callSiteEvals);
// @ts-expect-error
Object.freeze(error.__formattedFrames);
return (
`${error.name}: ${error.message}\n` +
// @ts-expect-error
error.__formattedFrames
.map((s: string) => ` at ${colors.stripColor(s)}`)
.join("\n")
Expand Down
4 changes: 2 additions & 2 deletions cli/js/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function isRecoverableError(e: Error): boolean {
// Returns `true` if `close()` is called in REPL.
// We should quit the REPL when this function returns `true`.
function isCloseCalled(): boolean {
// @ts-expect-error
return globalThis.closed;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (globalThis as any).closed;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
8 changes: 4 additions & 4 deletions cli/js/runtime_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { log, immutableDefine } from "./util.ts";
// TODO: factor out `Deno` global assignment to separate function
// Add internal object to Deno object.
// This is not exposed as part of the Deno types.
// @ts-expect-error
denoNs[internalSymbol] = internalObject;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(denoNs as any)[internalSymbol] = internalObject;

let windowIsClosing = false;

Expand Down Expand Up @@ -71,8 +71,8 @@ export function bootstrapMainRuntime(): void {
throw new Error("Worker runtime already bootstrapped");
}
// Remove bootstrapping methods from global scope
// @ts-expect-error
globalThis.bootstrap = undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).bootstrap = undefined;
log("bootstrapMainRuntime");
hasBootstrapped = true;
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);
Expand Down
8 changes: 4 additions & 4 deletions cli/js/runtime_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { setSignals } from "./signals.ts";
// TODO: factor out `Deno` global assignment to separate function
// Add internal object to Deno object.
// This is not exposed as part of the Deno types.
// @ts-expect-error
denoNs[internalSymbol] = internalObject;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(denoNs as any)[internalSymbol] = internalObject;

const encoder = new TextEncoder();

Expand Down Expand Up @@ -128,8 +128,8 @@ export function bootstrapWorkerRuntime(
throw new Error("Worker runtime already bootstrapped");
}
// Remove bootstrapping methods from global scope
// @ts-expect-error
globalThis.bootstrap = undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).bootstrap = undefined;
log("bootstrapWorkerRuntime");
hasBootstrapped = true;
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);
Expand Down
4 changes: 2 additions & 2 deletions cli/js/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ async function runTests({
const originalConsole = globalThis.console;

if (disableLog) {
// @ts-expect-error
globalThis.console = disabledConsole;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).console = disabledConsole;
}

let endMsg: TestMessage["end"];
Expand Down
26 changes: 15 additions & 11 deletions cli/js/web/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,18 @@ function groupEntries<T>(
lineMaxLength += separatorSpace;
maxLineLength[i] = lineMaxLength;
}
let order = "padStart";
let order: "padStart" | "padEnd" = "padStart";
if (value !== undefined) {
for (let i = 0; i < entries.length; i++) {
//@ts-expect-error
if (typeof value[i] !== "number" && typeof value[i] !== "bigint") {
/* eslint-disable @typescript-eslint/no-explicit-any */
if (
typeof (value as any)[i] !== "number" &&
typeof (value as any)[i] !== "bigint"
) {
order = "padEnd";
break;
}
/* eslint-enable */
}
}
// Each iteration creates a single line of grouped entries.
Expand All @@ -235,7 +239,6 @@ function groupEntries<T>(
for (; j < max - 1; j++) {
// In future, colors should be taken here into the account
const padding = maxLineLength[j - i];
//@ts-expect-error
str += `${entries[j]}, `[order](padding, " ");
}
if (order === "padStart") {
Expand Down Expand Up @@ -408,8 +411,8 @@ function createMapString(
},
group: false,
};
//@ts-expect-error
return createIterableString(value, ctx, level, maxLevel, printConfig);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return createIterableString(value as any, ctx, level, maxLevel, printConfig);
}

function createWeakSetString(): string {
Expand Down Expand Up @@ -477,7 +480,7 @@ function createPromiseString(
// TODO: Proxy

function createRawObjectString(
value: { [key: string]: unknown },
value: Record<string, unknown>,
ctx: ConsoleContext,
level: number,
maxLevel: number
Expand All @@ -490,8 +493,9 @@ function createRawObjectString(
let baseString = "";

let shouldShowDisplayName = false;
// @ts-expect-error
let displayName = value[Symbol.toStringTag];
let displayName = (value as { [Symbol.toStringTag]: string })[
Symbol.toStringTag
];
if (!displayName) {
displayName = getClassInstanceName(value);
}
Expand All @@ -511,8 +515,8 @@ function createRawObjectString(
for (const key of symbolKeys) {
entries.push(
`${key.toString()}: ${stringifyWithQuotes(
// @ts-expect-error
value[key],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value[key as any],
ctx,
level + 1,
maxLevel
Expand Down
3 changes: 1 addition & 2 deletions cli/js/web/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function sendFetchReq(
}

export async function fetch(
input: domTypes.Request | URL | string,
input: (domTypes.Request & { _bodySource?: unknown }) | URL | string,
init?: domTypes.RequestInit
): Promise<Response> {
let url: string;
Expand Down Expand Up @@ -285,7 +285,6 @@ export async function fetch(
method = input.method;
headers = input.headers;

//@ts-expect-error
if (input._bodySource) {
body = new DataView(await input.arrayBuffer());
}
Expand Down
4 changes: 2 additions & 2 deletions cli/js/web/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,23 @@ function setTimer(
}

export function setTimeout(
this: unknown,
cb: (...args: Args) => void,
delay = 0,
...args: Args
): number {
checkBigInt(delay);
// @ts-expect-error
checkThis(this);
return setTimer(cb, delay, args, false);
}

export function setInterval(
this: unknown,
cb: (...args: Args) => void,
delay = 0,
...args: Args
): number {
checkBigInt(delay);
// @ts-expect-error
checkThis(this);
return setTimer(cb, delay, args, true);
}
Expand Down
8 changes: 4 additions & 4 deletions cli/tests/unit/body_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ unitTest(

const body = buildBody(text);

// @ts-expect-error
body.contentType = "multipart/form-data;boundary=boundary";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(body as any).contentType = "multipart/form-data;boundary=boundary";

const formData = await body.formData();
assert(formData.has("field_1"));
Expand All @@ -62,8 +62,8 @@ unitTest(

const body = buildBody(text);

// @ts-expect-error
body.contentType = "application/x-www-form-urlencoded";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(body as any).contentType = "application/x-www-form-urlencoded";

const formData = await body.formData();
assert(formData.has("field_1"));
Expand Down
13 changes: 9 additions & 4 deletions cli/tests/unit/dispatch_json_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ unitTest(
}
);

/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-explicit-any,no-var */
declare global {
namespace Deno {
var core: any;
}
}
/* eslint-enable */

unitTest(function malformedJsonControlBuffer(): void {
// @ts-expect-error
const opId = Deno.core.ops()["op_open"];
// @ts-expect-error
const res = Deno.core.send(opId, new Uint8Array([1, 2, 3, 4, 5]));
const resText = new TextDecoder().decode(res);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const resJson = JSON.parse(resText) as any;
const resJson = JSON.parse(resText);
assert(!resJson.ok);
assert(resJson.err);
});
10 changes: 8 additions & 2 deletions cli/tests/unit/dispatch_minimal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ unitTest(async function sendAsyncStackTrace(): Promise<void> {
}
});

/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-explicit-any,no-var */
declare global {
namespace Deno {
var core: any;
}
}
/* eslint-enable */

unitTest(function malformedMinimalControlBuffer(): void {
// @ts-expect-error
const readOpId = Deno.core.ops()["op_read"];
// @ts-expect-error
const res = Deno.core.send(readOpId, new Uint8Array([1, 2, 3, 4, 5]));
const header = res.slice(0, 12);
const buf32 = new Int32Array(
Expand Down
8 changes: 3 additions & 5 deletions cli/tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ unitTest({ perms: { net: true } }, async function fetchBodyUsed(): Promise<
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
assertEquals(response.bodyUsed, false);
assertThrows((): void => {
// Assigning to read-only property throws in the strict mode.
// @ts-expect-error
response.bodyUsed = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(response as any).bodyUsed = true;
});
await response.blob();
assertEquals(response.bodyUsed, true);
Expand Down Expand Up @@ -657,10 +656,9 @@ unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise<
assert(_json);

// All calls after the body was consumed, should fail
const methods = ["json", "text", "formData", "arrayBuffer"];
const methods = ["json", "text", "formData", "arrayBuffer"] as const;
for (const method of methods) {
try {
// @ts-expect-error
await response[method]();
fail(
"Reading body multiple times should failed, the stream should've been locked."
Expand Down
8 changes: 4 additions & 4 deletions cli/tests/unit/files_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ unitTest(
// writing null should throw an error
let err;
try {
// @ts-expect-error
await file.write(null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await file.write(null as any);
} catch (e) {
err = e;
}
Expand Down Expand Up @@ -322,8 +322,8 @@ unitTest(
// reading file into null buffer should throw an error
let err;
try {
// @ts-expect-error
await file.read(null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await file.read(null as any);
} catch (e) {
err = e;
}
Expand Down
28 changes: 14 additions & 14 deletions cli/tests/unit/form_data_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ unitTest(function formDataParamsGetSuccess(): void {
formData.append("a", "true");
formData.append("b", "false");
formData.append("a", "null");
// @ts-expect-error
formData.append("d", undefined);
// @ts-expect-error
formData.append("e", null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formData.append("d", undefined as any);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formData.append("e", null as any);
assertEquals(formData.get("a"), "true");
assertEquals(formData.get("b"), "false");
assertEquals(formData.get("c"), null);
Expand All @@ -70,11 +70,11 @@ unitTest(function formDataParamsSetSuccess(): void {
assertEquals(formData.getAll("b"), ["false"]);
formData.set("a", "false");
assertEquals(formData.getAll("a"), ["false"]);
// @ts-expect-error
formData.set("d", undefined);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formData.set("d", undefined as any);
assertEquals(formData.get("d"), "undefined");
// @ts-expect-error
formData.set("e", null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formData.set("e", null as any);
assertEquals(formData.get("e"), "null");
});

Expand Down Expand Up @@ -143,8 +143,8 @@ unitTest(function formDataParamsArgumentsCheck(): void {
let hasThrown = 0;
let errMsg = "";
try {
// @ts-expect-error
formData[method]();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(formData as any)[method]();
hasThrown = 1;
} catch (err) {
errMsg = err.message;
Expand All @@ -167,8 +167,8 @@ unitTest(function formDataParamsArgumentsCheck(): void {
let errMsg = "";

try {
// @ts-expect-error
formData[method]();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(formData as any)[method]();
hasThrown = 1;
} catch (err) {
errMsg = err.message;
Expand All @@ -187,8 +187,8 @@ unitTest(function formDataParamsArgumentsCheck(): void {
hasThrown = 0;
errMsg = "";
try {
// @ts-expect-error
formData[method]("foo");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(formData as any)[method]("foo");
hasThrown = 1;
} catch (err) {
errMsg = err.message;
Expand Down
Loading

0 comments on commit 3fe6bc1

Please sign in to comment.