1
1
/* eslint complexity: 0, camelcase: 0, no-nested-ternary: 0 */
2
2
3
- import { signatures as shellSignatures , Topologies } from '@mongosh/shell-api' ;
3
+ import { signatures as shellSignatures , Topologies , TypeSignature } from '@mongosh/shell-api' ;
4
4
import semver from 'semver' ;
5
5
import {
6
6
CONVERSION_OPERATORS ,
@@ -14,6 +14,8 @@ import {
14
14
ON_PREM
15
15
} from 'mongodb-ace-autocompleter' ;
16
16
17
+ type TypeSignatureAttributes = { [ key : string ] : TypeSignature } ;
18
+
17
19
export interface AutocompleteParameters {
18
20
topology : ( ) => Topologies ;
19
21
connectionInfo : ( ) => undefined | {
@@ -49,13 +51,13 @@ const GROUP = '$group';
49
51
* @returns {array } Matching Completions, Current User Input.
50
52
*/
51
53
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 ;
59
61
60
62
// keep initial line param intact to always return in return statement
61
63
// check for contents of line with:
@@ -174,17 +176,29 @@ function filterQueries(params: AutocompleteParameters, completions: any, prefix:
174
176
return hits . map ( h => `${ split } ${ h . name } ` ) ;
175
177
}
176
178
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 ) => {
179
185
if ( ! c . startsWith ( prefix ) ) return false ;
186
+ if ( completions [ c ] . deprecated ) return false ;
187
+
180
188
const serverVersion = params . connectionInfo ( ) ?. server_version ;
181
189
if ( ! serverVersion ) return true ;
190
+
191
+ const acceptableVersions = completions [ c ] . serverVersions ;
182
192
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 ;
185
198
const isAcceptableTopology =
186
- ! completions [ c ] . topologies ||
187
- completions [ c ] . topologies . includes ( params . topology ( ) ) ;
199
+ ! acceptableTopologies ||
200
+ acceptableTopologies . includes ( params . topology ( ) ) ;
201
+
188
202
return isAcceptableVersion && isAcceptableTopology ;
189
203
} ) ;
190
204
0 commit comments