From f4727fe7b5cc1e04ee2698fc226edb94cfd5683a Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 8 Feb 2024 17:16:07 +0000 Subject: [PATCH 1/3] Convert CachedOperation callbacks to use Error type --- .../language-support/contextual/cached-operation.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts index c51970de1e6..95d5c2313f3 100644 --- a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts +++ b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts @@ -1,3 +1,5 @@ +import { asError } from "../../common/helpers-pure"; + /** * A cached mapping from strings to value of type U. */ @@ -7,7 +9,7 @@ export class CachedOperation { private readonly lru: string[]; private readonly inProgressCallbacks: Map< string, - Array<[(u: U) => void, (reason?: any) => void]> + Array<[(u: U) => void, (reason?: Error) => void]> >; constructor( @@ -18,7 +20,7 @@ export class CachedOperation { this.lru = []; this.inProgressCallbacks = new Map< string, - Array<[(u: U) => void, (reason?: any) => void]> + Array<[(u: U) => void, (reason?: Error) => void]> >(); this.cached = new Map(); } @@ -46,7 +48,7 @@ export class CachedOperation { } // Otherwise compute the new value, but leave a callback to allow sharing work - const callbacks: Array<[(u: U) => void, (reason?: any) => void]> = []; + const callbacks: Array<[(u: U) => void, (reason?: Error) => void]> = []; this.inProgressCallbacks.set(t, callbacks); try { const result = await this.operation(t, ...args); @@ -61,7 +63,7 @@ export class CachedOperation { return result; } catch (e) { // Rethrow error on all callbacks - callbacks.forEach((f) => f[1](e)); + callbacks.forEach((f) => f[1](asError(e))); throw e; } finally { this.inProgressCallbacks.delete(t); From 98c96b09eef8e506618a1039938958e59bf5291c Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 8 Feb 2024 17:34:02 +0000 Subject: [PATCH 2/3] Give CachedOperation a type parameter for its args --- .../contextual/cached-operation.ts | 10 +++---- .../contextual/template-provider.ts | 30 +++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts index 95d5c2313f3..c8a0b6bedd7 100644 --- a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts +++ b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts @@ -1,10 +1,10 @@ import { asError } from "../../common/helpers-pure"; /** - * A cached mapping from strings to value of type U. + * A cached mapping from args of type [string, S] to a value of type Promise. */ -export class CachedOperation { - private readonly operation: (t: string, ...args: any[]) => Promise; +export class CachedOperation { + private readonly operation: (t: string, ...args: S) => Promise; private readonly cached: Map; private readonly lru: string[]; private readonly inProgressCallbacks: Map< @@ -13,7 +13,7 @@ export class CachedOperation { >; constructor( - operation: (t: string, ...args: any[]) => Promise, + operation: (t: string, ...args: S) => Promise, private cacheSize = 100, ) { this.operation = operation; @@ -25,7 +25,7 @@ export class CachedOperation { this.cached = new Map(); } - async get(t: string, ...args: any[]): Promise { + async get(t: string, ...args: S): Promise { // Try and retrieve from the cache const fromCache = this.cached.get(t); if (fromCache !== undefined) { diff --git a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts index a18283e658a..19927bd8903 100644 --- a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts +++ b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts @@ -50,7 +50,7 @@ import { MultiCancellationToken } from "../../common/vscode/multi-cancellation-t */ export class TemplateQueryDefinitionProvider implements DefinitionProvider { - private cache: CachedOperation; + private cache: CachedOperation<[CancellationToken], LocationLink[]>; constructor( private cli: CodeQLCliServer, @@ -58,9 +58,7 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider { private dbm: DatabaseManager, private queryStorageDir: string, ) { - this.cache = new CachedOperation( - this.getDefinitions.bind(this), - ); + this.cache = new CachedOperation(this.getDefinitions.bind(this)); } async provideDefinition( @@ -112,7 +110,7 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider { * or from a selected identifier. */ export class TemplateQueryReferenceProvider implements ReferenceProvider { - private cache: CachedOperation; + private cache: CachedOperation<[CancellationToken], FullLocationLink[]>; constructor( private cli: CodeQLCliServer, @@ -120,9 +118,7 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider { private dbm: DatabaseManager, private queryStorageDir: string, ) { - this.cache = new CachedOperation( - this.getReferences.bind(this), - ); + this.cache = new CachedOperation(this.getReferences.bind(this)); } async provideReferences( @@ -185,7 +181,10 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider { * source-language files. */ export class TemplatePrintAstProvider { - private cache: CachedOperation; + private cache: CachedOperation< + [ProgressCallback, CancellationToken], + CoreCompletedQuery + >; constructor( private cli: CodeQLCliServer, @@ -193,9 +192,7 @@ export class TemplatePrintAstProvider { private dbm: DatabaseManager, private queryStorageDir: string, ) { - this.cache = new CachedOperation( - this.getAst.bind(this), - ); + this.cache = new CachedOperation(this.getAst.bind(this)); } async provideAst( @@ -283,15 +280,16 @@ export class TemplatePrintAstProvider { * source-language files. */ export class TemplatePrintCfgProvider { - private cache: CachedOperation<[Uri, Record] | undefined>; + private cache: CachedOperation< + [number, number], + [Uri, Record] + >; constructor( private cli: CodeQLCliServer, private dbm: DatabaseManager, ) { - this.cache = new CachedOperation<[Uri, Record] | undefined>( - this.getCfgUri.bind(this), - ); + this.cache = new CachedOperation(this.getCfgUri.bind(this)); } async provideCfgUri( From 01f6884b6c257679f0a886339b5ba6fefca0839b Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 13 Feb 2024 10:17:15 +0000 Subject: [PATCH 3/3] Undo changes to comment --- .../src/language-support/contextual/cached-operation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts index c8a0b6bedd7..a50e7d03b01 100644 --- a/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts +++ b/extensions/ql-vscode/src/language-support/contextual/cached-operation.ts @@ -1,7 +1,7 @@ import { asError } from "../../common/helpers-pure"; /** - * A cached mapping from args of type [string, S] to a value of type Promise. + * A cached mapping from strings to a value of type U. */ export class CachedOperation { private readonly operation: (t: string, ...args: S) => Promise;