From 5014cf22d2743655ec014400a6b4880afb9433f3 Mon Sep 17 00:00:00 2001 From: Samrith Shankar Date: Fri, 20 Mar 2020 14:38:34 +0100 Subject: [PATCH] Add require-await lint rule (denoland/deno#4401) --- encoding/binary_test.ts | 10 +++++----- encoding/yaml/example/sample_document.ts | 2 +- examples/chat/server.ts | 2 +- examples/chat/server_test.ts | 2 +- fs/copy_test.ts | 5 +---- fs/exists.ts | 17 +++++++++-------- fs/walk_test.ts | 4 ++-- http/file_server.ts | 10 +++++----- http/io.ts | 4 ++-- http/mock.ts | 8 ++++---- http/server_test.ts | 2 +- io/bufio.ts | 1 + io/bufio_test.ts | 6 +++--- io/iotest.ts | 14 +++++++------- io/ioutil_test.ts | 8 ++++---- io/readers.ts | 6 +++--- io/writers.ts | 4 ++-- node/_fs/_fs_close.ts | 2 +- node/_fs/_fs_close_test.ts | 2 +- node/_fs/_fs_dir_test.ts | 4 ++-- testing/bench.ts | 3 ++- textproto/reader_test.ts | 3 ++- textproto/test.ts | 2 +- util/async_test.ts | 7 ++++++- ws/mod.ts | 4 ++-- ws/test.ts | 12 ++++++------ 26 files changed, 75 insertions(+), 69 deletions(-) diff --git a/encoding/binary_test.ts b/encoding/binary_test.ts index 936fcbb6368c..54f8cbded5f3 100644 --- a/encoding/binary_test.ts +++ b/encoding/binary_test.ts @@ -24,12 +24,12 @@ Deno.test(async function testGetNBytes(): Promise { Deno.test(async function testGetNBytesThrows(): Promise { 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 { +Deno.test(function testPutVarbig(): void { const buff = new Uint8Array(8); putVarbig(buff, 0xffeeddccbbaa9988n); assertEquals( @@ -38,7 +38,7 @@ Deno.test(async function testPutVarbig(): Promise { ); }); -Deno.test(async function testPutVarbigLittleEndian(): Promise { +Deno.test(function testPutVarbigLittleEndian(): void { const buff = new Uint8Array(8); putVarbig(buff, 0x8899aabbccddeeffn, { endian: "little" }); assertEquals( @@ -47,13 +47,13 @@ Deno.test(async function testPutVarbigLittleEndian(): Promise { ); }); -Deno.test(async function testPutVarnum(): Promise { +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 { +Deno.test(function testPutVarnumLittleEndian(): void { const buff = new Uint8Array(4); putVarnum(buff, 0xccddeeff, { endian: "little" }); assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc])); diff --git a/encoding/yaml/example/sample_document.ts b/encoding/yaml/example/sample_document.ts index c9372e8ec397..da969d6794db 100644 --- a/encoding/yaml/example/sample_document.ts +++ b/encoding/yaml/example/sample_document.ts @@ -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); diff --git a/examples/chat/server.ts b/examples/chat/server.ts index 3c968e44e174..08aede05bbc6 100644 --- a/examples/chat/server.ts +++ b/examples/chat/server.ts @@ -8,7 +8,7 @@ import { const clients = new Map(); let clientId = 0; -async function dispatch(msg: string): Promise { +function dispatch(msg: string): void { for (const client of clients.values()) { client.send(msg); } diff --git a/examples/chat/server_test.ts b/examples/chat/server_test.ts index 899eb1b322e0..0d21b3787833 100644 --- a/examples/chat/server_test.ts +++ b/examples/chat/server_test.ts @@ -18,7 +18,7 @@ async function startServer(): Promise { 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(); } diff --git a/fs/copy_test.ts b/fs/copy_test.ts index fb8827f0c749..9ba5f2b2135d 100644 --- a/fs/copy_test.ts +++ b/fs/copy_test.ts @@ -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 -): Promise { +function testCopy(name: string, cb: (tempDir: string) => Promise): void { Deno.test({ name, async fn(): Promise { diff --git a/fs/exists.ts b/fs/exists.ts index ae64cb1f3f82..f9e5a0925345 100644 --- a/fs/exists.ts +++ b/fs/exists.ts @@ -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 { - 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; + } } /** diff --git a/fs/walk_test.ts b/fs/walk_test.ts index c2518cee32fd..96bfcae67571 100644 --- a/fs/walk_test.ts +++ b/fs/walk_test.ts @@ -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, t: Deno.TestFunction, ignore = false -): Promise { +): void { const name = t.name; async function fn(): Promise { const origCwd = cwd(); diff --git a/http/file_server.ts b/http/file_server.ts index 18a68aa49d40..20d5d61e64ec 100755 --- a/http/file_server.ts +++ b/http/file_server.ts @@ -158,17 +158,17 @@ async function serveDir( return res; } -async function serveFallback(req: ServerRequest, e: Error): Promise { +function serveFallback(req: ServerRequest, e: Error): Promise { 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") - }; + }); } } diff --git a/http/io.ts b/http/io.ts index 5518146a807d..6d5d1f66537c 100644 --- a/http/io.ts +++ b/http/io.ts @@ -7,8 +7,8 @@ import { STATUS_TEXT } from "./http_status.ts"; export function emptyReader(): Deno.Reader { return { - async read(_: Uint8Array): Promise { - return Deno.EOF; + read(_: Uint8Array): Promise { + return Promise.resolve(Deno.EOF); } }; } diff --git a/http/mock.ts b/http/mock.ts index 3a4eeed821e4..cee697bed899 100644 --- a/http/mock.ts +++ b/http/mock.ts @@ -14,11 +14,11 @@ export function mockConn(base: Partial = {}): Deno.Conn { rid: -1, closeRead: (): void => {}, closeWrite: (): void => {}, - read: async (): Promise => { - return 0; + read: (): Promise => { + return Promise.resolve(0); }, - write: async (): Promise => { - return -1; + write: (): Promise => { + return Promise.resolve(-1); }, close: (): void => {}, ...base diff --git a/http/server_test.ts b/http/server_test.ts index 0a4986dcf14c..2a7c46134b8d 100644 --- a/http/server_test.ts +++ b/http/server_test.ts @@ -68,7 +68,7 @@ test(async function responseWrite(): Promise { } }); -test(async function requestContentLength(): Promise { +test(function requestContentLength(): void { // Has content length { const req = new ServerRequest(); diff --git a/io/bufio.ts b/io/bufio.ts index 12b5c2d2b65a..a944adfed4cb 100644 --- a/io/bufio.ts +++ b/io/bufio.ts @@ -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 { diff --git a/io/bufio_test.ts b/io/bufio_test.ts index d925175f12bd..ae38f66673f3 100644 --- a/io/bufio_test.ts +++ b/io/bufio_test.ts @@ -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 { + read(buf: Uint8Array): Promise { let nread = this.stride; if (nread > this.data.byteLength) { nread = this.data.byteLength; @@ -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); } } diff --git a/io/iotest.ts b/io/iotest.ts index 1e922e33fe4f..e2cf315aa8d8 100644 --- a/io/iotest.ts +++ b/io/iotest.ts @@ -10,14 +10,14 @@ type Reader = Deno.Reader; export class OneByteReader implements Reader { constructor(readonly r: Reader) {} - async read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { 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))); } } @@ -27,12 +27,12 @@ export class OneByteReader implements Reader { export class HalfReader implements Reader { constructor(readonly r: Reader) {} - async read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { 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))); } } @@ -43,11 +43,11 @@ export class TimeoutReader implements Reader { count = 0; constructor(readonly r: Reader) {} - async read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { this.count++; if (this.count === 2) { throw new Deno.errors.TimedOut(); } - return this.r.read(p); + return Promise.resolve(this.r.read(p)); } } diff --git a/io/ioutil_test.ts b/io/ioutil_test.ts index 261f4def0d3b..3d85a0fa174a 100644 --- a/io/ioutil_test.ts +++ b/io/ioutil_test.ts @@ -17,10 +17,10 @@ class BinaryReader implements Reader { constructor(private bytes: Uint8Array = new Uint8Array(0)) {} - async read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { p.set(this.bytes.subarray(this.index, p.byteLength)); this.index += p.byteLength; - return p.byteLength; + return Promise.resolve(p.byteLength); } } @@ -52,7 +52,7 @@ Deno.test(async function testReadLong2(): Promise { assertEquals(long, 0x12345678); }); -Deno.test(async function testSliceLongToBytes(): Promise { +Deno.test(function testSliceLongToBytes(): void { const arr = sliceLongToBytes(0x1234567890abcdef); const actual = readLong(new BufReader(new BinaryReader(new Uint8Array(arr)))); const expected = readLong( @@ -65,7 +65,7 @@ Deno.test(async function testSliceLongToBytes(): Promise { assertEquals(actual, expected); }); -Deno.test(async function testSliceLongToBytes2(): Promise { +Deno.test(function testSliceLongToBytes2(): void { const arr = sliceLongToBytes(0x12345678); assertEquals(arr, [0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]); }); diff --git a/io/readers.ts b/io/readers.ts index 5b1bf194853e..2d81e246f14b 100644 --- a/io/readers.ts +++ b/io/readers.ts @@ -9,14 +9,14 @@ export class StringReader implements Reader { constructor(private readonly s: string) {} - async read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { 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); } } diff --git a/io/writers.ts b/io/writers.ts index 722e0297f247..b9a6a5e70ee9 100644 --- a/io/writers.ts +++ b/io/writers.ts @@ -14,11 +14,11 @@ export class StringWriter implements Writer { this.byteLength += c.byteLength; } - async write(p: Uint8Array): Promise { + write(p: Uint8Array): Promise { this.chunks.push(p); this.byteLength += p.byteLength; this.cache = undefined; - return p.byteLength; + return Promise.resolve(p.byteLength); } toString(): string { diff --git a/node/_fs/_fs_close.ts b/node/_fs/_fs_close.ts index e19eb932ec25..4ec10eefb8d6 100644 --- a/node/_fs/_fs_close.ts +++ b/node/_fs/_fs_close.ts @@ -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(); diff --git a/node/_fs/_fs_close_test.ts b/node/_fs/_fs_close_test.ts index 2c71172488e3..7c6dd684cd80 100644 --- a/node/_fs/_fs_close_test.ts +++ b/node/_fs/_fs_close_test.ts @@ -16,7 +16,7 @@ test({ else resolve(); }); }) - .then(async () => { + .then(() => { assert(!Deno.resources()[file.rid]); }) .catch(() => { diff --git a/node/_fs/_fs_dir_test.ts b/node/_fs/_fs_dir_test.ts index 3b8c5b341302..be276887bde1 100644 --- a/node/_fs/_fs_dir_test.ts +++ b/node/_fs/_fs_dir_test.ts @@ -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) => { @@ -25,7 +25,7 @@ test({ test({ name: "Closing current directory synchronously works", - async fn() { + fn() { new Dir(".").closeSync(); } }); diff --git a/testing/bench.ts b/testing/bench.ts index 5cec3ea39687..1c4b01c0b81f 100644 --- a/testing/bench.ts +++ b/testing/bench.ts @@ -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 { if (meta.main) { return runBenchmarks(opts); } + return Promise.resolve(undefined); } diff --git a/textproto/reader_test.ts b/textproto/reader_test.ts index 7aead064b774..f0ae63894dde 100644 --- a/textproto/reader_test.ts +++ b/textproto/reader_test.ts @@ -21,9 +21,10 @@ function reader(s: string): TextProtoReader { test({ ignore: true, name: "[textproto] Reader : DotBytes", - async fn(): Promise { + fn(): Promise { const _input = "dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n"; + return Promise.resolve(); } }); diff --git a/textproto/test.ts b/textproto/test.ts index 5b67bbf95641..9a992a2cf6f5 100644 --- a/textproto/test.ts +++ b/textproto/test.ts @@ -7,7 +7,7 @@ import { append } from "./mod.ts"; import { assertEquals } from "../testing/asserts.ts"; const { test } = Deno; -test(async function textprotoAppend(): Promise { +test(function textprotoAppend(): void { const enc = new TextEncoder(); const dec = new TextDecoder(); const u1 = enc.encode("Hello "); diff --git a/util/async_test.ts b/util/async_test.ts index 4607cf6cd245..d81b52b43e79 100644 --- a/util/async_test.ts +++ b/util/async_test.ts @@ -3,17 +3,20 @@ const { test } = Deno; import { assert, assertEquals, assertStrictEq } from "../testing/asserts.ts"; import { collectUint8Arrays, deferred, MuxAsyncIterator } from "./async.ts"; -test(async function asyncDeferred(): Promise { +test(function asyncDeferred(): Promise { const d = deferred(); d.resolve(12); + return Promise.resolve(); }); +// eslint-disable-next-line require-await async function* gen123(): AsyncIterableIterator { yield 1; yield 2; yield 3; } +// eslint-disable-next-line require-await async function* gen456(): AsyncIterableIterator { yield 4; yield 5; @@ -47,6 +50,7 @@ test(async function collectUint8Arrays0(): Promise { test(async function collectUint8Arrays1(): Promise { const buf = new Uint8Array([1, 2, 3]); + // eslint-disable-next-line require-await async function* gen(): AsyncIterableIterator { yield buf; } @@ -56,6 +60,7 @@ test(async function collectUint8Arrays1(): Promise { }); test(async function collectUint8Arrays4(): Promise { + // eslint-disable-next-line require-await async function* gen(): AsyncIterableIterator { yield new Uint8Array([1, 2, 3]); yield new Uint8Array([]); diff --git a/ws/mod.ts b/ws/mod.ts index 809654a06e6d..3332ed8dd22b 100644 --- a/ws/mod.ts +++ b/ws/mod.ts @@ -322,7 +322,7 @@ class WebSocketImpl implements WebSocket { return d; } - async send(data: WebSocketMessage): Promise { + send(data: WebSocketMessage): Promise { const opcode = typeof data === "string" ? OpCode.TextFrame : OpCode.BinaryFrame; const payload = typeof data === "string" ? encode(data) : data; @@ -336,7 +336,7 @@ class WebSocketImpl implements WebSocket { return this.enqueue(frame); } - async ping(data: WebSocketMessage = ""): Promise { + ping(data: WebSocketMessage = ""): Promise { const payload = typeof data === "string" ? encode(data) : data; const frame = { isLastFrame: true, diff --git a/ws/test.ts b/ws/test.ts index 820fe1423d5a..f1efa8e409b1 100644 --- a/ws/test.ts +++ b/ws/test.ts @@ -126,7 +126,7 @@ test("[ws] read unmasked bigger binary frame", async () => { assertEquals(bin.payload.length, payloadLength); }); -test("[ws] createSecAccept", async () => { +test("[ws] createSecAccept", () => { const nonce = "dGhlIHNhbXBsZSBub25jZQ=="; const d = createSecAccept(nonce); assertEquals(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="); @@ -335,8 +335,8 @@ test("[ws] createSecKeyHasCorrectLength", () => { test("[ws] WebSocket should throw `Deno.errors.ConnectionReset` when peer closed connection without close frame", async () => { const buf = new Buffer(); const eofReader: Deno.Reader = { - async read(_: Uint8Array): Promise { - return Deno.EOF; + read(_: Uint8Array): Promise { + return Promise.resolve(Deno.EOF); } }; const conn = dummyConn(eofReader, buf); @@ -353,8 +353,8 @@ test("[ws] WebSocket should throw `Deno.errors.ConnectionReset` when peer closed test("[ws] WebSocket shouldn't throw `Deno.errors.UnexpectedEof` on recive()", async () => { const buf = new Buffer(); const eofReader: Deno.Reader = { - async read(_: Uint8Array): Promise { - return Deno.EOF; + read(_: Uint8Array): Promise { + return Promise.resolve(Deno.EOF); } }; const conn = dummyConn(eofReader, buf); @@ -372,7 +372,7 @@ test({ const buf = new Buffer(); let timer: number | undefined; const lazyWriter: Deno.Writer = { - async write(_: Uint8Array): Promise { + write(_: Uint8Array): Promise { return new Promise(resolve => { timer = setTimeout(() => resolve(0), 1000); });