Skip to content

Commit

Permalink
ruff: load all the error codes at once
Browse files Browse the repository at this point in the history
  • Loading branch information
praiskup committed Aug 26, 2024
1 parent e67d200 commit 06688fc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions vcs-diff-lint-csdiff-ruff
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The script accepts the same parameters as `ruff check` itself.
"""

import os
from functools import cache
import sys
import json
from subprocess import Popen, PIPE
Expand All @@ -24,16 +25,19 @@ def ruff_check():
return json.loads(out)


def ruff_code_to_name(code):
@cache
def ruff_code_to_name():
"""
Convert noqa code e.g. F401 to its human-readable name, e.g. unused-import
Introspect ruff and map all possible noqa codes to a human-readable names.
"""
# This implementation will likely kill all ruff performance benefits
cmd = ["ruff", "rule", code, "--output-format=json"]
cmd = ["ruff", "rule", "--all", "--output-format=json"]
with Popen(cmd, stdout=PIPE) as proc:
out, _err = proc.communicate(timeout=60)
data = json.loads(out)
return data["name"]
the_map = {}
array = json.loads(out)
for rule in array:
the_map[rule["code"]] = rule['name']
return the_map


def main():
Expand All @@ -46,7 +50,7 @@ def main():
column = defect["location"]["column"] or ""
colsep = ":" if column else ""
event = "{0}[{1}]".format(
defect["code"], ruff_code_to_name(defect["code"]))
defect["code"], ruff_code_to_name()[defect["code"]])

print("Error: RUFF_WARNING:")
print("{file}:{line}{colsep}{column}: {event}: {msg}".format(
Expand Down

0 comments on commit 06688fc

Please sign in to comment.