Skip to content

Commit 760300d

Browse files
authored
Merge pull request #151 from msgpack/enable_noUncheckedIndexedAccess
enable noUncheckedIndexedAccess for stricter type checking
2 parents ac10a3d + 16facb0 commit 760300d

12 files changed

+20
-31
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ module.exports = {
4141
{ "selector": "typeLike", "format": ["PascalCase"], "leadingUnderscore": "allow" },
4242
],
4343
"@typescript-eslint/restrict-plus-operands": ["warn", { "checkCompoundAssignments": true }],
44-
"@typescript-eslint/no-non-null-assertion": "warn", // NOTE: pay attention to it because it may cause unexpected behavior
4544
"@typescript-eslint/no-throw-literal": "warn",
4645
"@typescript-eslint/no-extra-semi": "warn",
4746
"@typescript-eslint/no-extra-non-null-assertion": "warn",
@@ -70,6 +69,7 @@ module.exports = {
7069
"@typescript-eslint/no-empty-interface": "off",
7170
"@typescript-eslint/no-empty-function": "off",
7271
"@typescript-eslint/no-var-requires": "off",
72+
"@typescript-eslint/no-non-null-assertion": "off",
7373
"@typescript-eslint/ban-ts-comment": "off",
7474
},
7575
};

src/CachedKeyDecoder.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ export class CachedKeyDecoder implements KeyDecoder {
3131
}
3232

3333
private get(bytes: Uint8Array, inputOffset: number, byteLength: number): string | null {
34-
const records = this.caches[byteLength - 1];
35-
const recordsLength = records.length;
34+
const records = this.caches[byteLength - 1]!;
3635

37-
FIND_CHUNK: for (let i = 0; i < recordsLength; i++) {
38-
const record = records[i];
36+
FIND_CHUNK: for (const record of records) {
3937
const recordBytes = record.bytes;
4038

4139
for (let j = 0; j < byteLength; j++) {
@@ -49,7 +47,7 @@ export class CachedKeyDecoder implements KeyDecoder {
4947
}
5048

5149
private store(bytes: Uint8Array, value: string) {
52-
const records = this.caches[bytes.length - 1];
50+
const records = this.caches[bytes.length - 1]!;
5351
const record: KeyCacheRecord = { bytes, value };
5452

5553
if (records.length >= this.maxLengthPerKey) {

src/Decoder.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Decoder<ContextType> {
7878
private readonly maxMapLength = DEFAULT_MAX_LENGTH,
7979
private readonly maxExtLength = DEFAULT_MAX_LENGTH,
8080
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
81-
) { }
81+
) {}
8282

8383
private reinitializeState() {
8484
this.totalPos = 0;
@@ -378,7 +378,7 @@ export class Decoder<ContextType> {
378378
const stack = this.stack;
379379
while (stack.length > 0) {
380380
// arrays and maps
381-
const state = stack[stack.length - 1];
381+
const state = stack[stack.length - 1]!;
382382
if (state.type === State.ARRAY) {
383383
state.array[state.position] = object;
384384
state.position++;
@@ -399,7 +399,6 @@ export class Decoder<ContextType> {
399399
} else {
400400
// it must be `state.type === State.MAP_VALUE` here
401401

402-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
403402
state.map[state.key!] = object;
404403
state.readCount++;
405404

@@ -500,7 +499,7 @@ export class Decoder<ContextType> {
500499

501500
private stateIsMapKey(): boolean {
502501
if (this.stack.length > 0) {
503-
const state = this.stack[this.stack.length - 1];
502+
const state = this.stack[this.stack.length - 1]!;
504503
return state.type === State.MAP_KEY;
505504
}
506505
return false;

src/context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable @typescript-eslint/ban-types */
22

3-
export type SplitTypes<T, U> = U extends T
4-
? Exclude<T, U> extends never ? T : Exclude<T, U>
5-
: T;
3+
export type SplitTypes<T, U> = U extends T ? (Exclude<T, U> extends never ? T : Exclude<T, U>) : T;
64

75
export type SplitUndefined<T> = SplitTypes<T, undefined>;
86

src/utils/utf8.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
9797
: 0;
9898

9999
function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void {
100-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
101100
output.set(sharedTextEncoder!.encode(str), outputOffset);
102101
}
103102

104103
function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void {
105-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
106104
sharedTextEncoder!.encodeInto(str, output.subarray(outputOffset));
107105
}
108106

@@ -117,24 +115,24 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:
117115
const units: Array<number> = [];
118116
let result = "";
119117
while (offset < end) {
120-
const byte1 = bytes[offset++];
118+
const byte1 = bytes[offset++]!;
121119
if ((byte1 & 0x80) === 0) {
122120
// 1 byte
123121
units.push(byte1);
124122
} else if ((byte1 & 0xe0) === 0xc0) {
125123
// 2 bytes
126-
const byte2 = bytes[offset++] & 0x3f;
124+
const byte2 = bytes[offset++]! & 0x3f;
127125
units.push(((byte1 & 0x1f) << 6) | byte2);
128126
} else if ((byte1 & 0xf0) === 0xe0) {
129127
// 3 bytes
130-
const byte2 = bytes[offset++] & 0x3f;
131-
const byte3 = bytes[offset++] & 0x3f;
128+
const byte2 = bytes[offset++]! & 0x3f;
129+
const byte3 = bytes[offset++]! & 0x3f;
132130
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
133131
} else if ((byte1 & 0xf8) === 0xf0) {
134132
// 4 bytes
135-
const byte2 = bytes[offset++] & 0x3f;
136-
const byte3 = bytes[offset++] & 0x3f;
137-
const byte4 = bytes[offset++] & 0x3f;
133+
const byte2 = bytes[offset++]! & 0x3f;
134+
const byte3 = bytes[offset++]! & 0x3f;
135+
const byte4 = bytes[offset++]! & 0x3f;
138136
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
139137
if (unit > 0xffff) {
140138
unit -= 0x10000;
@@ -168,6 +166,5 @@ export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
168166

169167
export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
170168
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
171-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
172169
return sharedTextDecoder!.decode(stringBytes);
173170
}

test/ExtensionCodec.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import assert from "assert";
22
import util from "util";
33
import { encode, decode, ExtensionCodec, EXT_TIMESTAMP, decodeAsync } from "../src";
44

5-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
6-
75
describe("ExtensionCodec", () => {
86
context("timestamp", () => {
97
const defaultCodec = ExtensionCodec.defaultCodec;

test/codec-bigint.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const extensionCodec = new ExtensionCodec();
55
extensionCodec.register({
66
type: 0,
77
encode: (input: unknown) => {
8-
// eslint-disable-next-line valid-typeof
98
if (typeof input === "bigint") {
109
return encode(input.toString());
1110
} else {

test/codec-int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const INT64SPECS = {
1616
describe("codec: int64 / uint64", () => {
1717
context("int 64", () => {
1818
for (const name of Object.keys(INT64SPECS)) {
19-
const value = INT64SPECS[name];
19+
const value = INT64SPECS[name]!;
2020

2121
it(`sets and gets ${value} (${value < 0 ? "-" : ""}0x${Math.abs(value).toString(16)})`, () => {
2222
const b = new Uint8Array(8);

test/codec-timestamp.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const SPECS = {
2828
describe("codec: timestamp 32/64/96", () => {
2929
context("encode / decode", () => {
3030
for (const name of Object.keys(SPECS)) {
31-
const value = SPECS[name];
31+
const value = SPECS[name]!;
3232

3333
it(`encodes and decodes ${name} (${value.toISOString()})`, () => {
3434
const encoded = encode(value);
@@ -45,7 +45,6 @@ describe("codec: timestamp 32/64/96", () => {
4545

4646
context("encodeDateToTimeSpec", () => {
4747
it("decodes timestamp-ext binary to TimeSpec", () => {
48-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
4948
const encoded = encodeTimestampExtension(new Date(42000))!;
5049
assert.deepStrictEqual(decodeTimestampToTimeSpec(encoded), { sec: 42, nsec: 0 });
5150
});

test/msgpack-ext.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("msgpack-ext", () => {
2222
} as Record<string, [number, ExtData]>;
2323

2424
for (const name of Object.keys(SPECS)) {
25-
const [msgpackType, extData] = SPECS[name];
25+
const [msgpackType, extData] = SPECS[name]!;
2626

2727
it(`preserves ExtData by decode(encode(${name}))`, () => {
2828
const encoded = encode(extData);

0 commit comments

Comments
 (0)