Skip to content

Commit 11e0f49

Browse files
committed
Parse new access paths query BQRS output
1 parent b35d6cb commit 11e0f49

File tree

3 files changed

+105
-23
lines changed

3 files changed

+105
-23
lines changed

extensions/ql-vscode/src/model-editor/languages/ruby/suggestions.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
11
import type { BaseLogger } from "../../../common/logging";
2-
import type { DecodedBqrsChunk } from "../../../common/bqrs-cli-types";
2+
import type {
3+
BqrsCellValue,
4+
BqrsEntityValue,
5+
DecodedBqrsChunk,
6+
} from "../../../common/bqrs-cli-types";
37
import type { ModelsAsDataLanguage } from "../models-as-data";
48
import type { AccessPathSuggestionRow } from "../../suggestions";
59
import { isDefinitionType } from "../../suggestions";
610
import { parseRubyMethodFromPath, rubyMethodSignature } from "./access-paths";
711

12+
function checkTupleFormat(
13+
tuple: BqrsCellValue[],
14+
): tuple is [string, string, string, BqrsEntityValue, string] {
15+
if (tuple.length !== 5) {
16+
return false;
17+
}
18+
19+
const [type, methodName, value, node, definitionType] = tuple;
20+
if (
21+
typeof type !== "string" ||
22+
typeof methodName !== "string" ||
23+
typeof value !== "string" ||
24+
typeof node !== "object" ||
25+
typeof definitionType !== "string"
26+
) {
27+
return false;
28+
}
29+
30+
if (Array.isArray(node)) {
31+
return false;
32+
}
33+
34+
return true;
35+
}
36+
837
export function parseAccessPathSuggestionsResults(
938
bqrs: DecodedBqrsChunk,
1039
_modelsAsDataLanguage: ModelsAsDataLanguage,
1140
logger: BaseLogger,
1241
): AccessPathSuggestionRow[] {
1342
return bqrs.tuples
1443
.map((tuple, index): AccessPathSuggestionRow | null => {
15-
const row = tuple.filter(
16-
(value): value is string => typeof value === "string",
17-
);
18-
19-
if (row.length !== 5) {
44+
if (!checkTupleFormat(tuple)) {
2045
void logger.log(
21-
`Skipping result ${index} because it has the wrong length`,
46+
`Skipping result ${index} because it has the wrong format`,
2247
);
2348
return null;
2449
}
2550

26-
const type = row[0];
27-
const methodName = parseRubyMethodFromPath(row[1]);
28-
const value = row[2];
29-
const details = row[3];
30-
const definitionType = row[4];
51+
const type = tuple[0];
52+
const methodName = parseRubyMethodFromPath(tuple[1]);
53+
const value = tuple[2];
54+
const node = tuple[3];
55+
const definitionType = tuple[4];
3156

3257
if (!isDefinitionType(definitionType)) {
3358
void logger.log(
@@ -45,7 +70,7 @@ export function parseAccessPathSuggestionsResults(
4570
signature: rubyMethodSignature(type, methodName),
4671
},
4772
value,
48-
details,
73+
details: node.label ?? "",
4974
definitionType,
5075
};
5176
})

extensions/ql-vscode/test/unit-tests/model-editor/languages/ruby/suggestions.test.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe("parseAccessPathSuggestionsResults", () => {
2020
kind: "String",
2121
},
2222
{
23-
name: "details",
24-
kind: "String",
23+
name: "node",
24+
kind: "Entity",
2525
},
2626
{
2727
name: "defType",
@@ -33,7 +33,9 @@ describe("parseAccessPathSuggestionsResults", () => {
3333
"Correctness",
3434
"Method[assert!]",
3535
"Argument[self]",
36-
"self in assert!",
36+
{
37+
label: "self in assert!",
38+
},
3739
"parameter",
3840
],
3941
],
@@ -60,4 +62,53 @@ describe("parseAccessPathSuggestionsResults", () => {
6062
},
6163
]);
6264
});
65+
66+
it("should not parse an incorrect result format", async () => {
67+
const bqrsChunk: DecodedBqrsChunk = {
68+
columns: [
69+
{
70+
name: "type",
71+
kind: "String",
72+
},
73+
{
74+
name: "path",
75+
kind: "String",
76+
},
77+
{
78+
name: "value",
79+
kind: "String",
80+
},
81+
{
82+
name: "details",
83+
kind: "String",
84+
},
85+
{
86+
name: "defType",
87+
kind: "String",
88+
},
89+
],
90+
tuples: [
91+
[
92+
"Correctness",
93+
"Method[assert!]",
94+
"Argument[self]",
95+
"self in assert!",
96+
"parameter",
97+
],
98+
["Correctness", "Method[assert!]", "Argument[self]", "parameter"],
99+
],
100+
};
101+
102+
const logger = createMockLogger();
103+
expect(parseAccessPathSuggestionsResults(bqrsChunk, ruby, logger)).toEqual(
104+
[],
105+
);
106+
expect(logger.log).toHaveBeenCalledTimes(2);
107+
expect(logger.log).toHaveBeenCalledWith(
108+
"Skipping result 0 because it has the wrong format",
109+
);
110+
expect(logger.log).toHaveBeenCalledWith(
111+
"Skipping result 1 because it has the wrong format",
112+
);
113+
});
63114
});

extensions/ql-vscode/test/vscode-tests/no-workspace/model-editor/suggestion-queries.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe("runSuggestionsQuery", () => {
2929
kind: "String",
3030
},
3131
{
32-
name: "details",
33-
kind: "String",
32+
name: "node",
33+
kind: "Entity",
3434
},
3535
{
3636
name: "defType",
@@ -42,7 +42,9 @@ describe("runSuggestionsQuery", () => {
4242
"Correctness",
4343
"Method[assert!]",
4444
"Argument[self]",
45-
"self in assert!",
45+
{
46+
label: "self in assert!",
47+
},
4648
"parameter",
4749
],
4850
],
@@ -62,8 +64,8 @@ describe("runSuggestionsQuery", () => {
6264
kind: "String",
6365
},
6466
{
65-
name: "details",
66-
kind: "String",
67+
name: "node",
68+
kind: "Entity",
6769
},
6870
{
6971
name: "defType",
@@ -75,14 +77,18 @@ describe("runSuggestionsQuery", () => {
7577
"Correctness",
7678
"Method[assert!]",
7779
"ReturnValue",
78-
"call to puts",
80+
{
81+
label: "call to puts",
82+
},
7983
"return",
8084
],
8185
[
8286
"Correctness",
8387
"Method[assert!]",
8488
"Argument[self]",
85-
"self in assert!",
89+
{
90+
label: "self in assert!",
91+
},
8692
"parameter",
8793
],
8894
],

0 commit comments

Comments
 (0)