Skip to content

Commit 99783aa

Browse files
authored
feat(shell-api,autocomplete): skip deprecated methods MONGOSH-509 (#589)
Mark methods as deprecated and skip them for autocompletion.
1 parent 6fb4b8a commit 99783aa

28 files changed

+117
-31
lines changed

packages/autocomplete/index.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ describe('completer.completer', () => {
179179

180180
it('returns all suggestions', async() => {
181181
const i = 'db.';
182-
const dbComplete = Object.keys(shellSignatures.Database.attributes as any);
183-
const adjusted = dbComplete.map(c => `${i}${c}`);
182+
const attr = shellSignatures.Database.attributes as any;
183+
const dbComplete = Object.keys(attr);
184+
const adjusted = dbComplete
185+
.filter(c => !attr[c].deprecated)
186+
.map(c => `${i}${c}`);
184187
expect(await completer(noParams, i)).to.deep.equal([adjusted, i]);
185188
});
186189

@@ -221,7 +224,7 @@ describe('completer.completer', () => {
221224
it('returns all suggestions', async() => {
222225
const i = 'db.shipwrecks.';
223226
const collComplete = Object.keys(shellSignatures.Collection.attributes as any);
224-
const adjusted = collComplete.filter(c => !['count', 'update', 'remove'].includes(c)).map(c => `${i}${c}`);
227+
const adjusted = collComplete.filter(c => !['count', 'update', 'remove', 'insert', 'save'].includes(c)).map(c => `${i}${c}`);
225228

226229
expect(await completer(sharded440, i)).to.deep.equal([adjusted, i]);
227230
});

packages/autocomplete/index.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint complexity: 0, camelcase: 0, no-nested-ternary: 0 */
22

3-
import { signatures as shellSignatures, Topologies } from '@mongosh/shell-api';
3+
import { signatures as shellSignatures, Topologies, TypeSignature } from '@mongosh/shell-api';
44
import semver from 'semver';
55
import {
66
CONVERSION_OPERATORS,
@@ -14,6 +14,8 @@ import {
1414
ON_PREM
1515
} from 'mongodb-ace-autocompleter';
1616

17+
type TypeSignatureAttributes = { [key: string]: TypeSignature };
18+
1719
export interface AutocompleteParameters {
1820
topology: () => Topologies;
1921
connectionInfo: () => undefined | {
@@ -49,13 +51,13 @@ const GROUP = '$group';
4951
* @returns {array} Matching Completions, Current User Input.
5052
*/
5153
async function completer(params: AutocompleteParameters, line: string): Promise<[string[], string]> {
52-
const SHELL_COMPLETIONS = shellSignatures.ShellApi.attributes;
53-
const COLL_COMPLETIONS = shellSignatures.Collection.attributes;
54-
const DB_COMPLETIONS = shellSignatures.Database.attributes;
55-
const AGG_CURSOR_COMPLETIONS = shellSignatures.AggregationCursor.attributes;
56-
const COLL_CURSOR_COMPLETIONS = shellSignatures.Cursor.attributes;
57-
const RS_COMPLETIONS = shellSignatures.ReplicaSet.attributes;
58-
const SHARD_COMPLETE = shellSignatures.Shard.attributes;
54+
const SHELL_COMPLETIONS = shellSignatures.ShellApi.attributes as TypeSignatureAttributes;
55+
const COLL_COMPLETIONS = shellSignatures.Collection.attributes as TypeSignatureAttributes;
56+
const DB_COMPLETIONS = shellSignatures.Database.attributes as TypeSignatureAttributes;
57+
const AGG_CURSOR_COMPLETIONS = shellSignatures.AggregationCursor.attributes as TypeSignatureAttributes;
58+
const COLL_CURSOR_COMPLETIONS = shellSignatures.Cursor.attributes as TypeSignatureAttributes;
59+
const RS_COMPLETIONS = shellSignatures.ReplicaSet.attributes as TypeSignatureAttributes;
60+
const SHARD_COMPLETE = shellSignatures.Shard.attributes as TypeSignatureAttributes;
5961

6062
// keep initial line param intact to always return in return statement
6163
// check for contents of line with:
@@ -174,17 +176,29 @@ function filterQueries(params: AutocompleteParameters, completions: any, prefix:
174176
return hits.map(h => `${split}${h.name}`);
175177
}
176178

177-
function filterShellAPI(params: AutocompleteParameters, completions: any, prefix: string, split?: string[]): string[] {
178-
const hits: string[] = Object.keys(completions).filter((c: any) => {
179+
function filterShellAPI(
180+
params: AutocompleteParameters,
181+
completions: { [key: string]: TypeSignature },
182+
prefix: string,
183+
split?: string[]): string[] {
184+
const hits: string[] = Object.keys(completions).filter((c: string) => {
179185
if (!c.startsWith(prefix)) return false;
186+
if (completions[c].deprecated) return false;
187+
180188
const serverVersion = params.connectionInfo()?.server_version;
181189
if (!serverVersion) return true;
190+
191+
const acceptableVersions = completions[c].serverVersions;
182192
const isAcceptableVersion =
183-
(semver.gte(serverVersion, completions[c].serverVersions[0]) &&
184-
semver.lte(serverVersion, completions[c].serverVersions[1]));
193+
!acceptableVersions ||
194+
(semver.gte(serverVersion, acceptableVersions[0]) &&
195+
semver.lte(serverVersion, acceptableVersions[1]));
196+
197+
const acceptableTopologies = completions[c].topologies;
185198
const isAcceptableTopology =
186-
!completions[c].topologies ||
187-
completions[c].topologies.includes(params.topology());
199+
!acceptableTopologies ||
200+
acceptableTopologies.includes(params.topology());
201+
188202
return isAcceptableVersion && isAcceptableTopology;
189203
});
190204

packages/shell-api/src/aggregation-cursor.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('AggregationCursor', () => {
2424
expect(signatures.AggregationCursor.attributes.map).to.deep.equal({
2525
type: 'function',
2626
returnsPromise: false,
27+
deprecated: false,
2728
returnType: 'AggregationCursor',
2829
platforms: ALL_PLATFORMS,
2930
topologies: ALL_TOPOLOGIES,

packages/shell-api/src/bulk.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('Bulk API', () => {
3535
expect(signatures.Bulk.attributes.find).to.deep.equal({
3636
type: 'function',
3737
returnsPromise: false,
38+
deprecated: false,
3839
returnType: 'BulkFindOp',
3940
platforms: ALL_PLATFORMS,
4041
topologies: ALL_TOPOLOGIES,
@@ -236,6 +237,7 @@ describe('Bulk API', () => {
236237
expect(signatures.BulkFindOp.attributes.hint).to.deep.equal({
237238
type: 'function',
238239
returnsPromise: false,
240+
deprecated: false,
239241
returnType: 'BulkFindOp',
240242
platforms: ALL_PLATFORMS,
241243
topologies: ALL_TOPOLOGIES,

packages/shell-api/src/change-stream-cursor.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('ChangeStreamCursor', () => {
3030
expect(signatures.ChangeStreamCursor.attributes.next).to.deep.equal({
3131
type: 'function',
3232
returnsPromise: true,
33+
deprecated: false,
3334
returnType: { type: 'unknown', attributes: {} },
3435
platforms: ALL_PLATFORMS,
3536
topologies: ALL_TOPOLOGIES,

packages/shell-api/src/change-stream-cursor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
returnsPromise,
44
returnType,
55
hasAsyncChild,
6-
ShellApiClass
6+
ShellApiClass,
7+
deprecated
78
} from './decorators';
89
import {
910
ChangeStream,
@@ -58,6 +59,7 @@ export default class ChangeStreamCursor extends ShellApiClass {
5859
}
5960

6061
@returnsPromise
62+
@deprecated
6163
async hasNext(): Promise<void> {
6264
printWarning(
6365
'If there are no documents in the batch, hasNext will block. Use tryNext if you want to check if there ' +

packages/shell-api/src/collection.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('Collection', () => {
4040
expect(signatures.Collection.attributes.aggregate).to.deep.equal({
4141
type: 'function',
4242
returnsPromise: true,
43+
deprecated: false,
4344
returnType: 'AggregationCursor',
4445
platforms: ALL_PLATFORMS,
4546
topologies: ALL_TOPOLOGIES,

packages/shell-api/src/collection.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
serverVersions,
1010
ShellApiClass,
1111
shellApiClassDefault,
12-
topologies
12+
topologies,
13+
deprecated
1314
} from './decorators';
1415
import { ADMIN_DB, asPrintable, namespaceInfo, ServerVersions, Topologies } from './enums';
1516
import {
@@ -233,6 +234,7 @@ export default class Collection extends ShellApiClass {
233234
* @returns {Integer} The promise of the count.
234235
*/
235236
@returnsPromise
237+
@deprecated
236238
@serverVersions([ServerVersions.earliest, '4.0.0'])
237239
async count(query = {}, options: CountOptions = {}): Promise<number> {
238240
this._emitCollectionApiCall(
@@ -592,6 +594,8 @@ export default class Collection extends ShellApiClass {
592594
* @return {InsertManyResult}
593595
*/
594596
@returnsPromise
597+
@deprecated
598+
@serverVersions([ServerVersions.earliest, '3.6.0'])
595599
async insert(docs: Document | Document[], options: BulkWriteOptions = {}): Promise<InsertManyResult> {
596600
printDeprecationWarning(
597601
'Collection.insert() is deprecated. Use insertOne, insertMany or bulkWrite.',
@@ -702,6 +706,7 @@ export default class Collection extends ShellApiClass {
702706
* @return {Promise}
703707
*/
704708
@returnsPromise
709+
@deprecated
705710
@serverVersions([ServerVersions.earliest, '3.2.0'])
706711
async remove(query: Document, options: boolean | RemoveShellOptions = {}): Promise<DeleteResult | Document> {
707712
printDeprecationWarning(
@@ -730,6 +735,7 @@ export default class Collection extends ShellApiClass {
730735
}
731736

732737
@returnsPromise
738+
@deprecated
733739
save(): Promise<void> {
734740
throw new MongoshInvalidInputError('Collection.save() is deprecated. Use insertOne, insertMany, updateOne or updateMany.');
735741
}
@@ -772,6 +778,7 @@ export default class Collection extends ShellApiClass {
772778
}
773779

774780
@returnsPromise
781+
@deprecated
775782
@serverVersions([ServerVersions.earliest, '3.2.0'])
776783
async update(filter: Document, update: Document, options: UpdateOptions & { multi?: boolean } = {}): Promise<UpdateResult | Document> {
777784
printDeprecationWarning(

packages/shell-api/src/cursor.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('Cursor', () => {
2828
expect(signatures.Cursor.attributes.map).to.deep.equal({
2929
type: 'function',
3030
returnsPromise: false,
31+
deprecated: false,
3132
returnType: 'Cursor',
3233
platforms: ALL_PLATFORMS,
3334
topologies: ALL_TOPOLOGIES,

packages/shell-api/src/cursor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
serverVersions,
77
ShellApiClass,
88
shellApiClassDefault,
9-
toShellResult
9+
toShellResult,
10+
deprecated
1011
} from './decorators';
1112
import {
1213
ServerVersions,
@@ -75,7 +76,6 @@ export default class Cursor extends ShellApiClass {
7576
if (optionFlagNumber === 4) {
7677
throw new MongoshUnimplementedError('the slaveOk option is not supported.', CommonErrors.NotImplemented);
7778
}
78-
7979
const optionFlag: CursorFlag | undefined = (CURSOR_FLAGS as any)[optionFlagNumber];
8080

8181
if (!optionFlag) {
@@ -340,6 +340,7 @@ export default class Cursor extends ShellApiClass {
340340
return this;
341341
}
342342

343+
@deprecated
343344
@serverVersions([ServerVersions.earliest, '4.0.0'])
344345
maxScan(): void {
345346
throw new MongoshDeprecatedError(

0 commit comments

Comments
 (0)