diff --git a/packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts b/packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts index 83eb02c9..8fc41365 100644 --- a/packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts +++ b/packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts @@ -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.'); diff --git a/packages/mongodb-ts-autocomplete/src/autocompleter.ts b/packages/mongodb-ts-autocomplete/src/autocompleter.ts index f1927043..742d320e 100644 --- a/packages/mongodb-ts-autocomplete/src/autocompleter.ts +++ b/packages/mongodb-ts-autocomplete/src/autocompleter.ts @@ -202,22 +202,15 @@ declare global { } async autocomplete(code: string): Promise { - 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'; diff --git a/packages/mongodb-ts-autocomplete/src/autocompletion-context.ts b/packages/mongodb-ts-autocomplete/src/autocompletion-context.ts index e0057251..6fbbf11c 100644 --- a/packages/mongodb-ts-autocomplete/src/autocompletion-context.ts +++ b/packages/mongodb-ts-autocomplete/src/autocompletion-context.ts @@ -9,10 +9,12 @@ type CacheOptions = { }; export interface AutocompletionContext { - currentDatabaseAndConnection(): { - connectionId: string; - databaseName: string; - }; + currentDatabaseAndConnection(): + | { + connectionId: string; + databaseName: string; + } + | undefined; databasesForConnection(connectionId: string): Promise; collectionsForDatabase( connectionId: string,