Skip to content

Commit cb02359

Browse files
committed
chore(NODE-4321): add typescript to eslint rules
1 parent fe7c102 commit cb02359

15 files changed

+117
-81
lines changed

.eslintrc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@
181181
"typescript-eslint/ban-ts-comment": "off"
182182
}
183183
},
184+
{
185+
// Settings for typescript test files
186+
"files": [
187+
"src/**/*.ts"
188+
],
189+
"parser": "@typescript-eslint/parser",
190+
"parserOptions": {
191+
"project": ["./tsconfig.json"]
192+
},
193+
"extends": [
194+
"plugin:@typescript-eslint/recommended-requiring-type-checking"
195+
],
196+
"rules": {
197+
"@typescript-eslint/no-unsafe-member-access": "off",
198+
"@typescript-eslint/no-unsafe-argument": "off",
199+
"@typescript-eslint/no-unsafe-assignment": "off",
200+
"@typescript-eslint/no-unsafe-return": "off",
201+
"@typescript-eslint/no-unsafe-call": "off",
202+
203+
"@typescript-eslint/restrict-plus-operands": "off",
204+
"@typescript-eslint/restrict-template-expressions": "off"
205+
}
206+
},
184207
{
185208
// Settings for typescript type test files
186209
"files": [

src/change_stream.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,10 @@ export class ChangeStream<
894894

895895
if (isResumableError(changeStreamError, this.cursor.maxWireVersion)) {
896896
this._endStream();
897-
this.cursor.close();
897+
this.cursor.close().then(
898+
() => null,
899+
() => null
900+
); // TODO(NODE-XXX): Is this correct?
898901

899902
const topology = getTopology(this.parent);
900903
topology.selectServer(this.cursor.readPreference, {}, serverSelectionError => {
@@ -913,6 +916,7 @@ export class ChangeStream<
913916
*
914917
* we promisify _processErrorIteratorModeCallback until we have a promisifed version of selectServer.
915918
*/
919+
// eslint-disable-next-line @typescript-eslint/unbound-method
916920
private _processErrorIteratorMode = promisify(this._processErrorIteratorModeCallback);
917921

918922
/** @internal */
@@ -923,7 +927,10 @@ export class ChangeStream<
923927
}
924928

925929
if (isResumableError(changeStreamError, this.cursor.maxWireVersion)) {
926-
this.cursor.close();
930+
this.cursor.close().then(
931+
() => null,
932+
() => null
933+
); // TODO(NODE-XXX): Is this correct?;
927934

928935
const topology = getTopology(this.parent);
929936
topology.selectServer(this.cursor.readPreference, {}, serverSelectionError => {

src/cmap/command_monitoring_events.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ function extractCommand(command: WriteProtocolMessageType): Document {
248248
});
249249

250250
OP_QUERY_KEYS.forEach(key => {
251-
const opKey = key as typeof OP_QUERY_KEYS[number];
252-
if (command[opKey]) {
253-
result[opKey] = command[opKey];
251+
if (command[key]) {
252+
result[key] = command[key];
254253
}
255254
});
256255

src/cmap/connect.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { Document } from '../bson';
88
import { Int32 } from '../bson';
99
import { LEGACY_HELLO_COMMAND } from '../constants';
1010
import {
11-
AnyError,
1211
MongoCompatibilityError,
1312
MongoError,
1413
MongoErrorLabel,
@@ -462,39 +461,39 @@ function makeSocks5Connection(options: MakeConnectionOptions, callback: Callback
462461
}
463462

464463
// Then, establish the Socks5 proxy connection:
465-
SocksClient.createConnection(
466-
{
467-
existing_socket: rawSocket,
468-
timeout: options.connectTimeoutMS,
469-
command: 'connect',
470-
destination: {
471-
host: destination.host,
472-
port: destination.port
473-
},
474-
proxy: {
475-
// host and port are ignored because we pass existing_socket
476-
host: 'iLoveJavaScript',
477-
port: 0,
478-
type: 5,
479-
userId: options.proxyUsername || undefined,
480-
password: options.proxyPassword || undefined
481-
}
464+
SocksClient.createConnection({
465+
existing_socket: rawSocket,
466+
timeout: options.connectTimeoutMS,
467+
command: 'connect',
468+
destination: {
469+
host: destination.host,
470+
port: destination.port
482471
},
483-
(err: AnyError, info: { socket: Stream }) => {
484-
if (err) {
485-
return callback(connectionFailureError('error', err));
486-
}
487-
472+
proxy: {
473+
// host and port are ignored because we pass existing_socket
474+
host: 'iLoveJavaScript',
475+
port: 0,
476+
type: 5,
477+
userId: options.proxyUsername || undefined,
478+
password: options.proxyPassword || undefined
479+
}
480+
}).then(
481+
({ socket }) => {
488482
// Finally, now treat the resulting duplex stream as the
489483
// socket over which we send and receive wire protocol messages:
490484
makeConnection(
491485
{
492486
...options,
493-
existingSocket: info.socket,
487+
existingSocket: socket,
494488
proxyHost: undefined
495489
},
496490
callback
497491
);
492+
},
493+
error => {
494+
if (error) {
495+
return callback(connectionFailureError('error', error));
496+
}
498497
}
499498
);
500499
}

src/cmap/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
533533
clusterTime = session.clusterTime;
534534
}
535535

536-
const err = applySession(session, finalCmd, options as CommandOptions);
536+
const err = applySession(session, finalCmd, options);
537537
if (err) {
538538
return callback(err);
539539
}

src/cmap/wire_protocol/compression.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ export function compress(
5252
if (Snappy[PKG_VERSION].major <= 6) {
5353
Snappy.compress(dataToBeCompressed, callback);
5454
} else {
55-
Snappy.compress(dataToBeCompressed)
56-
.then(buffer => callback(undefined, buffer))
57-
.catch(error => callback(error));
55+
Snappy.compress(dataToBeCompressed).then(
56+
buffer => callback(undefined, buffer),
57+
error => callback(error)
58+
);
5859
}
5960
break;
6061
}
@@ -102,9 +103,10 @@ export function decompress(
102103
if (Snappy[PKG_VERSION].major <= 6) {
103104
Snappy.uncompress(compressedData, { asBuffer: true }, callback);
104105
} else {
105-
Snappy.uncompress(compressedData, { asBuffer: true })
106-
.then(buffer => callback(undefined, buffer))
107-
.catch(error => callback(error));
106+
Snappy.uncompress(compressedData, { asBuffer: true }).then(
107+
buffer => callback(undefined, buffer),
108+
error => callback(error)
109+
);
108110
}
109111
break;
110112
}

src/collection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ export class Collection<TSchema extends Document = Document> {
725725
}
726726

727727
if (typeof filter === 'function') {
728-
callback = filter as Callback<WithId<TSchema> | null>;
728+
callback = filter;
729729
filter = {};
730730
options = {};
731731
}
@@ -1128,7 +1128,7 @@ export class Collection<TSchema extends Document = Document> {
11281128
this.s.db.s.client,
11291129
new CountDocumentsOperation(
11301130
this as TODO_NODE_3286,
1131-
filter as Document,
1131+
filter,
11321132
resolveOptions(this, options as CountDocumentsOptions)
11331133
),
11341134
callback
@@ -1191,7 +1191,7 @@ export class Collection<TSchema extends Document = Document> {
11911191
callback?: Callback<any[]>
11921192
): Promise<any[]> | void {
11931193
if (typeof filter === 'function') {
1194-
(callback = filter as Callback<any[]>), (filter = {}), (options = {});
1194+
(callback = filter), (filter = {}), (options = {});
11951195
} else {
11961196
if (arguments.length === 3 && typeof options === 'function') {
11971197
(callback = options), (options = {});
@@ -1667,7 +1667,7 @@ export class Collection<TSchema extends Document = Document> {
16671667
callback?: Callback<number>
16681668
): Promise<number> | void {
16691669
if (typeof filter === 'function') {
1670-
(callback = filter as Callback<number>), (filter = {}), (options = {});
1670+
(callback = filter), (filter = {}), (options = {});
16711671
} else {
16721672
if (typeof options === 'function') (callback = options), (options = {});
16731673
}

src/cursor/abstract_cursor.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface CursorCloseOptions {
6767
/** @public */
6868
export interface CursorStreamOptions {
6969
/** A transformation method applied to each document emitted by the stream */
70-
transform?(doc: Document): Document;
70+
transform?(this: void, doc: Document): Document;
7171
}
7272

7373
/** @public */
@@ -883,7 +883,10 @@ class ReadableCursorStream extends Readable {
883883
// a client during iteration. Alternatively, we could do the "right" thing and
884884
// propagate the error message by removing this special case.
885885
if (err.message.match(/server is closed/)) {
886-
this._cursor.close();
886+
this._cursor.close().then(
887+
() => null,
888+
() => null
889+
); // TODO(NODE-XXXX): is this correct?
887890
return this.push(null);
888891
}
889892

@@ -902,7 +905,10 @@ class ReadableCursorStream extends Readable {
902905
if (result == null) {
903906
this.push(null);
904907
} else if (this.destroyed) {
905-
this._cursor.close();
908+
this._cursor.close().then(
909+
() => null,
910+
() => null
911+
); // TODO(NODE-XXXX): is this correct?
906912
} else {
907913
if (this.push(result)) {
908914
return this._readNext();

src/deps.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ try {
3535
} catch {} // eslint-disable-line
3636

3737
export interface KerberosClient {
38-
step: (challenge: string, callback?: Callback<string>) => Promise<string> | void;
39-
wrap: (
40-
challenge: string,
41-
options?: { user: string },
42-
callback?: Callback<string>
43-
) => Promise<string> | void;
44-
unwrap: (challenge: string, callback?: Callback<string>) => Promise<string> | void;
38+
step(challenge: string): Promise<string>;
39+
step(challenge: string, callback: Callback<string>): void;
40+
wrap(challenge: string, options: { user: string }): Promise<string>;
41+
wrap(challenge: string, options: { user: string }, callback: Callback<string>): void;
42+
unwrap(challenge: string): Promise<string>;
43+
unwrap(challenge: string, callback: Callback<string>): void;
4544
}
4645

4746
type ZStandardLib = {
@@ -80,11 +79,7 @@ type SnappyLib = {
8079
* @param callback - ONLY USED IN SNAPPY 6.x
8180
*/
8281
compress(buf: Buffer): Promise<Buffer>;
83-
compress(buf: Buffer, callback: (error?: Error, buffer?: Buffer) => void): Promise<Buffer> | void;
84-
compress(
85-
buf: Buffer,
86-
callback?: (error?: Error, buffer?: Buffer) => void
87-
): Promise<Buffer> | void;
82+
compress(buf: Buffer, callback: (error?: Error, buffer?: Buffer) => void): void;
8883

8984
/**
9085
* - Snappy 6.x takes a callback and returns void
@@ -99,12 +94,7 @@ type SnappyLib = {
9994
buf: Buffer,
10095
opt: { asBuffer: true },
10196
callback: (error?: Error, buffer?: Buffer) => void
102-
): Promise<Buffer> | void;
103-
uncompress(
104-
buf: Buffer,
105-
opt: { asBuffer: true },
106-
callback?: (error?: Error, buffer?: Buffer) => void
107-
): Promise<Buffer> | void;
97+
): void;
10898
};
10999

110100
export let Snappy: SnappyLib | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
@@ -141,6 +131,7 @@ interface AWS4 {
141131
* @param credentials - AWS credential details, sessionToken should be omitted entirely if its false-y
142132
*/
143133
sign(
134+
this: void,
144135
options: {
145136
path: '/';
146137
body: string;

src/mongo_client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,12 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
638638
.then(() => {
639639
// Do not return the result of callback
640640
})
641-
.finally(() => session.endSession());
641+
.finally(() => {
642+
session.endSession().then(
643+
() => null,
644+
() => null
645+
);
646+
});
642647
}
643648

644649
/**

src/operations/common_functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function indexInformation(
3434
let options = _optionsOrCallback as IndexInformationOptions;
3535
let callback = _callback as Callback;
3636
if ('function' === typeof _optionsOrCallback) {
37-
callback = _optionsOrCallback as Callback;
37+
callback = _optionsOrCallback;
3838
options = {};
3939
}
4040
// If we specified full information

src/operations/execute_operation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ export function executeOperation<
144144
});
145145
} catch (error) {
146146
if (session?.owner != null && session.owner === owner) {
147-
session.endSession();
147+
session.endSession().then(
148+
() => null,
149+
() => null
150+
);
148151
}
152+
149153
throw error;
150154
}
151155
});

src/read_preference.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,10 @@ export class ReadPreference {
163163
} else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') {
164164
const mode = readPreference.mode || readPreference.preference;
165165
if (mode && typeof mode === 'string') {
166-
return new ReadPreference(
167-
mode as ReadPreferenceMode,
168-
readPreference.tags ?? readPreferenceTags,
169-
{
170-
maxStalenessSeconds: readPreference.maxStalenessSeconds,
171-
hedge: options.hedge
172-
}
173-
);
166+
return new ReadPreference(mode, readPreference.tags ?? readPreferenceTags, {
167+
maxStalenessSeconds: readPreference.maxStalenessSeconds,
168+
hedge: options.hedge
169+
});
174170
}
175171
}
176172

@@ -193,7 +189,7 @@ export class ReadPreference {
193189
} else if (r && !(r instanceof ReadPreference) && typeof r === 'object') {
194190
const mode = r.mode || r.preference;
195191
if (mode && typeof mode === 'string') {
196-
options.readPreference = new ReadPreference(mode as ReadPreferenceMode, r.tags, {
192+
options.readPreference = new ReadPreference(mode, r.tags, {
197193
maxStalenessSeconds: r.maxStalenessSeconds
198194
});
199195
}

src/sessions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,10 @@ function attemptTransaction<TSchema>(
616616
}
617617

618618
if (!isPromiseLike(promise)) {
619-
session.abortTransaction();
619+
session.abortTransaction().then(
620+
() => null,
621+
() => null
622+
); // TODO(NODE-XXXX): is this correct?
620623
throw new MongoInvalidArgumentError(
621624
'Function provided to `withTransaction` must return a Promise'
622625
);

0 commit comments

Comments
 (0)