Skip to content

Commit 5e70517

Browse files
committed
chore(NODE-4321): add typescript to eslint rules
1 parent 9613021 commit 5e70517

18 files changed

+1516
-1498
lines changed

.eslintrc.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"plugin:prettier/recommended",
66
"plugin:@typescript-eslint/eslint-recommended",
77
"plugin:@typescript-eslint/recommended",
8-
"prettier/@typescript-eslint"
8+
"plugin:@typescript-eslint/recommended-requiring-type-checking"
99
],
1010
"env": {
1111
"node": true
@@ -17,8 +17,12 @@
1717
"Set": true,
1818
"Map": false
1919
},
20+
"parser": "@typescript-eslint/parser",
2021
"parserOptions": {
21-
"ecmaVersion": 2017
22+
"ecmaVersion": 2017,
23+
"project": [
24+
"./tsconfig.json"
25+
]
2226
},
2327
"plugins": [
2428
"prettier",
@@ -52,6 +56,11 @@
5256
"Function": false
5357
}
5458
}
55-
]
59+
],
60+
"@typescript-eslint/no-unsafe-member-access": "off",
61+
"@typescript-eslint/no-unsafe-assignment": "off",
62+
"@typescript-eslint/no-unsafe-return": "off",
63+
"@typescript-eslint/no-unsafe-argument": "off",
64+
"@typescript-eslint/no-unsafe-call": "off"
5665
}
5766
}

package-lock.json

Lines changed: 1436 additions & 1443 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@
3131
"@babel/plugin-external-helpers": "^7.10.4",
3232
"@babel/preset-env": "^7.11.0",
3333
"@istanbuljs/nyc-config-typescript": "^1.0.1",
34-
"@microsoft/api-extractor": "^7.11.2",
34+
"@microsoft/api-extractor": "^7.28.0",
3535
"@rollup/plugin-babel": "^5.2.0",
3636
"@rollup/plugin-commonjs": "^15.0.0",
3737
"@rollup/plugin-json": "^4.1.0",
3838
"@rollup/plugin-node-resolve": "^9.0.0",
3939
"@rollup/plugin-replace": "^4.0.0",
4040
"@rollup/plugin-typescript": "^6.0.0",
41-
"@typescript-eslint/eslint-plugin": "^3.10.1",
42-
"@typescript-eslint/parser": "^3.10.1",
41+
"@types/node": "^18.0.0",
42+
"@typescript-eslint/eslint-plugin": "^5.30.0",
43+
"@typescript-eslint/parser": "^5.30.0",
4344
"array-includes": "^3.1.3",
4445
"benchmark": "^2.1.4",
4546
"chai": "^4.2.0",
46-
"eslint": "^7.7.0",
47-
"eslint-config-prettier": "^6.11.0",
48-
"eslint-plugin-prettier": "^3.1.4",
49-
"eslint-plugin-tsdoc": "^0.2.6",
47+
"eslint": "^8.18.0",
48+
"eslint-config-prettier": "^8.5.0",
49+
"eslint-plugin-prettier": "^4.1.0",
50+
"eslint-plugin-tsdoc": "^0.2.16",
5051
"karma": "^6.3.4",
5152
"karma-chai": "^0.1.0",
5253
"karma-chrome-launcher": "^3.1.0",
@@ -56,19 +57,18 @@
5657
"mocha": "5.2.0",
5758
"node-fetch": "^2.6.1",
5859
"nyc": "^15.1.0",
59-
"object.entries": "^1.1.4",
60-
"prettier": "^2.1.1",
60+
"object.entries": "^1.1.5",
61+
"prettier": "^2.7.1",
6162
"rimraf": "^3.0.2",
6263
"rollup": "^2.26.5",
6364
"rollup-plugin-commonjs": "^10.1.0",
6465
"rollup-plugin-node-globals": "^1.4.0",
6566
"rollup-plugin-node-polyfills": "^0.2.1",
6667
"rollup-plugin-polyfill-node": "^0.7.0",
67-
"standard-version": "^9.3.0",
68-
"ts-node": "^9.0.0",
69-
"tsd": "^0.20.0",
70-
"typedoc": "^0.21.2",
71-
"typescript": "^4.0.2",
68+
"standard-version": "^9.5.0",
69+
"ts-node": "^10.8.1",
70+
"tsd": "^0.21.0",
71+
"typescript": "^4.7.4",
7272
"typescript-cached-transpile": "0.0.6",
7373
"uuid": "^8.3.2"
7474
},

src/code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class Code {
5252

5353
inspect(): string {
5454
const codeJson = this.toJSON();
55-
return `new Code("${codeJson.code}"${
55+
return `new Code("${codeJson.code.toString()}"${
5656
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
5757
})`;
5858
}

src/db_ref.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class DBRef {
115115
// NOTE: if OID is an ObjectId class it will just print the oid string.
116116
const oid =
117117
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
118+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
118119
return `new DBRef("${this.namespace}", new ObjectId("${oid}")${
119120
this.db ? `, "${this.db}"` : ''
120121
})`;

src/decimal128.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,8 @@ export class Decimal128 {
690690
// representation should be treated as 0/-0" spec cases in decimal128-1.json)
691691
if (significand_digits > 34) {
692692
string.push(`${0}`);
693-
if (exponent > 0) string.push('E+' + exponent);
694-
else if (exponent < 0) string.push('E' + exponent);
693+
if (exponent > 0) string.push(`E+${exponent}`);
694+
else if (exponent < 0) string.push(`E${exponent}`);
695695
return string.join('');
696696
}
697697

@@ -709,7 +709,7 @@ export class Decimal128 {
709709
// Exponent
710710
string.push('E');
711711
if (scientific_exponent > 0) {
712-
string.push('+' + scientific_exponent);
712+
string.push(`+${scientific_exponent}`);
713713
} else {
714714
string.push(`${scientific_exponent}`);
715715
}

src/extended_json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function isBSONType(value: unknown): value is BSONType {
4444
const BSON_INT32_MAX = 0x7fffffff;
4545
const BSON_INT32_MIN = -0x80000000;
4646
// INT64 boundaries
47+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
4748
const BSON_INT64_MAX = 0x7fffffffffffffff;
4849
const BSON_INT64_MIN = -0x8000000000000000;
4950

src/long.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,37 @@ import type { Timestamp } from './timestamp';
44

55
interface LongWASMHelpers {
66
/** Gets the high bits of the last operation performed */
7-
get_high(): number;
8-
div_u(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
9-
div_s(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
10-
rem_u(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
11-
rem_s(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
7+
get_high(this: void): number;
8+
div_u(
9+
this: void,
10+
lowBits: number,
11+
highBits: number,
12+
lowBitsDivisor: number,
13+
highBitsDivisor: number
14+
): number;
15+
div_s(
16+
this: void,
17+
lowBits: number,
18+
highBits: number,
19+
lowBitsDivisor: number,
20+
highBitsDivisor: number
21+
): number;
22+
rem_u(
23+
this: void,
24+
lowBits: number,
25+
highBits: number,
26+
lowBitsDivisor: number,
27+
highBitsDivisor: number
28+
): number;
29+
rem_s(
30+
this: void,
31+
lowBits: number,
32+
highBits: number,
33+
lowBitsDivisor: number,
34+
highBitsDivisor: number
35+
): number;
1236
mul(
37+
this: void,
1338
lowBits: number,
1439
highBits: number,
1540
lowBitsMultiplier: number,
@@ -471,6 +496,7 @@ export class Long {
471496
// into the result, and subtract it from the remainder. It is critical that
472497
// the approximate value is less than or equal to the real value so that the
473498
// remainder never becomes negative.
499+
// eslint-disable-next-line @typescript-eslint/no-this-alias
474500
rem = this;
475501
while (rem.gte(divisor)) {
476502
// Approximate the result of division. This may be a little greater or

src/objectid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class ObjectId {
3737
static cacheHexString: boolean;
3838

3939
/** ObjectId Bytes @internal */
40-
private [kId]: Buffer;
40+
private [kId]!: Buffer;
4141
/** ObjectId hexString cache @internal */
4242
private __id?: string;
4343

src/parser/calculate_size.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,16 @@ function calculateElement(
121121
);
122122
}
123123
} else if (value['_bsontype'] === 'Binary') {
124+
const binary: Binary = value;
124125
// Check what kind of subtype we have
125-
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
126+
if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
126127
return (
127128
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
128-
(value.position + 1 + 4 + 1 + 4)
129+
(binary.position + 1 + 4 + 1 + 4)
129130
);
130131
} else {
131132
return (
132-
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
133+
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1)
133134
);
134135
}
135136
} else if (value['_bsontype'] === 'Symbol') {

src/parser/deserializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ function deserializeObject(
697697
value = new DBRef(namespace, oid);
698698
} else {
699699
throw new BSONError(
700-
'Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"'
700+
`Detected unknown BSON type ${elementType.toString(16)} for fieldname "${name}"`
701701
);
702702
}
703703
if (name === '__proto__') {
@@ -742,9 +742,11 @@ function isolateEval(
742742
functionCache?: { [hash: string]: Function },
743743
object?: Document
744744
) {
745+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
745746
if (!functionCache) return new Function(functionString);
746747
// Check for cache hit, eval if missing and return cached function
747748
if (functionCache[functionString] == null) {
749+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
748750
functionCache[functionString] = new Function(functionString);
749751
}
750752

src/parser/serializer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ export function serializeInto(
763763
if (Array.isArray(object)) {
764764
// Get object keys
765765
for (let i = 0; i < object.length; i++) {
766-
const key = '' + i;
766+
const key = String(i);
767767
let value = object[i];
768768

769769
// Is there an override value
@@ -841,6 +841,7 @@ export function serializeInto(
841841
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
842842
index = serializeMinMax(buffer, key, value, index, true);
843843
} else if (typeof value['_bsontype'] !== 'undefined') {
844+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
844845
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
845846
}
846847
}
@@ -942,6 +943,7 @@ export function serializeInto(
942943
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
943944
index = serializeMinMax(buffer, key, value, index);
944945
} else if (typeof value['_bsontype'] !== 'undefined') {
946+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
945947
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
946948
}
947949
}
@@ -1047,6 +1049,7 @@ export function serializeInto(
10471049
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
10481050
index = serializeMinMax(buffer, key, value, index);
10491051
} else if (typeof value['_bsontype'] !== 'undefined') {
1052+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
10501053
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
10511054
}
10521055
}

src/parser/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ declare let window: any;
3333
declare let require: Function;
3434
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3535
declare let global: any;
36-
declare const self: unknown;
3736
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3837
declare let process: any; // Used by @rollup/plugin-replace
3938

src/utils/global.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function getGlobal<T = Record<string, unknown>>(): T {
1717
checkForMath(typeof window === 'object' && window) ||
1818
checkForMath(typeof self === 'object' && self) ||
1919
checkForMath(typeof global === 'object' && global) ||
20+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
2021
Function('return this')()
2122
);
2223
}

src/uuid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class UUID {
2525
static cacheHexString: boolean;
2626

2727
/** UUID Bytes @internal */
28-
private [kId]: Buffer;
28+
private [kId]!: Buffer;
2929
/** UUID hexString cache @internal */
3030
private __id?: string;
3131

test/binary_parser.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,27 +264,6 @@ BinaryParser.encode_int32 = function encode_int32(number, asArray) {
264264
return asArray ? [chr(a), chr(b), chr(c), chr(d)] : chr(a) + chr(b) + chr(c) + chr(d);
265265
};
266266

267-
BinaryParser.encode_int64 = function encode_int64(number) {
268-
var a, b, c, d, e, f, g, h, unsigned;
269-
unsigned = number < 0 ? number + 0x10000000000000000 : number;
270-
a = Math.floor(unsigned / 0xffffffffffffff);
271-
unsigned &= 0xffffffffffffff;
272-
b = Math.floor(unsigned / 0xffffffffffff);
273-
unsigned &= 0xffffffffffff;
274-
c = Math.floor(unsigned / 0xffffffffff);
275-
unsigned &= 0xffffffffff;
276-
d = Math.floor(unsigned / 0xffffffff);
277-
unsigned &= 0xffffffff;
278-
e = Math.floor(unsigned / 0xffffff);
279-
unsigned &= 0xffffff;
280-
f = Math.floor(unsigned / 0xffff);
281-
unsigned &= 0xffff;
282-
g = Math.floor(unsigned / 0xff);
283-
unsigned &= 0xff;
284-
h = Math.floor(unsigned);
285-
return chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h);
286-
};
287-
288267
/**
289268
* UTF8 methods
290269
*/

test/node/bson_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-loss-of-precision */
12
'use strict';
23

34
const { Buffer } = require('buffer');

test/node/extended_json_tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Extended JSON', function () {
7373
int32Number: 300,
7474
doubleNumber: 200.2,
7575
longNumberIntFit: 0x19000000000000,
76+
// eslint-disable-next-line no-loss-of-precision
7677
doubleNumberIntFit: 19007199250000000.12
7778
};
7879
});
@@ -93,6 +94,7 @@ describe('Extended JSON', function () {
9394
expect(300).to.equal(doc1.int32Number);
9495
expect(200.2).to.equal(doc1.doubleNumber);
9596
expect(0x19000000000000).to.equal(doc1.longNumberIntFit);
97+
// eslint-disable-next-line no-loss-of-precision
9698
expect(19007199250000000.12).to.equal(doc1.doubleNumberIntFit);
9799

98100
// Deserialize the document using strict mode

0 commit comments

Comments
 (0)