Skip to content

Commit

Permalink
Add print linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Anderson committed Aug 28, 2024
1 parent fc03943 commit ed741c7
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ luac.out

.release/*
!.release/local.sh
!.release/local

.scripts/.output

.venv
__pycache__

luacov-html
2 changes: 1 addition & 1 deletion .release/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ if [ -n "$project_version" ]; then
fi
if [ -n "$previous_version" ]; then
echo -n " Previous version: "
print_red: "$previous_version"
print_red "$previous_version"
fi
(
if [[ -n $game_type ]]; then
Expand Down
81 changes: 81 additions & 0 deletions .scripts/check_for_invalid_prints.py
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()
10 changes: 10 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ed741c7

Please sign in to comment.