Skip to content

NODE-2692: Increase linting strictness #2534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,6 @@
"strict": ["error", "global"],
"promise/no-native": "error",

// TBD
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-var-requires": "off",
"no-var": "off",
"prefer-const": "off",
"prefer-spread": "off",
"prefer-rest-params": "off"

// possible future options
// "@typescript-eslint/ban-types": 2,
// "@typescript-eslint/explicit-module-boundary-types": 2,
// "@typescript-eslint/no-empty-function": 2,
// "@typescript-eslint/no-explicit-any": 2,
// "@typescript-eslint/no-this-alias": 2,
// "@typescript-eslint/no-non-null-assertion": 2,
// "@typescript-eslint/no-var-requires": 2,
// "no-var": 2,
// "prefer-const": 2
},
"settings": {
"jsdoc": {
"check-types": false,
"mode": "typescript",
"tagNamePreference": {
"augments": "extends"
}
}
"@typescript-eslint/no-explicit-any": "off"
}
}
454 changes: 350 additions & 104 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
"build:docs": "npm run build:dts && typedoc",
"check:bench": "node test/benchmarks/driverBench",
"check:coverage": "nyc npm run check:test",
"check:lint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
"check:lint": "npm run check:ts && eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
"check:test": "mocha --recursive test/functional test/unit",
"check:ts": "tsc --noEmit",
"check:ts": "tsc -v && tsc --noEmit",
"check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js",
"check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js",
"check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js",
Expand Down
21 changes: 12 additions & 9 deletions src/apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { MongoClient } from './mongo_client';

/** @public */
export class Instrumentation extends EventEmitter {
$MongoClient: any;
$prototypeConnect: any;
$MongoClient?: typeof MongoClient;
$prototypeConnect?: typeof MongoClient.prototype['connect'];

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

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

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

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

return $prototypeConnect.call(this, callback);
};
} as MongoClient['connect'];

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

uninstrument() {
this.$MongoClient.prototype.connect = this.$prototypeConnect;
uninstrument(): void {
if (this.$MongoClient && this.$prototypeConnect) {
this.$MongoClient.prototype.connect = this.$prototypeConnect;
}
}
}
1 change: 1 addition & 0 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {

/** @public */
export interface Document {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

Expand Down
36 changes: 24 additions & 12 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { Topology } from './sdam/topology';
import type { Writable } from 'stream';
import type { StreamOptions } from './cursor/core_cursor';
import type { OperationParent } from './operations/command';
import type { CollationOptions } from './cmap/wire_protocol/write_command';
const kResumeQueue = Symbol('resumeQueue');

const CHANGE_STREAM_OPTIONS = ['resumeAfter', 'startAfter', 'startAtOperationTime', 'fullDocument'];
Expand All @@ -33,6 +34,14 @@ const CHANGE_DOMAIN_TYPES = {
CLUSTER: Symbol('Cluster')
};

export interface ResumeOptions {
startAtOperationTime?: Timestamp;
batchSize?: number;
maxAwaitTimeMS?: number;
collation?: CollationOptions;
readPreference?: ReadPreference;
}

/**
* Represents the logical starting point for a new or resuming {@link https://docs.mongodb.com/master/changeStreams/#change-stream-resume-token| Change Stream} on the server.
* @public
Expand Down Expand Up @@ -209,6 +218,7 @@ export class ChangeStream extends EventEmitter {
this.topology = parent.s.topology;
} else if (parent instanceof MongoClient) {
this.type = CHANGE_DOMAIN_TYPES.CLUSTER;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.topology = parent.topology!;
} else {
throw new TypeError(
Expand Down Expand Up @@ -406,30 +416,32 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
}
}

set resumeToken(token) {
set resumeToken(token: ResumeToken) {
this._resumeToken = token;
this.emit(ChangeStream.RESUME_TOKEN_CHANGED, token);
}

get resumeToken() {
get resumeToken(): ResumeToken {
return this._resumeToken;
}

get resumeOptions() {
const result: Document = {};
get resumeOptions(): ResumeOptions {
const result = {} as ResumeOptions;
for (const optionName of CURSOR_OPTIONS) {
if (Reflect.has(this.options, optionName)) {
result[optionName] = Reflect.get(this.options, optionName);
Reflect.set(result, optionName, Reflect.get(this.options, optionName));
}
}

if (this.resumeToken || this.startAtOperationTime) {
['resumeAfter', 'startAfter', 'startAtOperationTime'].forEach(key => delete result[key]);
['resumeAfter', 'startAfter', 'startAtOperationTime'].forEach(key =>
Reflect.deleteProperty(result, key)
);

if (this.resumeToken) {
const resumeKey =
this.options.startAfter && !this.hasReceived ? 'startAfter' : 'resumeAfter';
result[resumeKey] = this.resumeToken;
Reflect.set(result, resumeKey, this.resumeToken);
} else if (this.startAtOperationTime && maxWireVersion(this.server) >= 7) {
result.startAtOperationTime = this.startAtOperationTime;
}
Expand All @@ -438,7 +450,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
return result;
}

cacheResumeToken(resumeToken: ResumeToken) {
cacheResumeToken(resumeToken: ResumeToken): void {
if (this.bufferedCount() === 0 && this.cursorState.postBatchResumeToken) {
this.resumeToken = this.cursorState.postBatchResumeToken;
} else {
Expand All @@ -447,7 +459,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
this.hasReceived = true;
}

_processBatch(batchName: string, response?: Document) {
_processBatch(batchName: string, response?: Document): void {
const cursor = response?.cursor || {};
if (cursor.postBatchResumeToken) {
this.cursorState.postBatchResumeToken = cursor.postBatchResumeToken;
Expand All @@ -458,7 +470,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
}
}

_initializeCursor(callback: Callback) {
_initializeCursor(callback: Callback): void {
super._initializeCursor((err, response) => {
if (err || response == null) {
callback(err, response);
Expand All @@ -482,7 +494,7 @@ export class ChangeStreamCursor extends Cursor<AggregateOperation, ChangeStreamC
});
}

_getMore(callback: Callback) {
_getMore(callback: Callback): void {
super._getMore((err, response) => {
if (err) {
callback(err);
Expand Down Expand Up @@ -644,7 +656,7 @@ function processError(changeStream: ChangeStream, error?: AnyError, callback?: C
changeStream.closed = true;
}

if (cursor && isResumableError(error, maxWireVersion(cursor.server))) {
if (cursor && isResumableError(error as MongoError, maxWireVersion(cursor.server))) {
changeStream.cursor = undefined;

// stop listening to all events from old cursor
Expand Down
Loading