Skip to content
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
4 changes: 1 addition & 3 deletions packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ describe('MongoDBAutocompleter', function () {

it('deals with no connection', async function () {
autocompleterContext.currentDatabaseAndConnection = () => {
const error = new Error('No connection');
error.name = 'MongoshInvalidInputError';
throw error;
return undefined;
};

const completions = await autocompleter.autocomplete('db.');
Expand Down
21 changes: 7 additions & 14 deletions packages/mongodb-ts-autocomplete/src/autocompleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,15 @@ declare global {
}

async autocomplete(code: string): Promise<AutoCompletion[]> {
let connectionId: string;
let databaseName: string;

// If there's no known connection, currentDatabaseAndConnection() will
// error, but we won't be able to generate types for a connection, db
// object, etc, anyway. So just return no results in that case.
try {
({ connectionId, databaseName } =
this.context.currentDatabaseAndConnection());
} catch (err: any) {
if (err.name === 'MongoshInvalidInputError') {
return [];
}
throw err;
// If there's no known connection we won't be able to generate types for a
// connection, db object, etc. So just return no results in that case.
const dbAndConnection = this.context.currentDatabaseAndConnection();
if (!dbAndConnection) {
return [];
}

const { connectionId, databaseName } = dbAndConnection;

const tsAst = compileSourceFile(code);
const collectionName = inferCollectionNameFromFunctionCall(tsAst) || 'test';

Expand Down
10 changes: 6 additions & 4 deletions packages/mongodb-ts-autocomplete/src/autocompletion-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ type CacheOptions = {
};

export interface AutocompletionContext {
currentDatabaseAndConnection(): {
connectionId: string;
databaseName: string;
};
currentDatabaseAndConnection():
| {
connectionId: string;
databaseName: string;
}
| undefined;
databasesForConnection(connectionId: string): Promise<string[]>;
collectionsForDatabase(
connectionId: string,
Expand Down