Skip to content

Commit

Permalink
Add require-await lint rule (denoland/deno#4401)
Browse files Browse the repository at this point in the history
  • Loading branch information
samrith-s committed Mar 20, 2020
1 parent 69fee8d commit 237b472
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 69 deletions.
10 changes: 5 additions & 5 deletions encoding/binary_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ Deno.test(async function testGetNBytes(): Promise<void> {
Deno.test(async function testGetNBytesThrows(): Promise<void> {
const data = new Uint8Array([1, 2, 3, 4]);
const buff = new Deno.Buffer(data.buffer);
assertThrowsAsync(async () => {
await assertThrowsAsync(async () => {
await getNBytes(buff, 8);
}, Deno.errors.UnexpectedEof);
});

Deno.test(async function testPutVarbig(): Promise<void> {
Deno.test(function testPutVarbig(): void {
const buff = new Uint8Array(8);
putVarbig(buff, 0xffeeddccbbaa9988n);
assertEquals(
Expand All @@ -38,7 +38,7 @@ Deno.test(async function testPutVarbig(): Promise<void> {
);
});

Deno.test(async function testPutVarbigLittleEndian(): Promise<void> {
Deno.test(function testPutVarbigLittleEndian(): void {
const buff = new Uint8Array(8);
putVarbig(buff, 0x8899aabbccddeeffn, { endian: "little" });
assertEquals(
Expand All @@ -47,13 +47,13 @@ Deno.test(async function testPutVarbigLittleEndian(): Promise<void> {
);
});

Deno.test(async function testPutVarnum(): Promise<void> {
Deno.test(function testPutVarnum(): void {
const buff = new Uint8Array(4);
putVarnum(buff, 0xffeeddcc);
assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
});

Deno.test(async function testPutVarnumLittleEndian(): Promise<void> {
Deno.test(function testPutVarnumLittleEndian(): void {
const buff = new Uint8Array(4);
putVarnum(buff, 0xccddeeff, { endian: "little" });
assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
Expand Down
2 changes: 1 addition & 1 deletion encoding/yaml/example/sample_document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parse } from "../../yaml.ts";

const { readFileSync, cwd } = Deno;

(async () => {
(() => {
const yml = readFileSync(`${cwd()}/example/sample_document.yml`);

const document = new TextDecoder().decode(yml);
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

const clients = new Map<number, WebSocket>();
let clientId = 0;
async function dispatch(msg: string): Promise<void> {
function dispatch(msg: string): void {
for (const client of clients.values()) {
client.send(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function startServer(): Promise<Deno.Process> {
const r = new TextProtoReader(new BufReader(server.stdout));
const s = await r.readLine();
assert(s !== Deno.EOF && s.includes("chat server starting"));
} catch {
} catch (err) {
server.stdout!.close();
server.close();
}
Expand Down
5 changes: 1 addition & 4 deletions fs/copy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const testdataDir = path.resolve("fs", "testdata");
// TODO(axetroy): Add test for Windows once symlink is implemented for Windows.
const isWindows = Deno.build.os === "win";

async function testCopy(
name: string,
cb: (tempDir: string) => Promise<void>
): Promise<void> {
function testCopy(name: string, cb: (tempDir: string) => Promise<void>): void {
Deno.test({
name,
async fn(): Promise<void> {
Expand Down
17 changes: 9 additions & 8 deletions fs/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const { lstat, lstatSync } = Deno;
* Test whether or not the given path exists by checking with the file system
*/
export async function exists(filePath: string): Promise<boolean> {
return lstat(filePath)
.then((): boolean => true)
.catch((err: Error): boolean => {
if (err instanceof Deno.errors.NotFound) {
return false;
}
try {
await lstat(filePath);
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}

throw err;
});
throw err;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";

const isWindows = Deno.build.os == "win";

export async function testWalk(
export function testWalk(
setup: (arg0: string) => void | Promise<void>,
t: Deno.TestFunction,
ignore = false
): Promise<void> {
): void {
const name = t.name;
async function fn(): Promise<void> {
const origCwd = cwd();
Expand Down
10 changes: 5 additions & 5 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ async function serveDir(
return res;
}

async function serveFallback(req: ServerRequest, e: Error): Promise<Response> {
function serveFallback(req: ServerRequest, e: Error): Promise<Response> {
if (e instanceof Deno.errors.NotFound) {
return {
return Promise.resolve({
status: 404,
body: encoder.encode("Not found")
};
});
} else {
return {
return Promise.resolve({
status: 500,
body: encoder.encode("Internal server error")
};
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions http/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { STATUS_TEXT } from "./http_status.ts";

export function emptyReader(): Deno.Reader {
return {
async read(_: Uint8Array): Promise<number | Deno.EOF> {
return Deno.EOF;
read(_: Uint8Array): Promise<number | Deno.EOF> {
return Promise.resolve(Deno.EOF);
}
};
}
Expand Down
8 changes: 4 additions & 4 deletions http/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export function mockConn(base: Partial<Deno.Conn> = {}): Deno.Conn {
rid: -1,
closeRead: (): void => {},
closeWrite: (): void => {},
read: async (): Promise<number | Deno.EOF> => {
return 0;
read: (): Promise<number | Deno.EOF> => {
return Promise.resolve(0);
},
write: async (): Promise<number> => {
return -1;
write: (): Promise<number> => {
return Promise.resolve(-1);
},
close: (): void => {},
...base
Expand Down
2 changes: 1 addition & 1 deletion http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test(async function responseWrite(): Promise<void> {
}
});

test(async function requestContentLength(): Promise<void> {
test(function requestContentLength(): void {
// Has content length
{
const req = new ServerRequest();
Expand Down
1 change: 1 addition & 0 deletions io/bufio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ export async function* readStringDelim(
}

/** Read strings line-by-line from a Reader. */
// eslint-disable-next-line require-await
export async function* readLines(
reader: Reader
): AsyncIterableIterator<string> {
Expand Down
6 changes: 3 additions & 3 deletions io/bufio_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");
class TestReader implements Reader {
constructor(private data: Uint8Array, private stride: number) {}

async read(buf: Uint8Array): Promise<number | Deno.EOF> {
read(buf: Uint8Array): Promise<number | Deno.EOF> {
let nread = this.stride;
if (nread > this.data.byteLength) {
nread = this.data.byteLength;
Expand All @@ -200,11 +200,11 @@ class TestReader implements Reader {
nread = buf.byteLength;
}
if (nread === 0) {
return Deno.EOF;
return Promise.resolve(Deno.EOF);
}
copyBytes(buf as Uint8Array, this.data);
this.data = this.data.subarray(nread);
return nread;
return Promise.resolve(nread);
}
}

Expand Down
14 changes: 7 additions & 7 deletions io/iotest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ type Reader = Deno.Reader;
export class OneByteReader implements Reader {
constructor(readonly r: Reader) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
read(p: Uint8Array): Promise<number | Deno.EOF> {
if (p.byteLength === 0) {
return 0;
return Promise.resolve(0);
}
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
}
return this.r.read(p.subarray(0, 1));
return Promise.resolve(this.r.read(p.subarray(0, 1)));
}
}

Expand All @@ -27,12 +27,12 @@ export class OneByteReader implements Reader {
export class HalfReader implements Reader {
constructor(readonly r: Reader) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
read(p: Uint8Array): Promise<number | Deno.EOF> {
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
}
const half = Math.floor((p.byteLength + 1) / 2);
return this.r.read(p.subarray(0, half));
return Promise.resolve(this.r.read(p.subarray(0, half)));
}
}

Expand All @@ -43,11 +43,11 @@ export class TimeoutReader implements Reader {
count = 0;
constructor(readonly r: Reader) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
read(p: Uint8Array): Promise<number | Deno.EOF> {
this.count++;
if (this.count === 2) {
throw new Deno.errors.TimedOut();
}
return this.r.read(p);
return Promise.resolve(this.r.read(p));
}
}
8 changes: 4 additions & 4 deletions io/ioutil_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class BinaryReader implements Reader {

constructor(private bytes: Uint8Array = new Uint8Array(0)) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
read(p: Uint8Array): Promise<number | Deno.EOF> {
p.set(this.bytes.subarray(this.index, p.byteLength));
this.index += p.byteLength;
return p.byteLength;
return Promise.resolve(p.byteLength);
}
}

Expand Down Expand Up @@ -52,7 +52,7 @@ Deno.test(async function testReadLong2(): Promise<void> {
assertEquals(long, 0x12345678);
});

Deno.test(async function testSliceLongToBytes(): Promise<void> {
Deno.test(function testSliceLongToBytes(): void {
const arr = sliceLongToBytes(0x1234567890abcdef);
const actual = readLong(new BufReader(new BinaryReader(new Uint8Array(arr))));
const expected = readLong(
Expand All @@ -65,7 +65,7 @@ Deno.test(async function testSliceLongToBytes(): Promise<void> {
assertEquals(actual, expected);
});

Deno.test(async function testSliceLongToBytes2(): Promise<void> {
Deno.test(function testSliceLongToBytes2(): void {
const arr = sliceLongToBytes(0x12345678);
assertEquals(arr, [0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]);
});
Expand Down
6 changes: 3 additions & 3 deletions io/readers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class StringReader implements Reader {

constructor(private readonly s: string) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
read(p: Uint8Array): Promise<number | Deno.EOF> {
const n = Math.min(p.byteLength, this.buf.byteLength - this.offs);
p.set(this.buf.slice(this.offs, this.offs + n));
this.offs += n;
if (n === 0) {
return Deno.EOF;
return Promise.resolve(Deno.EOF);
}
return n;
return Promise.resolve(n);
}
}

Expand Down
4 changes: 2 additions & 2 deletions io/writers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export class StringWriter implements Writer {
this.byteLength += c.byteLength;
}

async write(p: Uint8Array): Promise<number> {
write(p: Uint8Array): Promise<number> {
this.chunks.push(p);
this.byteLength += p.byteLength;
this.cache = undefined;
return p.byteLength;
return Promise.resolve(p.byteLength);
}

toString(): string {
Expand Down
2 changes: 1 addition & 1 deletion node/_fs/_fs_close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { CallbackWithError } from "./_fs_common.ts";

export function close(fd: number, callback: CallbackWithError): void {
new Promise(async (resolve, reject) => {
new Promise((resolve, reject) => {
try {
Deno.close(fd);
resolve();
Expand Down
2 changes: 1 addition & 1 deletion node/_fs/_fs_close_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test({
else resolve();
});
})
.then(async () => {
.then(() => {
assert(!Deno.resources()[file.rid]);
})
.catch(() => {
Expand Down
4 changes: 2 additions & 2 deletions node/_fs/_fs_dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Dirent from "./_fs_dirent.ts";

test({
name: "Closing current directory with callback is successful",
async fn() {
fn() {
let calledBack = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new Dir(".").close((valOrErr: any) => {
Expand All @@ -25,7 +25,7 @@ test({

test({
name: "Closing current directory synchronously works",
async fn() {
fn() {
new Dir(".").closeSync();
}
});
Expand Down
3 changes: 2 additions & 1 deletion testing/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ export async function runBenchmarks({
}

/** Runs specified benchmarks if the enclosing script is main. */
export async function runIfMain(
export function runIfMain(
meta: ImportMeta,
opts: BenchmarkRunOptions = {}
): Promise<void> {
if (meta.main) {
return runBenchmarks(opts);
}
return Promise.resolve(undefined);
}
3 changes: 2 additions & 1 deletion textproto/reader_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ function reader(s: string): TextProtoReader {
test({
ignore: true,
name: "[textproto] Reader : DotBytes",
async fn(): Promise<void> {
fn(): Promise<void> {
const _input =
"dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n";
return Promise.resolve();
}
});

Expand Down
2 changes: 1 addition & 1 deletion textproto/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { append } from "./mod.ts";
import { assertEquals } from "../testing/asserts.ts";
const { test } = Deno;

test(async function textprotoAppend(): Promise<void> {
test(function textprotoAppend(): void {
const enc = new TextEncoder();
const dec = new TextDecoder();
const u1 = enc.encode("Hello ");
Expand Down

0 comments on commit 237b472

Please sign in to comment.