Skip to content

Commit

Permalink
Update codebase to support TypeScript strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmellen committed Feb 11, 2020
1 parent 2b380e0 commit 16ab35c
Show file tree
Hide file tree
Showing 48 changed files with 257 additions and 189 deletions.
2 changes: 1 addition & 1 deletion cli/compilers/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ mod tests {
.unwrap()
.code
.as_bytes()
.starts_with(b"console.log(\"Hello World\");"));
.starts_with(b"\"use strict\";\nconsole.log(\"Hello World\");"));
}

#[tokio::test]
Expand Down
1 change: 1 addition & 0 deletions cli/compilers/wasm_wrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
const importObject = Object.create(null);
//IMPORTS

Expand Down
6 changes: 3 additions & 3 deletions cli/js/body_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ testPerm({ net: true }, async function bodyMultipartFormData(): Promise<void> {

const formData = await body.formData();
assert(formData.has("field_1"));
assertEquals(formData.get("field_1").toString(), "value_1 \r\n");
assertEquals(formData.get("field_1")!.toString(), "value_1 \r\n");
assert(formData.has("field_2"));
});

Expand All @@ -62,7 +62,7 @@ testPerm({ net: true }, async function bodyURLEncodedFormData(): Promise<void> {

const formData = await body.formData();
assert(formData.has("field_1"));
assertEquals(formData.get("field_1").toString(), "Hi");
assertEquals(formData.get("field_1")!.toString(), "Hi");
assert(formData.has("field_2"));
assertEquals(formData.get("field_2").toString(), "<Deno>");
assertEquals(formData.get("field_2")!.toString(), "<Deno>");
});
68 changes: 34 additions & 34 deletions cli/js/buffer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function repeat(c: string, bytes: number): Uint8Array {

test(function bufferNewBuffer(): void {
init();
const buf = new Buffer(testBytes.buffer as ArrayBuffer);
check(buf, testString);
const buf = new Buffer(testBytes!.buffer as ArrayBuffer);
check(buf, testString!);
});

test(async function bufferBasicOperations(): Promise<void> {
Expand All @@ -94,25 +94,25 @@ test(async function bufferBasicOperations(): Promise<void> {
buf.truncate(0);
check(buf, "");

let n = await buf.write(testBytes.subarray(0, 1));
let n = await buf.write(testBytes!.subarray(0, 1));
assertEquals(n, 1);
check(buf, "a");

n = await buf.write(testBytes.subarray(1, 2));
n = await buf.write(testBytes!.subarray(1, 2));
assertEquals(n, 1);
check(buf, "ab");

n = await buf.write(testBytes.subarray(2, 26));
n = await buf.write(testBytes!.subarray(2, 26));
assertEquals(n, 24);
check(buf, testString.slice(0, 26));
check(buf, testString!.slice(0, 26));

buf.truncate(26);
check(buf, testString.slice(0, 26));
check(buf, testString!.slice(0, 26));

buf.truncate(20);
check(buf, testString.slice(0, 20));
check(buf, testString!.slice(0, 20));

await empty(buf, testString.slice(0, 20), new Uint8Array(5));
await empty(buf, testString!.slice(0, 20), new Uint8Array(5));
await empty(buf, "", new Uint8Array(100));

// TODO buf.writeByte()
Expand All @@ -134,8 +134,8 @@ test(async function bufferLargeByteWrites(): Promise<void> {
const buf = new Buffer();
const limit = 9;
for (let i = 3; i < limit; i += 3) {
const s = await fillBytes(buf, "", 5, testBytes);
await empty(buf, s, new Uint8Array(Math.floor(testString.length / i)));
const s = await fillBytes(buf, "", 5, testBytes!);
await empty(buf, s, new Uint8Array(Math.floor(testString!.length / i)));
}
check(buf, "");
});
Expand Down Expand Up @@ -163,9 +163,9 @@ test(async function bufferLargeByteReads(): Promise<void> {
init();
const buf = new Buffer();
for (let i = 3; i < 30; i += 3) {
const n = Math.floor(testBytes.byteLength / i);
const s = await fillBytes(buf, "", 5, testBytes.subarray(0, n));
await empty(buf, s, new Uint8Array(testString.length));
const n = Math.floor(testBytes!.byteLength / i);
const s = await fillBytes(buf, "", 5, testBytes!.subarray(0, n));
await empty(buf, s, new Uint8Array(testString!.length));
}
check(buf, "");
});
Expand All @@ -183,11 +183,11 @@ test(async function bufferReadFrom(): Promise<void> {
buf,
"",
5,
testBytes.subarray(0, Math.floor(testBytes.byteLength / i))
testBytes!.subarray(0, Math.floor(testBytes!.byteLength / i))
);
const b = new Buffer();
await b.readFrom(buf);
const fub = new Uint8Array(testString.length);
const fub = new Uint8Array(testString!.length);
await empty(b, s, fub);
}
});
Expand All @@ -200,11 +200,11 @@ test(async function bufferReadFromSync(): Promise<void> {
buf,
"",
5,
testBytes.subarray(0, Math.floor(testBytes.byteLength / i))
testBytes!.subarray(0, Math.floor(testBytes!.byteLength / i))
);
const b = new Buffer();
b.readFromSync(buf);
const fub = new Uint8Array(testString.length);
const fub = new Uint8Array(testString!.length);
await empty(b, s, fub);
}
});
Expand Down Expand Up @@ -236,42 +236,42 @@ test(async function bufferTestGrow(): Promise<void> {

test(async function testReadAll(): Promise<void> {
init();
const reader = new Buffer(testBytes.buffer as ArrayBuffer);
const reader = new Buffer(testBytes!.buffer as ArrayBuffer);
const actualBytes = await readAll(reader);
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
assertEquals(testBytes!.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes!.length; ++i) {
assertEquals(testBytes![i], actualBytes[i]);
}
});

test(function testReadAllSync(): void {
init();
const reader = new Buffer(testBytes.buffer as ArrayBuffer);
const reader = new Buffer(testBytes!.buffer as ArrayBuffer);
const actualBytes = readAllSync(reader);
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
assertEquals(testBytes!.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes!.length; ++i) {
assertEquals(testBytes![i], actualBytes[i]);
}
});

test(async function testWriteAll(): Promise<void> {
init();
const writer = new Buffer();
await writeAll(writer, testBytes);
await writeAll(writer, testBytes!);
const actualBytes = writer.bytes();
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
assertEquals(testBytes!.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes!.length; ++i) {
assertEquals(testBytes![i], actualBytes[i]);
}
});

test(function testWriteAllSync(): void {
init();
const writer = new Buffer();
writeAllSync(writer, testBytes);
writeAllSync(writer, testBytes!);
const actualBytes = writer.bytes();
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
assertEquals(testBytes!.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes!.length; ++i) {
assertEquals(testBytes![i], actualBytes[i]);
}
});
16 changes: 8 additions & 8 deletions cli/js/chmod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ testPerm({ read: true, write: true }, function chmodSyncSuccess(): void {
// Check success when not on windows
if (isNotWindows) {
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.mode & 0o777, 0o777);
assertEquals(fileInfo.mode! & 0o777, 0o777);
}
});

Expand All @@ -35,15 +35,15 @@ if (isNotWindows) {
Deno.symlinkSync(filename, symlinkName);

let symlinkInfo = Deno.lstatSync(symlinkName);
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
const symlinkMode = symlinkInfo.mode! & 0o777; // platform dependent

Deno.chmodSync(symlinkName, 0o777);

// Change actual file mode, not symlink
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.mode & 0o777, 0o777);
assertEquals(fileInfo.mode! & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
assertEquals(symlinkInfo.mode! & 0o777, symlinkMode);
}
);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ testPerm({ read: true, write: true }, async function chmodSuccess(): Promise<
// Check success when not on windows
if (isNotWindows) {
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.mode & 0o777, 0o777);
assertEquals(fileInfo.mode! & 0o777, 0o777);
}
});

Expand All @@ -105,15 +105,15 @@ if (isNotWindows) {
Deno.symlinkSync(filename, symlinkName);

let symlinkInfo = Deno.lstatSync(symlinkName);
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
const symlinkMode = symlinkInfo.mode! & 0o777; // platform dependent

await Deno.chmod(symlinkName, 0o777);

// Just change actual file mode, not symlink
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.mode & 0o777, 0o777);
assertEquals(fileInfo.mode! & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
assertEquals(symlinkInfo.mode! & 0o777, symlinkMode);
}
);
}
Expand Down
9 changes: 5 additions & 4 deletions cli/js/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const customInspect = Deno.symbols.customInspect;
const {
Console,
stringifyArgs
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = Deno[Deno.symbols.internal] as any;
// @ts-ignore
} = Deno[Deno.symbols.internal];

function stringify(...args: unknown[]): string {
return stringifyArgs(args).replace(/\n$/, "");
Expand Down Expand Up @@ -306,6 +306,7 @@ test(function consoleTestCallToStringOnLabel(): void {
for (const method of methods) {
let hasCalled = false;

// @ts-ignore
console[method]({
toString(): void {
hasCalled = true;
Expand Down Expand Up @@ -464,7 +465,7 @@ test(function consoleGroupWarn(): void {
console.warn("6");
console.warn("7");
assertEquals(
both.toString(),
both!.toString(),
`1
2
3
Expand Down Expand Up @@ -694,6 +695,6 @@ test(function consoleDirXml(): void {
test(function consoleTrace(): void {
mockConsole((console, _out, err): void => {
console.trace("%s", "custom message");
assert(err.toString().includes("Trace: custom message"));
assert(err!.toString().includes("Trace: custom message"));
});
});
4 changes: 2 additions & 2 deletions cli/js/error_stack_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { test, assert } from "./test_util.ts";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { setPrepareStackTrace } = Deno[Deno.symbols.internal] as any;
// @ts-ignore
const { setPrepareStackTrace } = Deno[Deno.symbols.internal];

interface CallSite {
getThis(): unknown;
Expand Down
22 changes: 18 additions & 4 deletions cli/js/event_target_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { test, assertEquals } from "./test_util.ts";
test(function addEventListenerTest(): void {
const document = new EventTarget();

// @ts-ignore
assertEquals(document.addEventListener("x", null, false), undefined);
// @ts-ignore
assertEquals(document.addEventListener("x", null, true), undefined);
// @ts-ignore
assertEquals(document.addEventListener("x", null), undefined);
});

Expand All @@ -14,7 +17,7 @@ test(function constructedEventTargetCanBeUsedAsExpected(): void {
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;

const listener = (e): void => {
const listener = (e: Event): void => {
assertEquals(e, event);
++callCount;
};
Expand All @@ -34,11 +37,19 @@ test(function constructedEventTargetCanBeUsedAsExpected(): void {

test(function anEventTargetCanBeSubclassed(): void {
class NicerEventTarget extends EventTarget {
on(type, callback?, options?): void {
on(
type: string,
callback: (e: Event) => void,
options?: __domTypes.AddEventListenerOptions
): void {
this.addEventListener(type, callback, options);
}

off(type, callback?, options?): void {
off(
type: string,
callback: (e: Event) => void,
options?: __domTypes.EventListenerOptions
): void {
this.removeEventListener(type, callback, options);
}
}
Expand All @@ -60,8 +71,11 @@ test(function anEventTargetCanBeSubclassed(): void {

test(function removingNullEventListenerShouldSucceed(): void {
const document = new EventTarget();
// @ts-ignore
assertEquals(document.removeEventListener("x", null, false), undefined);
// @ts-ignore
assertEquals(document.removeEventListener("x", null, true), undefined);
// @ts-ignore
assertEquals(document.removeEventListener("x", null), undefined);
});

Expand All @@ -70,7 +84,7 @@ test(function constructedEventTargetUseObjectPrototype(): void {
const event = new Event("toString", { bubbles: true, cancelable: false });
let callCount = 0;

const listener = (e): void => {
const listener = (e: Event): void => {
assertEquals(e, event);
++callCount;
};
Expand Down
7 changes: 4 additions & 3 deletions cli/js/event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ test(function eventPreventDefaultSuccess(): void {

test(function eventInitializedWithNonStringType(): void {
const type = undefined;
// @ts-ignore
const event = new Event(type);

assertEquals(event.isTrusted, false);
Expand All @@ -85,11 +86,11 @@ test(function eventInitializedWithNonStringType(): void {
test(function eventIsTrusted(): void {
const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assertNotEquals(desc1, undefined);
assertEquals(typeof desc1.get, "function");
assertEquals(typeof desc1!.get, "function");

const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assertNotEquals(desc2, undefined);
assertEquals(typeof desc2.get, "function");
assertEquals(typeof desc2!.get, "function");

assertEquals(desc1.get, desc2.get);
assertEquals(desc1!.get, desc2!.get);
});
Loading

0 comments on commit 16ab35c

Please sign in to comment.