[K9VULN-4719] Allow rules to specify beforeAll
and afterAll
functions
#695
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem are you trying to solve?
Some rules would be easier to write if there was a way to run code before any
visit
function is invoked or code after allvisit
functions are invoked.This is useful when the rule's logic depends on the presence of absence of multiple conditions. For example, a C# rule might want to enforce that a type extending System.Exception implements all 3 required constructors, e.g. like so). A rule like this could capture all constructor nodes, set global booleans within the
visit
function if they match a specific pattern, and then after all nodes are visited, determine whether to report a violation or not.Currently, one could hack together this functionality by incrementing an integer within each visit function and comparing it with the array length to determine whether it's the first or last invocation, but that's...awful.
What is your solution?
beforeAll
function, call it before processing any query matches.afterAll
function, call it after processing all query matches.Alternatives considered
What the reviewer should know