-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rob Anderson
committed
Aug 28, 2024
1 parent
fc03943
commit ed741c7
Showing
5 changed files
with
540 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,9 +41,11 @@ luac.out | |
|
||
.release/* | ||
!.release/local.sh | ||
!.release/local | ||
|
||
.scripts/.output | ||
|
||
.venv | ||
__pycache__ | ||
|
||
luacov-html |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
import re | ||
import sys | ||
|
||
|
||
def check_print_statements(file_path): | ||
violations = [] | ||
|
||
with open(file_path, "r") as file: | ||
lines = file.readlines() | ||
|
||
for line_number, line in enumerate(lines, start=1): | ||
match = re.search(r"\bprint\b", line) | ||
if match: | ||
column_number = match.start() + 1 | ||
if not re.search(r"\bself:Print\b|\bG_RLF:Print\b", line): | ||
violations.append( | ||
{ | ||
"ruleId": "invalid-print", | ||
"message": { | ||
"text": f"Invalid `print`, use `self:Print(...)` or `G_RLF:Print(...)`" | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": {"uri": file_path}, | ||
"region": { | ||
"startLine": line_number, | ||
"startColumn": column_number, | ||
}, | ||
} | ||
} | ||
], | ||
} | ||
) | ||
|
||
return violations | ||
|
||
|
||
def generate_sarif(violations): | ||
sarif_output = { | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Invalid Print", | ||
"rules": [ | ||
{ | ||
"id": "invalid-print", | ||
"shortDescription": { | ||
"text": "Disallowed `print` statements" | ||
}, | ||
"fullDescription": { | ||
"text": "Lua files should not contain `print` statements that are not `self:Print(...)` or `G_RLF:Print(...)`." | ||
}, | ||
} | ||
], | ||
} | ||
}, | ||
"results": violations, | ||
} | ||
], | ||
} | ||
return sarif_output | ||
|
||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
print("Usage: check_for_invalid_prints.py <file_path>") | ||
sys.exit(1) | ||
|
||
file_path = sys.argv[1] | ||
violations = check_print_statements(file_path) | ||
sarif_output = generate_sarif(violations) | ||
|
||
print(json.dumps(sarif_output, indent=2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 |
---|---|---|
|
@@ -37,6 +37,16 @@ lint: | |
- [email protected] | ||
- [email protected] | ||
- [email protected] | ||
- no-invalid-prints | ||
definitions: | ||
- name: no-invalid-prints | ||
files: [lua] | ||
runtime: python | ||
commands: | ||
- name: lint | ||
output: sarif | ||
run: python3 ${workspace}/.scripts/check_for_invalid_prints.py ${target} | ||
success_codes: [0, 1] | ||
actions: | ||
enabled: | ||
- trunk-announce | ||
|
Oops, something went wrong.