Skip to content

Commit be61664

Browse files
Merge pull request #3342 from github/robertbrignull/cached_operation
Give more types to CachedOperation to avoid use of any
2 parents 8d1480a + 01f6884 commit be61664

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

extensions/ql-vscode/src/language-support/contextual/cached-operation.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
import { asError } from "../../common/helpers-pure";
2+
13
/**
2-
* A cached mapping from strings to value of type U.
4+
* A cached mapping from strings to a value of type U.
35
*/
4-
export class CachedOperation<U> {
5-
private readonly operation: (t: string, ...args: any[]) => Promise<U>;
6+
export class CachedOperation<S extends unknown[], U> {
7+
private readonly operation: (t: string, ...args: S) => Promise<U>;
68
private readonly cached: Map<string, U>;
79
private readonly lru: string[];
810
private readonly inProgressCallbacks: Map<
911
string,
10-
Array<[(u: U) => void, (reason?: any) => void]>
12+
Array<[(u: U) => void, (reason?: Error) => void]>
1113
>;
1214

1315
constructor(
14-
operation: (t: string, ...args: any[]) => Promise<U>,
16+
operation: (t: string, ...args: S) => Promise<U>,
1517
private cacheSize = 100,
1618
) {
1719
this.operation = operation;
1820
this.lru = [];
1921
this.inProgressCallbacks = new Map<
2022
string,
21-
Array<[(u: U) => void, (reason?: any) => void]>
23+
Array<[(u: U) => void, (reason?: Error) => void]>
2224
>();
2325
this.cached = new Map<string, U>();
2426
}
2527

26-
async get(t: string, ...args: any[]): Promise<U> {
28+
async get(t: string, ...args: S): Promise<U> {
2729
// Try and retrieve from the cache
2830
const fromCache = this.cached.get(t);
2931
if (fromCache !== undefined) {
@@ -46,7 +48,7 @@ export class CachedOperation<U> {
4648
}
4749

4850
// Otherwise compute the new value, but leave a callback to allow sharing work
49-
const callbacks: Array<[(u: U) => void, (reason?: any) => void]> = [];
51+
const callbacks: Array<[(u: U) => void, (reason?: Error) => void]> = [];
5052
this.inProgressCallbacks.set(t, callbacks);
5153
try {
5254
const result = await this.operation(t, ...args);
@@ -61,7 +63,7 @@ export class CachedOperation<U> {
6163
return result;
6264
} catch (e) {
6365
// Rethrow error on all callbacks
64-
callbacks.forEach((f) => f[1](e));
66+
callbacks.forEach((f) => f[1](asError(e)));
6567
throw e;
6668
} finally {
6769
this.inProgressCallbacks.delete(t);

extensions/ql-vscode/src/language-support/contextual/template-provider.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ import { MultiCancellationToken } from "../../common/vscode/multi-cancellation-t
5050
*/
5151

5252
export class TemplateQueryDefinitionProvider implements DefinitionProvider {
53-
private cache: CachedOperation<LocationLink[]>;
53+
private cache: CachedOperation<[CancellationToken], LocationLink[]>;
5454

5555
constructor(
5656
private cli: CodeQLCliServer,
5757
private qs: QueryRunner,
5858
private dbm: DatabaseManager,
5959
private queryStorageDir: string,
6060
) {
61-
this.cache = new CachedOperation<LocationLink[]>(
62-
this.getDefinitions.bind(this),
63-
);
61+
this.cache = new CachedOperation(this.getDefinitions.bind(this));
6462
}
6563

6664
async provideDefinition(
@@ -112,17 +110,15 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
112110
* or from a selected identifier.
113111
*/
114112
export class TemplateQueryReferenceProvider implements ReferenceProvider {
115-
private cache: CachedOperation<FullLocationLink[]>;
113+
private cache: CachedOperation<[CancellationToken], FullLocationLink[]>;
116114

117115
constructor(
118116
private cli: CodeQLCliServer,
119117
private qs: QueryRunner,
120118
private dbm: DatabaseManager,
121119
private queryStorageDir: string,
122120
) {
123-
this.cache = new CachedOperation<FullLocationLink[]>(
124-
this.getReferences.bind(this),
125-
);
121+
this.cache = new CachedOperation(this.getReferences.bind(this));
126122
}
127123

128124
async provideReferences(
@@ -185,17 +181,18 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
185181
* source-language files.
186182
*/
187183
export class TemplatePrintAstProvider {
188-
private cache: CachedOperation<CoreCompletedQuery>;
184+
private cache: CachedOperation<
185+
[ProgressCallback, CancellationToken],
186+
CoreCompletedQuery
187+
>;
189188

190189
constructor(
191190
private cli: CodeQLCliServer,
192191
private qs: QueryRunner,
193192
private dbm: DatabaseManager,
194193
private queryStorageDir: string,
195194
) {
196-
this.cache = new CachedOperation<CoreCompletedQuery>(
197-
this.getAst.bind(this),
198-
);
195+
this.cache = new CachedOperation(this.getAst.bind(this));
199196
}
200197

201198
async provideAst(
@@ -283,15 +280,16 @@ export class TemplatePrintAstProvider {
283280
* source-language files.
284281
*/
285282
export class TemplatePrintCfgProvider {
286-
private cache: CachedOperation<[Uri, Record<string, string>] | undefined>;
283+
private cache: CachedOperation<
284+
[number, number],
285+
[Uri, Record<string, string>]
286+
>;
287287

288288
constructor(
289289
private cli: CodeQLCliServer,
290290
private dbm: DatabaseManager,
291291
) {
292-
this.cache = new CachedOperation<[Uri, Record<string, string>] | undefined>(
293-
this.getCfgUri.bind(this),
294-
);
292+
this.cache = new CachedOperation(this.getCfgUri.bind(this));
295293
}
296294

297295
async provideCfgUri(

0 commit comments

Comments
 (0)