-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move getCandidates to the shared directory
- Loading branch information
1 parent
d9ae0c6
commit 2fdafaf
Showing
6 changed files
with
178 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
extensions/ql-vscode/src/model-editor/shared/auto-model-candidates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { Method, MethodSignature } from "../method"; | ||
import type { ModeledMethod } from "../modeled-method"; | ||
import type { Mode } from "./mode"; | ||
import { groupMethods, sortGroupNames, sortMethods } from "./sorting"; | ||
|
||
/** | ||
* Return the candidates that the model should be run on. This includes limiting the number of | ||
* candidates to the candidate limit and filtering out anything that is already modeled and respecting | ||
* the order in the UI. | ||
* @param mode Whether it is application or framework mode. | ||
* @param methods all methods. | ||
* @param modeledMethodsBySignature the currently modeled methods. | ||
* @returns list of modeled methods that are candidates for modeling. | ||
*/ | ||
|
||
export function getCandidates( | ||
mode: Mode, | ||
methods: readonly Method[], | ||
modeledMethodsBySignature: Record<string, readonly ModeledMethod[]>, | ||
processedByAutoModelMethods: Set<string>, | ||
): MethodSignature[] { | ||
// Filter out any methods already processed by auto-model | ||
methods = methods.filter( | ||
(m) => !processedByAutoModelMethods.has(m.signature), | ||
); | ||
|
||
// Sort the same way as the UI so we send the first ones listed in the UI first | ||
const grouped = groupMethods(methods, mode); | ||
const sortedGroupNames = sortGroupNames(grouped); | ||
const sortedMethods = sortedGroupNames.flatMap((name) => | ||
sortMethods(grouped[name]), | ||
); | ||
|
||
const candidates: MethodSignature[] = []; | ||
|
||
for (const method of sortedMethods) { | ||
const modeledMethods: ModeledMethod[] = [ | ||
...(modeledMethodsBySignature[method.signature] ?? []), | ||
]; | ||
|
||
// Anything that is modeled is not a candidate | ||
if (modeledMethods.some((m) => m.type !== "none")) { | ||
continue; | ||
} | ||
|
||
// A method that is supported is modeled outside of the model file, so it is not a candidate. | ||
if (method.supported) { | ||
continue; | ||
} | ||
|
||
// The rest are candidates | ||
candidates.push(method); | ||
} | ||
return candidates; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
extensions/ql-vscode/test/unit-tests/model-editor/shared/auto-model-candidates.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import type { Method } from "../../../../src/model-editor/method"; | ||
import { EndpointType } from "../../../../src/model-editor/method"; | ||
import type { ModeledMethod } from "../../../../src/model-editor/modeled-method"; | ||
import { getCandidates } from "../../../../src/model-editor/shared/auto-model-candidates"; | ||
import { Mode } from "../../../../src/model-editor/shared/mode"; | ||
|
||
describe("getCandidates", () => { | ||
it("doesn't return methods that are already modelled", () => { | ||
const methods: Method[] = [ | ||
{ | ||
library: "my.jar", | ||
signature: "org.my.A#x()", | ||
endpointType: EndpointType.Method, | ||
packageName: "org.my", | ||
typeName: "A", | ||
methodName: "x", | ||
methodParameters: "()", | ||
supported: false, | ||
supportedType: "none", | ||
usages: [], | ||
}, | ||
]; | ||
const modeledMethods: Record<string, ModeledMethod[]> = { | ||
"org.my.A#x()": [ | ||
{ | ||
type: "neutral", | ||
kind: "sink", | ||
provenance: "manual", | ||
signature: "org.my.A#x()", | ||
endpointType: EndpointType.Method, | ||
packageName: "org.my", | ||
typeName: "A", | ||
methodName: "x", | ||
methodParameters: "()", | ||
}, | ||
], | ||
}; | ||
const candidates = getCandidates( | ||
Mode.Application, | ||
methods, | ||
modeledMethods, | ||
new Set(), | ||
); | ||
expect(candidates.length).toEqual(0); | ||
}); | ||
|
||
it("doesn't return methods that are supported from other sources", () => { | ||
const methods: Method[] = [ | ||
{ | ||
library: "my.jar", | ||
signature: "org.my.A#x()", | ||
endpointType: EndpointType.Method, | ||
packageName: "org.my", | ||
typeName: "A", | ||
methodName: "x", | ||
methodParameters: "()", | ||
supported: true, | ||
supportedType: "none", | ||
usages: [], | ||
}, | ||
]; | ||
const modeledMethods = {}; | ||
const candidates = getCandidates( | ||
Mode.Application, | ||
methods, | ||
modeledMethods, | ||
new Set(), | ||
); | ||
expect(candidates.length).toEqual(0); | ||
}); | ||
|
||
it("doesn't return methods that are already processed by auto model", () => { | ||
const methods: Method[] = [ | ||
{ | ||
library: "my.jar", | ||
signature: "org.my.A#x()", | ||
endpointType: EndpointType.Method, | ||
packageName: "org.my", | ||
typeName: "A", | ||
methodName: "x", | ||
methodParameters: "()", | ||
supported: false, | ||
supportedType: "none", | ||
usages: [], | ||
}, | ||
]; | ||
const modeledMethods = {}; | ||
const candidates = getCandidates( | ||
Mode.Application, | ||
methods, | ||
modeledMethods, | ||
new Set(["org.my.A#x()"]), | ||
); | ||
expect(candidates.length).toEqual(0); | ||
}); | ||
|
||
it("returns methods that are neither modeled nor supported from other sources", () => { | ||
const methods: Method[] = []; | ||
methods.push({ | ||
library: "my.jar", | ||
signature: "org.my.A#x()", | ||
endpointType: EndpointType.Method, | ||
packageName: "org.my", | ||
typeName: "A", | ||
methodName: "x", | ||
methodParameters: "()", | ||
supported: false, | ||
supportedType: "none", | ||
usages: [], | ||
}); | ||
const modeledMethods = {}; | ||
const candidates = getCandidates( | ||
Mode.Application, | ||
methods, | ||
modeledMethods, | ||
new Set(), | ||
); | ||
expect(candidates.length).toEqual(1); | ||
}); | ||
}); |