Skip to content

Commit f1372a2

Browse files
committed
Added ESLint linting. (+more)
- Minimized the package content. - Added more keywords to the README.
1 parent ea059c6 commit f1372a2

19 files changed

+2915
-147
lines changed

.eslintignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
.eslintrc.js
4+
rollup.config.js
5+
examples

.eslintrc.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
parserOptions: {
5+
tsconfigRootDir: __dirname,
6+
project: ['./tsconfig.json', './tsconfig.test.json'],
7+
},
8+
plugins: [
9+
'@typescript-eslint',
10+
],
11+
extends: [
12+
'eslint:recommended',
13+
'plugin:@typescript-eslint/recommended',
14+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
15+
],
16+
rules: {
17+
'@typescript-eslint/no-explicit-any': 'off',
18+
'@typescript-eslint/no-unused-vars': ['error', { vars: 'all', args: 'after-used', ignoreRestSiblings: false }]
19+
},
20+
};

examples/binaryMesh/schemas.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export const Mesh = object({
1616
});
1717

1818
// Helpful for the top-most level element
19-
export type Mesh = Parsed<typeof Mesh>;
19+
export type Mesh = Parsed<typeof Mesh>;

package-lock.json

+2,810-97
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+30-5
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,44 @@
66
"module": "dist/esm/index.js",
77
"types": "dist/index.d.ts",
88
"scripts": {
9+
"lint": "eslint . --ext .ts",
910
"build": "rollup -c",
10-
"prepublishOnly": "npm run build",
11+
"prepublishOnly": "npm run lint && npm run build",
1112
"dryPublish": "npm publish --dry-run",
1213
"test": "cross-env TS_NODE_PROJECT=\"tsconfig.test.json\" mocha --reporter spec --require ts-node/register src/**/*.test.ts",
1314
"test-single": "cross-env TS_NODE_PROJECT=\"tsconfig.test.json\" mocha --reporter spec --require ts-node/register"
1415
},
1516
"keywords": [
1617
"typescript",
18+
"type",
19+
"types",
20+
"typed",
1721
"binary",
18-
"structure"
22+
"bin",
23+
"data",
24+
"structure",
25+
"schema",
26+
"backend",
27+
"front-end",
28+
"json",
29+
"arraybuffer",
30+
"buffer",
31+
"encoding",
32+
"decoding",
33+
"serial",
34+
"serialize",
35+
"serialization",
36+
"deserialization",
37+
"deserialize"
1938
],
2039
"files": [
21-
"tsconfig.test.json",
2240
"README.md",
23-
"src",
24-
"dist"
41+
"LICENSE",
42+
"dist/index.d.ts",
43+
"dist/cjs/index.js",
44+
"dist/cjs/index.js.map",
45+
"dist/esm/index.js",
46+
"dist/esm/index.js.map"
2547
],
2648
"author": "Iwo Plaza <[email protected]>",
2749
"license": "MIT",
@@ -31,8 +53,11 @@
3153
"@rollup/plugin-typescript": "^8.3.0",
3254
"@types/chai": "^4.3.0",
3355
"@types/mocha": "^9.0.0",
56+
"@typescript-eslint/eslint-plugin": "^5.10.1",
57+
"@typescript-eslint/parser": "^5.10.1",
3458
"chai": "^4.3.4",
3559
"cross-env": "^7.0.3",
60+
"eslint": "^8.7.0",
3661
"mocha": "^9.1.3",
3762
"rollup": "^2.63.0",
3863
"rollup-plugin-dts": "^4.1.0",

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default [
2828
resolve(),
2929
commonjs(),
3030
typescript({ tsconfig: './tsconfig.json' }),
31-
terser()
31+
terser(),
3232
]
3333
},
3434
{

src/describe/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ObjectSchema, CharsSchema, SubTypeKey } from '../structure';
22
import { ArraySchema } from '../structure/array';
33
import { OptionalSchema } from '../structure/optional';
4-
import { GenericObjectSchema, ObjectSchemaMap } from '../structure/object';
4+
import { GenericObjectSchema } from '../structure/object';
55
import { TupleSchema } from '../structure/tuple';
66
import { Schema, SchemaProperties } from '../structure/types';
77
import { ValueOrProvider } from '../utilityTypes';
@@ -16,14 +16,14 @@ export const object = <P extends SchemaProperties>(properties: P) =>
1616

1717
export const generic = <P extends SchemaProperties, S extends {[key in keyof S]: ObjectSchema<any>}>(properties: P, subTypeMap: ValueOrProvider<S>) =>
1818
new GenericObjectSchema(
19-
SubTypeKey.STRING as const as any,
19+
SubTypeKey.STRING as any,
2020
properties,
2121
subTypeMap
2222
);
2323

2424
export const genericEnum = <P extends SchemaProperties, S extends {[key in keyof S]: ObjectSchema<any>}>(properties: P, subTypeMap: ValueOrProvider<S>) =>
2525
new GenericObjectSchema(
26-
SubTypeKey.ENUM as const as any,
26+
SubTypeKey.ENUM as any,
2727
properties,
2828
subTypeMap
2929
);

src/io/bufferReader.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class BufferReader implements ISerialInput {
88
private readonly helperByteView: Uint8Array;
99
private readonly switchEndianness: boolean;
1010

11-
private byteOffset: number = 0;
11+
private byteOffset = 0;
1212

1313
constructor(buffer: ArrayBufferLike) {
1414
if (buffer instanceof Buffer) {
@@ -19,7 +19,7 @@ export class BufferReader implements ISerialInput {
1919
this.uint8View = new Uint8Array(buffer, 0);
2020
this.byteOffset = 0;
2121

22-
let helperBuffer = new ArrayBuffer(4);
22+
const helperBuffer = new ArrayBuffer(4);
2323
this.helperIntView = new Int32Array(helperBuffer);
2424
this.helperFloatView = new Float32Array(helperBuffer);
2525
this.helperByteView = new Uint8Array(helperBuffer);

src/io/bufferReaderWriter.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as mocha from 'mocha';
21
import * as chai from 'chai';
32
import { BufferWriter } from './bufferWriter';
43
import { BufferReader } from './bufferReader';
@@ -20,7 +19,7 @@ describe('BufferWriter/BufferReader', () => {
2019
const writer = new BufferWriter(buffer);
2120

2221
// Writing the ints
23-
for (let int of intList) {
22+
for (const int of intList) {
2423
writer.writeInt(int);
2524
}
2625

@@ -48,7 +47,7 @@ describe('BufferWriter/BufferReader', () => {
4847
const writer = new BufferWriter(buffer);
4948

5049
// Writing the ints
51-
for (let float of floatList) {
50+
for (const float of floatList) {
5251
writer.writeFloat(float);
5352
}
5453

src/io/bufferWriter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class BufferWriter implements ISerialOutput {
88
private readonly helperByteView: Uint8Array;
99
private readonly switchEndianness: boolean;
1010

11-
private byteOffset: number = 0;
11+
private byteOffset = 0;
1212

1313
constructor(buffer: ArrayBufferLike) {
1414
if (buffer instanceof Buffer) {
@@ -19,7 +19,7 @@ export class BufferWriter implements ISerialOutput {
1919
this.uint8View = new Uint8Array(buffer, 0);
2020
this.byteOffset = 0;
2121

22-
let helperBuffer = new ArrayBuffer(4);
22+
const helperBuffer = new ArrayBuffer(4);
2323
this.helperIntView = new Int32Array(helperBuffer);
2424
this.helperFloatView = new Float32Array(helperBuffer);
2525
this.helperByteView = new Uint8Array(helperBuffer);

src/parsed.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { generic, arrayOf, INT, STRING, BOOL, object, Parsed, genericEnum } from
66
const enum ExpressionType {
77
ADD = 0,
88
NEGATE = 1,
9-
};
9+
}
1010

1111
export const Expression =
1212
genericEnum({}, () => ({
@@ -19,7 +19,7 @@ export const Expression =
1919
}),
2020
}));
2121
type Expression = Parsed<typeof Expression>;
22-
const expr = {} as Expression;
22+
// const expr = {} as Expression;
2323

2424

2525
export const KeyframeNodeTemplate =
@@ -40,4 +40,4 @@ export const KeyframeNodeTemplate =
4040
});
4141

4242
type KeyframeNodeTemplate = Parsed<typeof KeyframeNodeTemplate>;
43-
const s = {} as KeyframeNodeTemplate;
43+
// const s = {} as KeyframeNodeTemplate;

src/structure/array.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const expect = chai.expect;
88
describe('(read/write)Array', () => {
99
it('should encode and decode a simple int array', () => {
1010
const length = randIntBetween(0, 5);
11-
let value = [];
11+
const value = [];
1212
for (let i = 0; i < length; ++i) {
1313
value.push(randIntBetween(-10000, 10000));
1414
}

src/structure/array.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ export class ArraySchema<T> extends Schema<T[]> {
2929
}
3030

3131
sizeOf(values: T[]): number {
32-
const len = values.length;
33-
3432
// Length encoding
35-
let size = INT.sizeOf(len);
33+
let size = INT.sizeOf();
3634
// Values encoding
37-
size += values.map((v: any) => this.elementType.sizeOf(v)).reduce((a, b) => a + b, 0);
35+
size += values.map((v) => this.elementType.sizeOf(v)).reduce((a, b) => a + b, 0);
3836

3937
return size;
4038
}

src/structure/baseTypes.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as mocha from 'mocha';
21
import * as chai from 'chai';
32
import { randBetween, randIntBetween } from '../test/random';
43
import { BufferReader, BufferWriter } from '../io';
54
import { Schema } from './types';
65
import { BOOL, BYTE, INT, FLOAT, STRING, } from '.';
6+
import { Parsed } from '../utilityTypes';
77

88

9-
function encodeAndDecode<T extends Schema<any>, P extends T['_infered']>(description: T, value: P, bufferSize: number): P {
9+
function encodeAndDecode<T extends Schema<Parsed<T>>>(description: T, value: Parsed<T>, bufferSize: number): Parsed<T> {
1010
const buffer = Buffer.alloc(bufferSize);
1111
description.write(new BufferWriter(buffer), value);
1212

src/structure/baseTypes.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class BoolSchema extends Schema<boolean> {
1414
output.writeBool(value);
1515
}
1616

17-
sizeOf(value: boolean): number {
17+
sizeOf(): number {
1818
return 1;
1919
}
2020
}
@@ -54,7 +54,7 @@ export class ByteSchema extends Schema<number> {
5454
output.writeByte(value);
5555
}
5656

57-
sizeOf(value: number): number {
57+
sizeOf(): number {
5858
return 1;
5959
}
6060
}
@@ -74,7 +74,7 @@ export class IntSchema extends Schema<number> {
7474
output.writeInt(value);
7575
}
7676

77-
sizeOf(value: number): number {
77+
sizeOf(): number {
7878
return 4;
7979
}
8080
}
@@ -94,7 +94,7 @@ export class FloatSchema extends Schema<number> {
9494
output.writeFloat(value);
9595
}
9696

97-
sizeOf(_: number): number {
97+
sizeOf(): number {
9898
return 4;
9999
}
100100
}

src/structure/chars.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CharsSchema extends Schema<string> {
2727
return content;
2828
}
2929

30-
sizeOf(value: string): number {
30+
sizeOf(): number {
3131
return this.length;
3232
}
3333
}

src/structure/object.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ export class ObjectSchema<T extends SchemaProperties, O extends InferedPropertie
2323

2424
read(input: ISerialInput): O {
2525
// Sorting the keys in ASCII ascending order, so that the order is platform independent.
26-
const keys: string[] = Object.keys(this.properties).sort();
27-
let result: any = {};
26+
const keys: (keyof T)[] = Object.keys(this.properties).sort();
27+
const result = {} as O;
2828

2929
for (const key of keys) {
30-
result[key] = this.properties[key].read(input);
30+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
31+
result[key] = this.properties[key].read(input) as O[typeof key];
3132
}
3233

3334
return result;
@@ -45,16 +46,16 @@ export class ObjectSchema<T extends SchemaProperties, O extends InferedPropertie
4546
}
4647
}
4748

48-
type InferedSubTypes<T extends {[key in keyof T]: ObjectSchema<any>}> = {
49+
type InferedSubTypes<T extends {[key in keyof T]: ObjectSchema<SchemaProperties>}> = {
4950
[Key in keyof T]: T[Key]['_infered'] & { type: Key }
5051
};
5152

5253
export type ObjectSchemaMap<S, SI extends {[key in keyof SI]: SI[key]}> = {[key in keyof S]: ObjectSchema<SI[key]>};
5354

5455
export class GenericObjectSchema<
5556
T extends SchemaProperties, // Base properties
56-
S extends {[key in keyof S]: ObjectSchema<any>}, // Sub type map
57-
K extends (keyof S extends string ? SubTypeKey.STRING : SubTypeKey.ENUM)
57+
S extends {[Key in keyof S]: ObjectSchema<SchemaProperties>}, // Sub type map
58+
K extends ((keyof S) extends string ? SubTypeKey.STRING : SubTypeKey.ENUM)
5859
> extends ObjectSchema<T, InferedProperties<T> & InferedSubTypes<S>[keyof S]> {
5960
constructor(
6061
public readonly keyedBy: K,
@@ -72,7 +73,7 @@ export class GenericObjectSchema<
7273
// Figuring out sub-types
7374
const subTypeDescription = this.getSubTypeMap()[value.type] || null;
7475
if (subTypeDescription === null) {
75-
throw new Error(`Unknown sub-type '${value.type}' in among '${JSON.stringify(Object.keys(this.subTypeMap))}'`);
76+
throw new Error(`Unknown sub-type '${value.type.toString()}' in among '${JSON.stringify(Object.keys(this.subTypeMap))}'`);
7677
}
7778

7879
// Writing the sub-type out.
@@ -87,10 +88,12 @@ export class GenericObjectSchema<
8788
super.write(output, value);
8889

8990
// Extra sub-type fields
91+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
9092
const extraKeys: string[] = Object.keys(subTypeDescription.properties).sort();
9193

9294
for (const key of extraKeys) {
93-
const prop = subTypeDescription.properties[key];
95+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
96+
const prop: Schema<unknown> = subTypeDescription.properties[key];
9497

9598
prop.write(output, value[key]);
9699
}
@@ -105,18 +108,18 @@ export class GenericObjectSchema<
105108
throw new Error(`Unknown sub-type '${subTypeKey}' in among '${JSON.stringify(Object.keys(subTypeMap))}'`);
106109
}
107110

108-
let result: any = super.read(input);
111+
const result = super.read(input);
109112

110113
// Making the sub type key available to the result object.
111-
result.type = subTypeKey;
114+
result.type = subTypeKey as keyof S;
112115

113116
if (subTypeDescription !== null) {
114-
const extraKeys: string[] = Object.keys(subTypeDescription.properties).sort();
117+
const extraKeys = Object.keys(subTypeDescription.properties).sort();
115118

116119
for (const key of extraKeys) {
117120
const prop = (subTypeDescription.properties)[key];
118-
119-
result[key] = prop.read(input);
121+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
122+
(result as any)[key] = prop.read(input);
120123
}
121124
}
122125

@@ -132,12 +135,12 @@ export class GenericObjectSchema<
132135
// Extra sub-type fields
133136
const subTypeDescription = this.getSubTypeMap()[value.type] || null;
134137
if (subTypeDescription === null) {
135-
throw new Error(`Unknown sub-type '${value.type}' in among '${JSON.stringify(Object.keys(this.subTypeMap))}'`);
138+
throw new Error(`Unknown sub-type '${value.type.toString()}' in among '${JSON.stringify(Object.keys(this.subTypeMap))}'`);
136139
}
137140

138141
size += Object.keys(subTypeDescription.properties) // Going through extra property keys
139142
.map(key => subTypeDescription.properties[key].sizeOf(value[key])) // Mapping extra properties into their sizes
140-
.reduce((a, b) => a + b); // Summing them up
143+
.reduce((a, b) => a + b, 0); // Summing them up
141144

142145
return size;
143146
}

0 commit comments

Comments
 (0)