Skip to content

Commit 0b3ea16

Browse files
committed
Add a result type
1 parent dac4201 commit 0b3ea16

2 files changed

Lines changed: 22 additions & 27 deletions

File tree

extensions/ql-vscode/src/cli.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Readable } from 'stream';
88
import { StringDecoder } from 'string_decoder';
99
import * as tk from 'tree-kill';
1010
import { promisify } from 'util';
11-
import { CancellationToken, Disposable, Uri, window } from 'vscode';
11+
import { CancellationToken, Disposable, Uri } from 'vscode';
1212

1313
import { BQRSInfo, DecodedBqrsChunk } from './pure/bqrs-cli-types';
1414
import { CliConfig } from './config';
@@ -43,6 +43,16 @@ export interface QuerySetup {
4343
compilationCache?: string;
4444
}
4545

46+
/**
47+
* The expected output of `codeql resolve queries --format bylanguage`.
48+
*/
49+
export interface QueryInfoByLanguage {
50+
// What should the value of these objects be? I've only ever seen it as an empty object.
51+
byLanguage: Record<string, Record<string, unknown>>;
52+
noDeclaredLanguage: Record<string, unknown>;
53+
multipleDeclaredLanguages: Record<string, unknown>;
54+
}
55+
4656
/**
4757
* The expected output of `codeql resolve database`.
4858
*/
@@ -486,30 +496,14 @@ export class CodeQLCliServer implements Disposable {
486496
* Finds the language that a query is analyzing.
487497
* @param queryUri The URI of the query
488498
*/
489-
async findLanguage(workspaces: string[], queryUri?: Uri): Promise<string> {
490-
let language = '';
491-
if (queryUri) {
492-
const subcommandArgs = [
493-
'--format', 'bylanguage',
494-
queryUri.fsPath,
495-
'--additional-packs',
496-
workspaces.join(path.delimiter)
497-
];
498-
try {
499-
const JSONResults = await this.runCodeQlCliCommand(['resolve', 'queries'], subcommandArgs, 'Finding query language');
500-
501-
// This corresponds to `codeql resolve queries <query> --format bylanguage | jq -r '.byLanguage | keys[]'`
502-
language = Object.keys(JSON.parse(JSONResults).byLanguage)[0];
503-
}
504-
catch (e) {
505-
// Add an option to manually specify the language, in case automatic language detection fails.
506-
void this.logger.log('Failed to detect language. Using manual prompt instead.');
507-
language = await window.showQuickPick(
508-
['cpp', 'csharp', 'go', 'java', 'javascript', 'python'], { placeHolder: 'Select language' }
509-
) || '';
510-
}
511-
}
512-
return language;
499+
async resolveQueryByLanguage(workspaces: string[], queryUri: Uri): Promise<QueryInfoByLanguage> {
500+
const subcommandArgs = [
501+
'--format', 'bylanguage',
502+
queryUri.fsPath,
503+
'--additional-packs',
504+
workspaces.join(path.delimiter)
505+
];
506+
return JSON.parse(await this.runCodeQlCliCommand(['resolve', 'queries'], subcommandArgs, 'Resolving query by language'));
513507
}
514508

515509
/**

extensions/ql-vscode/src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,11 @@ async function activateWithInstalledDistribution(
563563
// (Temporary new command to test `cliServer.findLanguage`)
564564
ctx.subscriptions.push(
565565
commandRunner('codeQL.findLanguage', async (
566-
uri: Uri | undefined
566+
uri: Uri
567567
) => {
568568
const diskWorkspaceFolders = helpers.getOnDiskWorkspaceFolders();
569-
const language = await cliServer.findLanguage(diskWorkspaceFolders, uri || window.activeTextEditor?.document.uri);
569+
const queryInfo = await cliServer.resolveQueryByLanguage(diskWorkspaceFolders, uri || window.activeTextEditor?.document.uri);
570+
const language = (Object.keys(queryInfo.byLanguage))[0];
570571
void helpers.showAndLogInformationMessage(language);
571572
})
572573
);

0 commit comments

Comments
 (0)