Skip to content

Commit 201070d

Browse files
committed
add some eslint-typescript rules
1 parent 79e1df0 commit 201070d

File tree

11 files changed

+32
-21
lines changed

11 files changed

+32
-21
lines changed

.eslintrc.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,23 @@ module.exports = {
4242
],
4343
"@typescript-eslint/restrict-plus-operands": ["warn", { "checkCompoundAssignments": true }],
4444
"@typescript-eslint/no-throw-literal": "warn",
45+
"@typescript-eslint/unbound-method": "warn",
46+
"@typescript-eslint/explicit-module-boundary-types": "warn",
4547
"@typescript-eslint/no-extra-semi": "warn",
4648
"@typescript-eslint/no-extra-non-null-assertion": "warn",
4749
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
4850
"@typescript-eslint/no-use-before-define": "warn",
4951
"@typescript-eslint/no-for-in-array": "warn",
52+
"@typescript-eslint/no-unsafe-argument": "warn",
53+
"@typescript-eslint/no-unsafe-call": "warn",
54+
// "@typescript-eslint/no-unsafe-member-access": "warn", // too strict, especially for testing
5055
"@typescript-eslint/no-unnecessary-condition": ["warn", { "allowConstantLoopConditions": true }],
56+
"@typescript-eslint/no-unnecessary-type-constraint": "warn",
5157
"@typescript-eslint/no-implied-eval": "warn",
5258
"@typescript-eslint/no-non-null-asserted-optional-chain": "warn",
5359
"@typescript-eslint/no-invalid-void-type": "warn",
5460
"@typescript-eslint/no-loss-of-precision": "warn",
5561
"@typescript-eslint/no-confusing-void-expression": "warn",
56-
"@typescript-eslint/no-unnecessary-type-constraint": "warn",
5762
"@typescript-eslint/prefer-for-of": "warn",
5863
"@typescript-eslint/prefer-includes": "warn",
5964
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
@@ -62,9 +67,9 @@ module.exports = {
6267
"@typescript-eslint/prefer-nullish-coalescing": "warn",
6368
"@typescript-eslint/prefer-optional-chain": "warn",
6469
"@typescript-eslint/prefer-ts-expect-error": "warn",
70+
"@typescript-eslint/prefer-regexp-exec": "warn",
6571

6672
"@typescript-eslint/indent": "off",
67-
"@typescript-eslint/explicit-module-boundary-types": "off",
6873
"@typescript-eslint/no-explicit-any": "off",
6974
"@typescript-eslint/no-empty-interface": "off",
7075
"@typescript-eslint/no-empty-function": "off",

src/CachedKeyDecoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class CachedKeyDecoder implements KeyDecoder {
2626
}
2727
}
2828

29-
public canBeCached(byteLength: number) {
29+
public canBeCached(byteLength: number): boolean {
3030
return byteLength > 0 && byteLength <= this.maxKeyLength;
3131
}
3232

src/Decoder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const DataViewIndexOutOfBoundsError: typeof Error = (() => {
4848
// IE11: The spec says it should throw RangeError,
4949
// IE11: but in IE11 it throws TypeError.
5050
EMPTY_VIEW.getInt8(0);
51-
} catch (e) {
51+
} catch (e: any) {
5252
return e.constructor;
5353
}
5454
throw new Error("never reached");
@@ -65,7 +65,7 @@ export class DecodeError extends Error {
6565
super(message);
6666

6767
// fix the prototype chain in a cross-platform way
68-
const proto = Object.create(DecodeError.prototype);
68+
const proto: typeof DecodeError.prototype = Object.create(DecodeError.prototype);
6969
Object.setPrototypeOf(this, proto);
7070

7171
Object.defineProperty(this, "name", {
@@ -188,11 +188,11 @@ export class Decoder<ContextType = undefined> {
188188
);
189189
}
190190

191-
public decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>) {
191+
public decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>): AsyncGenerator<unknown, void, unknown> {
192192
return this.decodeMultiAsync(stream, true);
193193
}
194194

195-
public decodeStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>) {
195+
public decodeStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>): AsyncGenerator<unknown, void, unknown> {
196196
return this.decodeMultiAsync(stream, false);
197197
}
198198

src/decodeAsync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function decodeAsync<ContextType>(
2626
export function decodeArrayStream<ContextType>(
2727
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
2828
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
29-
) {
29+
): AsyncGenerator<unknown, void, unknown> {
3030
const stream = ensureAsyncIterable(streamLike);
3131

3232
const decoder = new Decoder(
@@ -45,7 +45,7 @@ export function decodeArrayStream<ContextType>(
4545
export function decodeMultiStream<ContextType>(
4646
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
4747
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
48-
) {
48+
): AsyncGenerator<unknown, void, unknown> {
4949
const stream = ensureAsyncIterable(streamLike);
5050

5151
const decoder = new Decoder(
@@ -67,6 +67,6 @@ export function decodeMultiStream<ContextType>(
6767
export function decodeStream<ContextType>(
6868
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
6969
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
70-
) {
70+
): AsyncGenerator<unknown, void, unknown> {
7171
return decodeMultiStream(streamLike, options);
7272
}

src/utils/int.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export function setInt64(view: DataView, offset: number, value: number): void {
1515
view.setUint32(offset + 4, low);
1616
}
1717

18-
export function getInt64(view: DataView, offset: number) {
18+
export function getInt64(view: DataView, offset: number): number {
1919
const high = view.getInt32(offset);
2020
const low = view.getUint32(offset + 4);
2121
return high * 0x1_0000_0000 + low;
2222
}
2323

24-
export function getUint64(view: DataView, offset: number) {
24+
export function getUint64(view: DataView, offset: number): number {
2525
const high = view.getUint32(offset);
2626
const low = view.getUint32(offset + 4);
2727
return high * 0x1_0000_0000 + low;

src/utils/typedArrays.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function ensureUint8Array(buffer: ArrayLike<number> | Uint8Array | ArrayBufferView | ArrayBuffer) {
1+
export function ensureUint8Array(buffer: ArrayLike<number> | Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array {
22
if (buffer instanceof Uint8Array) {
33
return buffer;
44
} else if (ArrayBuffer.isView(buffer)) {

test/codec-float.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import assert from "assert";
22
import { decode } from "../src";
3+
// ieee754 module's type declarations are too strict
4+
// https://github.com/feross/ieee754/pull/38
5+
// @ts-nocheck
6+
import * as ieee754 from "ieee754";
37

4-
const ieee754 = require("ieee754");
58

69
const FLOAT32_TYPE = 0xca;
710
const FLOAT64_TYPE = 0xcb;

test/decode-blob.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import assert from "assert";
22
import "web-streams-polyfill";
33
import { encode, decode, decodeAsync } from "@msgpack/msgpack";
44

5-
const MyBlob = typeof Blob !== "undefined" ? Blob : require("blob-polyfill").Blob;
5+
const MyBlob: typeof Blob = typeof Blob !== "undefined" ? Blob : require("blob-polyfill").Blob;
66

77
describe("Blob", () => {
88
it("decodes it with `decode()`", async function () {
99
const blob = new MyBlob([encode("Hello!")]);
10+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1011
if (!blob.arrayBuffer) {
1112
this.skip();
1213
}
@@ -15,6 +16,7 @@ describe("Blob", () => {
1516

1617
it("decodes it with `decodeAsync()`", async function () {
1718
const blob = new MyBlob([encode("Hello!")]);
19+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1820
if (!blob.stream) {
1921
this.skip();
2022
}

test/edge-cases.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// kind of hand-written fuzzing data
22
// any errors should not break Encoder/Decoder instance states
33
import assert from "assert";
4-
import { encode, decode, decodeAsync, Encoder, Decoder } from "../src";
4+
import { encode, decode, Encoder, Decoder } from "../src";
55
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";
66

77
function testEncoder(encoder: Encoder): void {

test/karma-run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-call */
12
// the util module requires process.env
23
(globalThis as any).process = {
34
env: {},

0 commit comments

Comments
 (0)