Skip to content

Commit

Permalink
squash! Update codebase to support TypeScript strict mode
Browse files Browse the repository at this point in the history
Account for code review comments
  • Loading branch information
maxmellen committed Feb 11, 2020
1 parent 16ab35c commit f9eccc4
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 95 deletions.
80 changes: 47 additions & 33 deletions cli/js/buffer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This code has been ported almost directly from Go's src/bytes/buffer_test.go
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
import { assertEquals, test } from "./test_util.ts";
import { assert, assertEquals, test } from "./test_util.ts";

const { Buffer, readAll, readAllSync, writeAll, writeAllSync } = Deno;
type Buffer = Deno.Buffer;
Expand Down Expand Up @@ -78,12 +78,16 @@ function repeat(c: string, bytes: number): Uint8Array {

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

test(async function bufferBasicOperations(): Promise<void> {
init();
assert(testBytes);
assert(testString);
const buf = new Buffer();
for (let i = 0; i < 5; i++) {
check(buf, "");
Expand All @@ -94,25 +98,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 Down Expand Up @@ -161,11 +165,13 @@ test(async function bufferTooLargeByteWrites(): Promise<void> {

test(async function bufferLargeByteReads(): Promise<void> {
init();
assert(testBytes);
assert(testString);
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 @@ -177,34 +183,38 @@ test(function bufferCapWithPreallocatedSlice(): void {

test(async function bufferReadFrom(): Promise<void> {
init();
assert(testBytes);
assert(testString);
const buf = new Buffer();
for (let i = 3; i < 30; i += 3) {
const s = await fillBytes(
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);
}
});

test(async function bufferReadFromSync(): Promise<void> {
init();
assert(testBytes);
assert(testString);
const buf = new Buffer();
for (let i = 3; i < 30; i += 3) {
const s = await fillBytes(
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 +246,46 @@ test(async function bufferTestGrow(): Promise<void> {

test(async function testReadAll(): Promise<void> {
init();
const reader = new Buffer(testBytes!.buffer as ArrayBuffer);
assert(testBytes);
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);
assert(testBytes);
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();
assert(testBytes);
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();
assert(testBytes);
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]);
}
});
26 changes: 17 additions & 9 deletions cli/js/chmod_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEquals } from "./test_util.ts";
import { testPerm, assert, assertEquals } from "./test_util.ts";

const isNotWindows = Deno.build.os !== "win";

Expand All @@ -16,7 +16,8 @@ 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);
assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777);
}
});

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

let symlinkInfo = Deno.lstatSync(symlinkName);
const symlinkMode = symlinkInfo.mode! & 0o777; // platform dependent
assert(symlinkInfo.mode);
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);
assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(symlinkInfo.mode! & 0o777, symlinkMode);
assert(symlinkInfo.mode);
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
}
);
}
Expand Down Expand Up @@ -86,7 +90,8 @@ 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);
assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777);
}
});

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

let symlinkInfo = Deno.lstatSync(symlinkName);
const symlinkMode = symlinkInfo.mode! & 0o777; // platform dependent
assert(symlinkInfo.mode);
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);
assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(symlinkInfo.mode! & 0o777, symlinkMode);
assert(symlinkInfo.mode);
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
}
);
}
Expand Down
14 changes: 11 additions & 3 deletions cli/js/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ test(function consoleTestWithVariousOrInvalidFormatSpecifier(): void {
});

test(function consoleTestCallToStringOnLabel(): void {
const methods = ["count", "countReset", "time", "timeLog", "timeEnd"];
const methods = [
"count",
"countReset",
"time",
"timeLog",
"timeEnd"
] as const;

for (const method of methods) {
let hasCalled = false;
Expand Down Expand Up @@ -452,6 +458,7 @@ test(function consoleGroup(): void {
// console.group with console.warn test
test(function consoleGroupWarn(): void {
mockConsole((console, _out, _err, both): void => {
assert(both);
console.warn("1");
console.group();
console.warn("2");
Expand All @@ -465,7 +472,7 @@ test(function consoleGroupWarn(): void {
console.warn("6");
console.warn("7");
assertEquals(
both!.toString(),
both.toString(),
`1
2
3
Expand Down Expand Up @@ -695,6 +702,7 @@ 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);
assert(err.toString().includes("Trace: custom message"));
});
});
12 changes: 6 additions & 6 deletions cli/js/event_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { test, assertEquals, assertNotEquals } from "./test_util.ts";
import { test, assertEquals, assert } from "./test_util.ts";

test(function eventInitializedWithType(): void {
const type = "click";
Expand Down Expand Up @@ -70,8 +70,8 @@ test(function eventPreventDefaultSuccess(): void {
});

test(function eventInitializedWithNonStringType(): void {
const type = undefined;
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const type: any = undefined;
const event = new Event(type);

assertEquals(event.isTrusted, false);
Expand All @@ -85,11 +85,11 @@ test(function eventInitializedWithNonStringType(): void {
// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js
test(function eventIsTrusted(): void {
const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assertNotEquals(desc1, undefined);
assertEquals(typeof desc1!.get, "function");
assert(desc1);
assertEquals(typeof desc1.get, "function");

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

assertEquals(desc1!.get, desc2!.get);
Expand Down
9 changes: 4 additions & 5 deletions cli/js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ testPerm({ net: true }, async function responseClone(): Promise<void> {
assert(response !== response1);
assertEquals(response.status, response1.status);
assertEquals(response.statusText, response1.statusText);
const ab = await response.arrayBuffer();
const ab1 = await response1.arrayBuffer();
for (let i = 0; i < ab.byteLength; i++) {
// @ts-ignore
assertEquals(ab[i], ab1[i]);
const u8a = new Uint8Array(await response.arrayBuffer());
const u8a1 = new Uint8Array(await response1.arrayBuffer());
for (let i = 0; i < u8a.byteLength; i++) {
assertEquals(u8a[i], u8a1[i]);
}
});

Expand Down
12 changes: 9 additions & 3 deletions cli/js/form_data_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ test(function formDataParamsForEachSuccess(): void {
});

test(function formDataParamsArgumentsCheck(): void {
const methodRequireOneParam = ["delete", "getAll", "get", "has", "forEach"];

const methodRequireTwoParams = ["append", "set"];
const methodRequireOneParam = [
"delete",
"getAll",
"get",
"has",
"forEach"
] as const;

const methodRequireTwoParams = ["append", "set"] as const;

methodRequireOneParam.forEach((method): void => {
const formData = new FormData();
Expand Down
3 changes: 2 additions & 1 deletion cli/js/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ testPerm({ env: true }, function getDir(): void {
if (Deno.build.os !== r.os) continue;
if (r.shouldHaveValue) {
const d = Deno.dir(s.kind);
assert(d!.length > 0);
assert(d);
assert(d.length > 0);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion cli/js/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ testPerm({ run: true }, async function runStdinPiped(): Promise<void> {
args: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
stdin: "piped"
});
assert(p.stdin);
assert(!p.stdout);
assert(!p.stderr);

const msg = new TextEncoder().encode("hello");
const n = await p.stdin!.write(msg);
const n = await p.stdin.write(msg);
assertEquals(n, msg.byteLength);

p.stdin!.close();
Expand Down
2 changes: 1 addition & 1 deletion cli/js/timers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function deferred(): {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reject: (reason?: any) => void;
} {
let resolve: ((value?: {} | PromiseLike<{}>) => void) | undefined = undefined;
let resolve: (value?: {} | PromiseLike<{}>) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let reject: ((reason?: any) => void) | undefined = undefined;
const promise = new Promise<{}>((res, rej): void => {
Expand Down
Loading

0 comments on commit f9eccc4

Please sign in to comment.