Skip to content

Commit 5dcd36d

Browse files
committed
chore: increase linting strictness
1 parent 9587764 commit 5dcd36d

32 files changed

+673
-386
lines changed

.eslintrc.json

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,6 @@
3232
"strict": ["error", "global"],
3333
"promise/no-native": "error",
3434

35-
// TBD
36-
"@typescript-eslint/ban-types": "off",
37-
"@typescript-eslint/explicit-module-boundary-types": "off",
38-
"@typescript-eslint/no-empty-function": "off",
39-
"@typescript-eslint/no-explicit-any": "off",
40-
"@typescript-eslint/no-this-alias": "off",
41-
"@typescript-eslint/no-non-null-assertion": "off",
42-
"@typescript-eslint/no-var-requires": "off",
43-
"no-var": "off",
44-
"prefer-const": "off",
45-
"prefer-spread": "off",
46-
"prefer-rest-params": "off"
47-
48-
// possible future options
49-
// "@typescript-eslint/ban-types": 2,
50-
// "@typescript-eslint/explicit-module-boundary-types": 2,
51-
// "@typescript-eslint/no-empty-function": 2,
52-
// "@typescript-eslint/no-explicit-any": 2,
53-
// "@typescript-eslint/no-this-alias": 2,
54-
// "@typescript-eslint/no-non-null-assertion": 2,
55-
// "@typescript-eslint/no-var-requires": 2,
56-
// "no-var": 2,
57-
// "prefer-const": 2
58-
},
59-
"settings": {
60-
"jsdoc": {
61-
"check-types": false,
62-
"mode": "typescript",
63-
"tagNamePreference": {
64-
"augments": "extends"
65-
}
66-
}
35+
"@typescript-eslint/no-explicit-any": "off"
6736
}
6837
}

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"build:docs": "npm run build:dts && typedoc",
9393
"check:bench": "node test/benchmarks/driverBench",
9494
"check:coverage": "nyc npm run check:test",
95-
"check:lint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
95+
"check:lint": "tsc -v && tsc --noEmit && eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
9696
"check:test": "mocha --recursive test/functional test/unit",
9797
"check:ts": "tsc --noEmit",
9898
"check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js",

src/apm.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type { MongoClient } from './mongo_client';
77

88
/** @public */
99
export class Instrumentation extends EventEmitter {
10-
$MongoClient: any;
11-
$prototypeConnect: any;
10+
$MongoClient?: typeof MongoClient;
11+
$prototypeConnect?: typeof MongoClient['connect'];
1212

1313
/** @event */
1414
static readonly STARTED = 'started' as const;
@@ -21,13 +21,14 @@ export class Instrumentation extends EventEmitter {
2121
super();
2222
}
2323

24-
instrument(MongoClient: any, callback: Callback) {
24+
instrument(mongoClientClass: typeof MongoClient, callback?: Callback): void {
2525
// store a reference to the original functions
26-
this.$MongoClient = MongoClient;
27-
const $prototypeConnect = (this.$prototypeConnect = MongoClient.prototype.connect);
26+
this.$MongoClient = mongoClientClass;
27+
const $prototypeConnect = (this.$prototypeConnect = mongoClientClass.prototype.connect);
2828

29+
// eslint-disable-next-line @typescript-eslint/no-this-alias
2930
const instrumentation = this;
30-
MongoClient.prototype.connect = function (this: MongoClient, callback: Callback) {
31+
mongoClientClass.prototype.connect = function (this: MongoClient, callback: Callback) {
3132
// override monitorCommands to be switched on
3233
this.s.options = { ...(this.s.options ?? {}), monitorCommands: true };
3334

@@ -42,12 +43,14 @@ export class Instrumentation extends EventEmitter {
4243
);
4344

4445
return $prototypeConnect.call(this, callback);
45-
};
46+
} as MongoClient['connect'];
4647

4748
if (typeof callback === 'function') callback(undefined, this);
4849
}
4950

50-
uninstrument() {
51-
this.$MongoClient.prototype.connect = this.$prototypeConnect;
51+
uninstrument(): void {
52+
if (this.$MongoClient) {
53+
this.$MongoClient.prototype.connect = this.$prototypeConnect as any;
54+
}
5255
}
5356
}

src/bson.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525

2626
/** @public */
2727
export interface Document {
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2829
[key: string]: any;
2930
}
3031

src/change_stream.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export class ChangeStream extends EventEmitter {
209209
this.topology = parent.s.topology;
210210
} else if (parent instanceof MongoClient) {
211211
this.type = CHANGE_DOMAIN_TYPES.CLUSTER;
212+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
212213
this.topology = parent.topology!;
213214
} else {
214215
throw new TypeError(
@@ -406,16 +407,16 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
406407
}
407408
}
408409

409-
set resumeToken(token) {
410+
set resumeToken(token: ResumeToken) {
410411
this._resumeToken = token;
411412
this.emit(ChangeStream.RESUME_TOKEN_CHANGED, token);
412413
}
413414

414-
get resumeToken() {
415+
get resumeToken(): ResumeToken {
415416
return this._resumeToken;
416417
}
417418

418-
get resumeOptions() {
419+
get resumeOptions(): Document {
419420
const result: Document = {};
420421
for (const optionName of CURSOR_OPTIONS) {
421422
if (Reflect.has(this.options, optionName)) {
@@ -438,7 +439,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
438439
return result;
439440
}
440441

441-
cacheResumeToken(resumeToken: ResumeToken) {
442+
cacheResumeToken(resumeToken: ResumeToken): void {
442443
if (this.bufferedCount() === 0 && this.cursorState.postBatchResumeToken) {
443444
this.resumeToken = this.cursorState.postBatchResumeToken;
444445
} else {
@@ -447,7 +448,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
447448
this.hasReceived = true;
448449
}
449450

450-
_processBatch(batchName: string, response?: Document) {
451+
_processBatch(batchName: string, response?: Document): void {
451452
const cursor = response?.cursor || {};
452453
if (cursor.postBatchResumeToken) {
453454
this.cursorState.postBatchResumeToken = cursor.postBatchResumeToken;
@@ -458,7 +459,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
458459
}
459460
}
460461

461-
_initializeCursor(callback: Callback) {
462+
_initializeCursor(callback: Callback): void {
462463
super._initializeCursor((err, response) => {
463464
if (err || response == null) {
464465
callback(err, response);
@@ -482,7 +483,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
482483
});
483484
}
484485

485-
_getMore(callback: Callback) {
486+
_getMore(callback: Callback): void {
486487
super._getMore((err, response) => {
487488
if (err) {
488489
callback(err);
@@ -644,7 +645,7 @@ function processError(changeStream: ChangeStream, error?: AnyError, callback?: C
644645
changeStream.closed = true;
645646
}
646647

647-
if (cursor && isResumableError(error, maxWireVersion(cursor.server))) {
648+
if (cursor && isResumableError(error as MongoError, maxWireVersion(cursor.server))) {
648649
changeStream.cursor = undefined;
649650

650651
// stop listening to all events from old cursor

src/collection.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
EstimatedDocumentCountOperation,
4646
EstimatedDocumentCountOptions
4747
} from './operations/estimated_document_count';
48-
import { FindOperation, FindOptions } from './operations/find';
48+
import { FindOperation, FindOptions, Sort } from './operations/find';
4949
import { FindOneOperation } from './operations/find_one';
5050
import {
5151
FindAndModifyOperation,
@@ -126,7 +126,7 @@ export interface CollectionOptions
126126

127127
/** @internal */
128128
export interface CollectionPrivate {
129-
pkFactory: PkFactory | typeof ObjectId;
129+
pkFactory: PkFactory;
130130
db: Db;
131131
topology: Topology;
132132
options: any;
@@ -186,7 +186,9 @@ export class Collection implements OperationParent {
186186
options,
187187
topology: db.s.topology,
188188
namespace: new MongoDBNamespace(db.databaseName, name),
189-
pkFactory: db.options?.pkFactory ? db.options.pkFactory : ObjectId,
189+
pkFactory: db.options?.pkFactory
190+
? db.options.pkFactory
191+
: ((ObjectId as unknown) as PkFactory), // TODO: remove when bson is typed
190192
readPreference: ReadPreference.fromOptions(options),
191193
readConcern: ReadConcern.fromOptions(options),
192194
writeConcern: WriteConcern.fromOptions(options),
@@ -213,23 +215,21 @@ export class Collection implements OperationParent {
213215

214216
/**
215217
* The name of the database this collection belongs to
216-
* @readonly
217218
*/
218219
get dbName(): string {
219220
return this.s.namespace.db;
220221
}
221222

222223
/**
223224
* The name of this collection
224-
* @readonly
225225
*/
226226
get collectionName(): string {
227+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
227228
return this.s.namespace.collection!;
228229
}
229230

230231
/**
231232
* The namespace of this collection, in the format `${this.dbName}.${this.collectionName}`
232-
* @readonly
233233
*/
234234
get namespace(): string {
235235
return this.s.namespace.toString();
@@ -238,7 +238,6 @@ export class Collection implements OperationParent {
238238
/**
239239
* The current readConcern of the collection. If not explicitly defined for
240240
* this collection, will be inherited from the parent DB
241-
* @readonly
242241
*/
243242
get readConcern(): ReadConcern | undefined {
244243
if (this.s.readConcern == null) {
@@ -250,7 +249,6 @@ export class Collection implements OperationParent {
250249
/**
251250
* The current readPreference of the collection. If not explicitly defined for
252251
* this collection, will be inherited from the parent DB
253-
* @readonly
254252
*/
255253
get readPreference(): ReadPreference | undefined {
256254
if (this.s.readPreference == null) {
@@ -263,7 +261,6 @@ export class Collection implements OperationParent {
263261
/**
264262
* The current writeConcern of the collection. If not explicitly defined for
265263
* this collection, will be inherited from the parent DB
266-
* @readonly
267264
*/
268265
get writeConcern(): WriteConcern | undefined {
269266
if (this.s.writeConcern == null) {
@@ -1445,7 +1442,7 @@ export class Collection implements OperationParent {
14451442
callback?: Callback<Document | Document[]>
14461443
): Promise<Document | Document[]> | void {
14471444
if ('function' === typeof options) (callback = options), (options = {});
1448-
// Out must allways be defined (make sure we don't break weirdly on pre 1.8+ servers)
1445+
// Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
14491446
if (options?.out == null) {
14501447
throw new Error(
14511448
'the out option parameter must be defined, see mongodb docs for possible values'
@@ -1652,16 +1649,30 @@ export class Collection implements OperationParent {
16521649
* @param options - Optional settings for the command
16531650
* @param callback - An optional callback, a Promise will be returned if none is provided
16541651
*/
1652+
findAndRemove(query: Document, callback: Callback): void;
1653+
findAndRemove(query: Document, sort: Sort, callback: Callback): void;
1654+
findAndRemove(query: Document, options: FindAndModifyOptions, callback: Callback): void;
16551655
findAndRemove(
16561656
query: Document,
1657-
sort: Document,
1657+
sort: Sort,
16581658
options: FindAndModifyOptions,
16591659
callback: Callback
1660-
): Promise<Document> | void {
1661-
const args = Array.prototype.slice.call(arguments, 1);
1662-
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
1663-
sort = args.length ? args.shift() || [] : [];
1664-
options = args.length ? args.shift() || {} : {};
1660+
): void;
1661+
findAndRemove(query: Document): Promise<Document>;
1662+
findAndRemove(query: Document, sort: Sort): Promise<Document>;
1663+
findAndRemove(query: Document, sort: Sort, options: FindAndModifyOptions): Promise<Document>;
1664+
findAndRemove(
1665+
query: Document,
1666+
...args: any[]
1667+
): // TODO: Use labeled tuples when api-extractor supports TS 4.0
1668+
/*sort?: Sort | FindAndModifyOptions | Callback<Document>, */
1669+
/*options?: FindAndModifyOptions | Callback<Document>, */
1670+
/*callback?: Callback<Document>*/
1671+
Promise<Document> | void {
1672+
const callback =
1673+
typeof args[args.length - 1] === 'function' ? (args.pop() as Callback) : undefined;
1674+
const sort = (args.length ? args.shift() || [] : []) as Sort;
1675+
const options = (args.length ? args.shift() || {} : {}) as FindAndModifyOptions;
16651676

16661677
// Add the remove option
16671678
options.remove = true;
@@ -1687,26 +1698,23 @@ export class Collection implements OperationParent {
16871698
* @param callback - An optional callback, a Promise will be returned if none is provided
16881699
*/
16891700
group(
1701+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
16901702
keys: any,
1691-
condition: any,
1692-
initial: any,
1693-
reduce: any,
1694-
finalize: any,
1695-
command: any,
1696-
options: any,
1697-
callback: Callback
1698-
) {
1699-
const args = Array.prototype.slice.call(arguments, 3);
1700-
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
1701-
reduce = args.length ? args.shift() : null;
1702-
finalize = args.length ? args.shift() : null;
1703-
command = args.length ? args.shift() : null;
1704-
options = args.length ? args.shift() || {} : {};
1703+
condition: Document,
1704+
initial: Document,
1705+
// TODO: Use labeled tuples when api-extractor supports TS 4.0
1706+
...args: [/*reduce?:*/ any, /*finalize?:*/ any, /*command?:*/ any, /*callback?:*/ Callback]
1707+
): Promise<Document> | void {
1708+
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
1709+
let reduce = args.length ? args.shift() : undefined;
1710+
let finalize = args.length ? args.shift() : undefined;
1711+
let command = args.length ? args.shift() : undefined;
1712+
const options = args.length ? args.shift() || {} : {};
17051713

17061714
// Make sure we are backward compatible
17071715
if (!(typeof finalize === 'function')) {
17081716
command = finalize;
1709-
finalize = null;
1717+
finalize = undefined;
17101718
}
17111719

17121720
if (
@@ -1751,7 +1759,7 @@ export class Collection implements OperationParent {
17511759
*
17521760
* @param query - Query object to locate the object to modify.
17531761
* @param sort - If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
1754-
* @param doc - The fields/vals to be updated.
1762+
* @param doc - The fields/values to be updated.
17551763
* @param options - Optional settings for the command
17561764
* @param callback - An optional callback, a Promise will be returned if none is provided
17571765
*/

0 commit comments

Comments
 (0)