Skip to content

Commit

Permalink
refactor: add no-floating-promises rule (#338)
Browse files Browse the repository at this point in the history
* refactor: add no-floating-promises rule and fix linting errors that appeared

* refactor: use void for storage

* refactor: wait for activation in tests

* refactor: await deactivating

* refactor: remove void

* refactor: remove one more void
  • Loading branch information
alenakhineika authored Aug 12, 2021
1 parent 8d53bc0 commit e63a769
Show file tree
Hide file tree
Showing 36 changed files with 199 additions and 196 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
'restrict-template-expressions': 0,
'@typescript-eslint/restrict-template-expressions': 0,

'@typescript-eslint/no-floating-promises': 2,

// VV These rules we'd like to turn off one day so they error.
'@typescript-eslint/no-unsafe-assignment': 0,
'@typescript-eslint/no-unsafe-member-access': 0,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/launchMongoShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const openMongoDBShell = (connectionController: ConnectionController): Promise<b
if (
!connectionController.isCurrentlyConnected()
) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'You need to be connected before launching the MongoDB Shell.'
);

Expand All @@ -106,15 +106,15 @@ const openMongoDBShell = (connectionController: ConnectionController): Promise<b
const shellCommand: string | undefined = vscode.workspace.getConfiguration('mdb').get('shell');

if (!userShell) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'Error: No shell found, please set your default shell environment in vscode.'
);

return Promise.resolve(false);
}

if (!shellCommand) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'No MongoDB shell command found. Please set the shell command in the MongoDB extension settings.'
);
return Promise.resolve(false);
Expand Down
24 changes: 12 additions & 12 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class ConnectionController {
try {
return this.addNewConnectionStringAndConnect(connectionString);
} catch (error) {
vscode.window.showErrorMessage(error.message);
void vscode.window.showErrorMessage(error.message);

return false;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ export default class ConnectionController {
connectionModel: ConnectionModel,
connectionType: ConnectionTypes
): void {
this._telemetryService.trackNewConnection(
void this._telemetryService.trackNewConnection(
newDataService.client.client,
connectionModel,
connectionType
Expand Down Expand Up @@ -323,7 +323,7 @@ export default class ConnectionController {
connectionId,
connectionInfoAsString
);
this._storageController.storeNewConnection(newLoadedConnection);
void this._storageController.storeNewConnection(newLoadedConnection);
}

return this.connect(connectionId, connectionModel, connectionType);
Expand Down Expand Up @@ -412,7 +412,7 @@ export default class ConnectionController {
}

log.info('Successfully connected');
vscode.window.showInformationMessage('MongoDB connection successful.');
void vscode.window.showInformationMessage('MongoDB connection successful.');

this._activeDataService = newDataService;
this._activeConnectionModel = connectionModel;
Expand Down Expand Up @@ -449,7 +449,7 @@ export default class ConnectionController {
: savedConnectionModel
);
} catch (error) {
vscode.window.showErrorMessage(`Unable to load connection: ${error}`);
void vscode.window.showErrorMessage(`Unable to load connection: ${error}`);

return false;
}
Expand All @@ -463,7 +463,7 @@ export default class ConnectionController {

return connectResult.successfullyConnected;
} catch (error) {
vscode.window.showErrorMessage(error.message);
void vscode.window.showErrorMessage(error.message);

return false;
}
Expand All @@ -476,7 +476,7 @@ export default class ConnectionController {
);

if (!this._activeDataService) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'Unable to disconnect: no active connection.'
);

Expand All @@ -502,10 +502,10 @@ export default class ConnectionController {

try {
await _disconnect();
vscode.window.showInformationMessage('MongoDB disconnected.');
void vscode.window.showInformationMessage('MongoDB disconnected.');
} catch (error) {
// Show an error, however we still reset the active connection to free up the extension.
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'An error occured while disconnecting from the current connection.'
);
}
Expand Down Expand Up @@ -533,7 +533,7 @@ export default class ConnectionController {
async removeMongoDBConnection(connectionId: string): Promise<boolean> {
if (!this._connections[connectionId]) {
// No active connection(s) to remove.
vscode.window.showErrorMessage('Connection does not exist.');
void vscode.window.showErrorMessage('Connection does not exist.');

return false;
}
Expand All @@ -559,7 +559,7 @@ export default class ConnectionController {

await this.removeSavedConnection(connectionId);

vscode.window.showInformationMessage('MongoDB connection removed.');
void vscode.window.showInformationMessage('MongoDB connection removed.');

return true;
}
Expand All @@ -571,7 +571,7 @@ export default class ConnectionController {

if (connectionIds.length === 0) {
// No active connection(s) to remove.
vscode.window.showErrorMessage('No connections to remove.');
void vscode.window.showErrorMessage('No connections to remove.');

return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/editors/collectionDocumentsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ implements vscode.TextDocumentContentProvider {
const operationId = uriParams.get(OPERATION_ID_URI_IDENTIFIER);

if (!operationId) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'Unable to list documents: invalid operation'
);

Expand All @@ -58,7 +58,7 @@ implements vscode.TextDocumentContentProvider {
// Ensure we're still connected to the correct connection.
if (connectionId !== this._connectionController.getActiveConnectionId()) {
operation.isCurrentlyFetchingMoreDocuments = false;
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to list documents: no longer connected to ${connectionId}`
);

Expand All @@ -74,7 +74,7 @@ implements vscode.TextDocumentContentProvider {
if (dataservice === null) {
const errorMessage = `Unable to list documents: no longer connected to ${connectionId}`;

vscode.window.showErrorMessage(errorMessage);
void vscode.window.showErrorMessage(errorMessage);

throw new Error(errorMessage);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ implements vscode.TextDocumentContentProvider {
const printableError = error as { message: string };
const errorMessage = `Unable to list documents: ${printableError.message}`;

vscode.window.showErrorMessage(errorMessage);
void vscode.window.showErrorMessage(errorMessage);

throw Error(errorMessage);
}
Expand Down
16 changes: 8 additions & 8 deletions src/editors/editorsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class EditorsController {
)) as EJSON.SerializableTypes;

if (mdbDocument === null) {
vscode.window.showErrorMessage(`
void vscode.window.showErrorMessage(`
Unable to open mongodb document: document ${JSON.stringify(data.documentId)} not found
`);

Expand All @@ -148,7 +148,7 @@ export default class EditorsController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(printableError.message);
void vscode.window.showErrorMessage(printableError.message);

return false;
}
Expand Down Expand Up @@ -196,17 +196,17 @@ export default class EditorsController {
});

// Save document changes to active editor.
activeEditor?.document.save();
await activeEditor?.document.save();

vscode.window.showInformationMessage(
void vscode.window.showInformationMessage(
`The document was saved successfully to '${namespace}'`
);

return true;
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(printableError.message);
void vscode.window.showErrorMessage(printableError.message);

return false;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ export default class EditorsController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to open documents: ${printableError.message}`
);

Expand All @@ -272,7 +272,7 @@ export default class EditorsController {
this._collectionDocumentsOperationsStore.operations[operationId]
.isCurrentlyFetchingMoreDocuments
) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'Already fetching more documents...'
);

Expand All @@ -281,7 +281,7 @@ export default class EditorsController {

// Ensure we're still connected to the correct connection.
if (connectionId !== this._connectionController.getActiveConnectionId()) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to view more documents: no longer connected to ${connectionId}`
);

Expand Down
38 changes: 14 additions & 24 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ export default class PlaygroundController {
this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
this._disconnectFromServiceProvider();
}
);

this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
this._connectToServiceProvider();
void this._connectToServiceProvider();
}
);

Expand Down Expand Up @@ -140,7 +133,7 @@ export default class PlaygroundController {
.map((selection) => this._getSelectedText(selection))
.join('\n');

this._showCodeLensForSelection(
void this._showCodeLensForSelection(
sortedSelections,
changeEvent.textEditor
);
Expand Down Expand Up @@ -173,7 +166,7 @@ export default class PlaygroundController {
lastSelection.end.line === editor.document.lineCount - 1 &&
lastSelectedLineContent.trim().length > 0
) {
editor.edit(edit => {
void editor.edit(edit => {
edit.insert(
new vscode.Position(lastSelection.end.line + 1, 0),
'\n'
Expand All @@ -190,10 +183,6 @@ export default class PlaygroundController {
);
}

_disconnectFromServiceProvider(): void {
this._languageServerController.disconnectFromServiceProvider();
}

async _connectToServiceProvider(): Promise<void> {
await this._languageServerController.disconnectFromServiceProvider();

Expand Down Expand Up @@ -246,7 +235,7 @@ export default class PlaygroundController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to create a playground: ${printableError.message}`
);

Expand Down Expand Up @@ -323,7 +312,7 @@ export default class PlaygroundController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to create a playground: ${printableError.message}`
);

Expand Down Expand Up @@ -455,7 +444,7 @@ export default class PlaygroundController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to open a result document: ${printableError.message}`
);
}
Expand All @@ -467,7 +456,7 @@ export default class PlaygroundController {
.get('confirmRunAll');

if (!this._connectionController.isCurrentlyConnected()) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
'Please connect to a database before running a playground.'
);

Expand Down Expand Up @@ -508,7 +497,8 @@ export default class PlaygroundController {

this._playgroundResult = evaluateResponse.result;

await this._explorerController.refresh();
this._explorerController.refresh();

await this._openPlaygroundResult();

return true;
Expand All @@ -519,7 +509,7 @@ export default class PlaygroundController {
!this._activeTextEditor ||
this._activeTextEditor.document.languageId !== 'mongodb'
) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
"Please open a '.mongodb' playground file before running it."
);

Expand All @@ -533,7 +523,7 @@ export default class PlaygroundController {
!Array.isArray(selections) ||
(selections.length === 1 && this._getSelectedText(selections[0]) === '')
) {
vscode.window.showInformationMessage(
void vscode.window.showInformationMessage(
'Please select one or more lines in the playground.'
);

Expand All @@ -551,7 +541,7 @@ export default class PlaygroundController {
!this._activeTextEditor ||
this._activeTextEditor.document.languageId !== 'mongodb'
) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
"Please open a '.mongodb' playground file before running it."
);

Expand All @@ -569,7 +559,7 @@ export default class PlaygroundController {
!this._activeTextEditor ||
this._activeTextEditor.document.languageId !== 'mongodb'
) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
"Please open a '.mongodb' playground file before running it."
);

Expand Down Expand Up @@ -603,7 +593,7 @@ export default class PlaygroundController {
} catch (error) {
const printableError = error as { message: string };

vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Unable to open a playground: ${printableError.message}`
);

Expand Down
3 changes: 2 additions & 1 deletion src/explorer/collectionTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,10 @@ export default class CollectionTreeItem extends vscode.TreeItem
`${this.databaseName}.${collectionName}`,
(err: Error | null, successfullyDroppedCollection = false) => {
if (err) {
vscode.window.showErrorMessage(
void vscode.window.showErrorMessage(
`Drop collection failed: ${err.message}`
);

return resolve(false);
}

Expand Down
3 changes: 2 additions & 1 deletion src/explorer/connectionTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ export default class ConnectionTreeItem extends vscode.TreeItem
() => resolve(true),
(err) => {
this.isExpanded = false;
vscode.window.showErrorMessage(err);
void vscode.window.showErrorMessage(err);

return resolve(false);
}
);
Expand Down
Loading

0 comments on commit e63a769

Please sign in to comment.