Merge pull request #3320 from github/koesie10/access-paths-query-bqrs

Parse new access paths query BQRS output
This commit is contained in:
Koen Vlaswinkel
2024-02-06 17:03:02 +01:00
committed by GitHub
3 changed files with 121 additions and 39 deletions

View File

@@ -1,10 +1,39 @@
import type { BaseLogger } from "../../../common/logging";
import type { DecodedBqrsChunk } from "../../../common/bqrs-cli-types";
import type {
BqrsCellValue,
BqrsEntityValue,
DecodedBqrsChunk,
} from "../../../common/bqrs-cli-types";
import type { ModelsAsDataLanguage } from "../models-as-data";
import type { AccessPathSuggestionRow } from "../../suggestions";
import { isDefinitionType } from "../../suggestions";
import { parseRubyMethodFromPath, rubyMethodSignature } from "./access-paths";
function checkTupleFormat(
tuple: BqrsCellValue[],
): tuple is [string, string, string, BqrsEntityValue, string] {
if (tuple.length !== 5) {
return false;
}
const [type, methodName, value, node, definitionType] = tuple;
if (
typeof type !== "string" ||
typeof methodName !== "string" ||
typeof value !== "string" ||
typeof node !== "object" ||
typeof definitionType !== "string"
) {
return false;
}
if (Array.isArray(node)) {
return false;
}
return true;
}
export function parseAccessPathSuggestionsResults(
bqrs: DecodedBqrsChunk,
_modelsAsDataLanguage: ModelsAsDataLanguage,
@@ -12,22 +41,18 @@ export function parseAccessPathSuggestionsResults(
): AccessPathSuggestionRow[] {
return bqrs.tuples
.map((tuple, index): AccessPathSuggestionRow | null => {
const row = tuple.filter(
(value): value is string => typeof value === "string",
);
if (row.length !== 5) {
if (!checkTupleFormat(tuple)) {
void logger.log(
`Skipping result ${index} because it has the wrong length`,
`Skipping result ${index} because it has the wrong format`,
);
return null;
}
const type = row[0];
const methodName = parseRubyMethodFromPath(row[1]);
const value = row[2];
const details = row[3];
const definitionType = row[4];
const type = tuple[0];
const methodName = parseRubyMethodFromPath(tuple[1]);
const value = tuple[2];
const node = tuple[3];
const definitionType = tuple[4];
if (!isDefinitionType(definitionType)) {
void logger.log(
@@ -45,7 +70,7 @@ export function parseAccessPathSuggestionsResults(
signature: rubyMethodSignature(type, methodName),
},
value,
details,
details: node.label ?? "",
definitionType,
};
})

View File

@@ -5,6 +5,65 @@ import { parseAccessPathSuggestionsResults } from "../../../../../src/model-edit
describe("parseAccessPathSuggestionsResults", () => {
it("should parse the results", async () => {
const bqrsChunk: DecodedBqrsChunk = {
columns: [
{
name: "type",
kind: "String",
},
{
name: "path",
kind: "String",
},
{
name: "value",
kind: "String",
},
{
name: "node",
kind: "Entity",
},
{
name: "defType",
kind: "String",
},
],
tuples: [
[
"Correctness",
"Method[assert!]",
"Argument[self]",
{
label: "self in assert!",
},
"parameter",
],
],
};
const result = parseAccessPathSuggestionsResults(
bqrsChunk,
ruby,
createMockLogger(),
);
expect(result).toEqual([
{
method: {
packageName: "",
typeName: "Correctness",
methodName: "assert!",
methodParameters: "",
signature: "Correctness#assert!",
},
value: "Argument[self]",
details: "self in assert!",
definitionType: "parameter",
},
]);
});
it("should not parse an incorrect result format", async () => {
const bqrsChunk: DecodedBqrsChunk = {
columns: [
{
@@ -36,28 +95,20 @@ describe("parseAccessPathSuggestionsResults", () => {
"self in assert!",
"parameter",
],
["Correctness", "Method[assert!]", "Argument[self]", "parameter"],
],
};
const result = parseAccessPathSuggestionsResults(
bqrsChunk,
ruby,
createMockLogger(),
const logger = createMockLogger();
expect(parseAccessPathSuggestionsResults(bqrsChunk, ruby, logger)).toEqual(
[],
);
expect(logger.log).toHaveBeenCalledTimes(2);
expect(logger.log).toHaveBeenCalledWith(
"Skipping result 0 because it has the wrong format",
);
expect(logger.log).toHaveBeenCalledWith(
"Skipping result 1 because it has the wrong format",
);
expect(result).toEqual([
{
method: {
packageName: "",
typeName: "Correctness",
methodName: "assert!",
methodParameters: "",
signature: "Correctness#assert!",
},
value: "Argument[self]",
details: "self in assert!",
definitionType: "parameter",
},
]);
});
});

View File

@@ -31,8 +31,8 @@ describe("runSuggestionsQuery", () => {
kind: "String",
},
{
name: "details",
kind: "String",
name: "node",
kind: "Entity",
},
{
name: "defType",
@@ -44,7 +44,9 @@ describe("runSuggestionsQuery", () => {
"Correctness",
"Method[assert!]",
"Argument[self]",
"self in assert!",
{
label: "self in assert!",
},
"parameter",
],
],
@@ -64,8 +66,8 @@ describe("runSuggestionsQuery", () => {
kind: "String",
},
{
name: "details",
kind: "String",
name: "node",
kind: "Entity",
},
{
name: "defType",
@@ -77,14 +79,18 @@ describe("runSuggestionsQuery", () => {
"Correctness",
"Method[assert!]",
"ReturnValue",
"call to puts",
{
label: "call to puts",
},
"return",
],
[
"Correctness",
"Method[assert!]",
"Argument[self]",
"self in assert!",
{
label: "self in assert!",
},
"parameter",
],
],