Skip to content

Commit

Permalink
Merge pull request msgpack#151 from msgpack/enable_noUncheckedIndexed…
Browse files Browse the repository at this point in the history
…Access

enable noUncheckedIndexedAccess for stricter type checking
  • Loading branch information
gfx authored Jan 3, 2021
2 parents ac10a3d + 16facb0 commit 760300d
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ module.exports = {
{ "selector": "typeLike", "format": ["PascalCase"], "leadingUnderscore": "allow" },
],
"@typescript-eslint/restrict-plus-operands": ["warn", { "checkCompoundAssignments": true }],
"@typescript-eslint/no-non-null-assertion": "warn", // NOTE: pay attention to it because it may cause unexpected behavior
"@typescript-eslint/no-throw-literal": "warn",
"@typescript-eslint/no-extra-semi": "warn",
"@typescript-eslint/no-extra-non-null-assertion": "warn",
Expand Down Expand Up @@ -70,6 +69,7 @@ module.exports = {
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/ban-ts-comment": "off",
},
};
8 changes: 3 additions & 5 deletions src/CachedKeyDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ export class CachedKeyDecoder implements KeyDecoder {
}

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

FIND_CHUNK: for (let i = 0; i < recordsLength; i++) {
const record = records[i];
FIND_CHUNK: for (const record of records) {
const recordBytes = record.bytes;

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

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

if (records.length >= this.maxLengthPerKey) {
Expand Down
7 changes: 3 additions & 4 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Decoder<ContextType> {
private readonly maxMapLength = DEFAULT_MAX_LENGTH,
private readonly maxExtLength = DEFAULT_MAX_LENGTH,
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
) { }
) {}

private reinitializeState() {
this.totalPos = 0;
Expand Down Expand Up @@ -378,7 +378,7 @@ export class Decoder<ContextType> {
const stack = this.stack;
while (stack.length > 0) {
// arrays and maps
const state = stack[stack.length - 1];
const state = stack[stack.length - 1]!;
if (state.type === State.ARRAY) {
state.array[state.position] = object;
state.position++;
Expand All @@ -399,7 +399,6 @@ export class Decoder<ContextType> {
} else {
// it must be `state.type === State.MAP_VALUE` here

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

Expand Down Expand Up @@ -500,7 +499,7 @@ export class Decoder<ContextType> {

private stateIsMapKey(): boolean {
if (this.stack.length > 0) {
const state = this.stack[this.stack.length - 1];
const state = this.stack[this.stack.length - 1]!;
return state.type === State.MAP_KEY;
}
return false;
Expand Down
4 changes: 1 addition & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/ban-types */

export type SplitTypes<T, U> = U extends T
? Exclude<T, U> extends never ? T : Exclude<T, U>
: T;
export type SplitTypes<T, U> = U extends T ? (Exclude<T, U> extends never ? T : Exclude<T, U>) : T;

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

Expand Down
17 changes: 7 additions & 10 deletions src/utils/utf8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
: 0;

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

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

Expand All @@ -117,24 +115,24 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:
const units: Array<number> = [];
let result = "";
while (offset < end) {
const byte1 = bytes[offset++];
const byte1 = bytes[offset++]!;
if ((byte1 & 0x80) === 0) {
// 1 byte
units.push(byte1);
} else if ((byte1 & 0xe0) === 0xc0) {
// 2 bytes
const byte2 = bytes[offset++] & 0x3f;
const byte2 = bytes[offset++]! & 0x3f;
units.push(((byte1 & 0x1f) << 6) | byte2);
} else if ((byte1 & 0xf0) === 0xe0) {
// 3 bytes
const byte2 = bytes[offset++] & 0x3f;
const byte3 = bytes[offset++] & 0x3f;
const byte2 = bytes[offset++]! & 0x3f;
const byte3 = bytes[offset++]! & 0x3f;
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
} else if ((byte1 & 0xf8) === 0xf0) {
// 4 bytes
const byte2 = bytes[offset++] & 0x3f;
const byte3 = bytes[offset++] & 0x3f;
const byte4 = bytes[offset++] & 0x3f;
const byte2 = bytes[offset++]! & 0x3f;
const byte3 = bytes[offset++]! & 0x3f;
const byte4 = bytes[offset++]! & 0x3f;
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
if (unit > 0xffff) {
unit -= 0x10000;
Expand Down Expand Up @@ -168,6 +166,5 @@ export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE

export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return sharedTextDecoder!.decode(stringBytes);
}
2 changes: 0 additions & 2 deletions test/ExtensionCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import assert from "assert";
import util from "util";
import { encode, decode, ExtensionCodec, EXT_TIMESTAMP, decodeAsync } from "../src";

/* eslint-disable @typescript-eslint/no-non-null-assertion */

describe("ExtensionCodec", () => {
context("timestamp", () => {
const defaultCodec = ExtensionCodec.defaultCodec;
Expand Down
1 change: 0 additions & 1 deletion test/codec-bigint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: 0,
encode: (input: unknown) => {
// eslint-disable-next-line valid-typeof
if (typeof input === "bigint") {
return encode(input.toString());
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/codec-int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const INT64SPECS = {
describe("codec: int64 / uint64", () => {
context("int 64", () => {
for (const name of Object.keys(INT64SPECS)) {
const value = INT64SPECS[name];
const value = INT64SPECS[name]!;

it(`sets and gets ${value} (${value < 0 ? "-" : ""}0x${Math.abs(value).toString(16)})`, () => {
const b = new Uint8Array(8);
Expand Down
3 changes: 1 addition & 2 deletions test/codec-timestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const SPECS = {
describe("codec: timestamp 32/64/96", () => {
context("encode / decode", () => {
for (const name of Object.keys(SPECS)) {
const value = SPECS[name];
const value = SPECS[name]!;

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

context("encodeDateToTimeSpec", () => {
it("decodes timestamp-ext binary to TimeSpec", () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const encoded = encodeTimestampExtension(new Date(42000))!;
assert.deepStrictEqual(decodeTimestampToTimeSpec(encoded), { sec: 42, nsec: 0 });
});
Expand Down
2 changes: 1 addition & 1 deletion test/msgpack-ext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("msgpack-ext", () => {
} as Record<string, [number, ExtData]>;

for (const name of Object.keys(SPECS)) {
const [msgpackType, extData] = SPECS[name];
const [msgpackType, extData] = SPECS[name]!;

it(`preserves ExtData by decode(encode(${name}))`, () => {
const encoded = encode(extData);
Expand Down
2 changes: 1 addition & 1 deletion test/msgpack-test-suite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const TEST_TYPES = {
describe("msgpack-test-suite", () => {
Exam.getExams(TEST_TYPES).forEach((exam) => {
const types = exam.getTypes(TEST_TYPES);
const first = types[0];
const first = types[0]!;
const title = `${first}: ${exam.stringify(first)}`;
it(`encodes ${title}`, () => {
types.forEach((type) => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
"noUncheckedIndexedAccess": true,

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand Down

0 comments on commit 760300d

Please sign in to comment.