Add RDJson support. (#11682)

## Summary

Implement support for RDJson output for `ruff check`, as requested in
#8655.

## Test Plan

Tested using a snapshot test. Same approach as for e.g. the JSON output
formatter.

## Additional info

I tried to keep the implementation close to the JSON implementation.

I had to deviate a bit to make the `suggestions` key work: If there are
no suggestions, then setting `suggestions` to `null` is invalid
according to the JSONSchema. Therefore, I opted for a slightly more
complex implementation, that skips the `suggestions` key entirely if
there are no fixes available for the given diagnostic. Maybe it would
have been easier to set `"suggestions": []`, but I ended up doing it
this way.

I didn't consider notebooks, as I _think_ that RDJson doesn't work with
notebooks. This should be confirmed, and if so, there should be some
form of warning or error emitted when trying to output diagnostics for a
notebook.

I also didn't consider `ruff format`, as this comment:
https://github.com/astral-sh/ruff/issues/8655#issuecomment-1811446160
suggests that that wouldn't be compatible.

I'm new to Rust, any feedback is appreciated. 🙂 I
implemented this in order to have a productive rainy saturday afternoon,
I'm not knowledgeable about RDJson beyond the sources linked in the
issue.
This commit is contained in:
Tobias Fischer 2024-06-02 19:59:57 +02:00 committed by GitHub
parent 6d79ddc0aa
commit 0ea2519e80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 252 additions and 2 deletions

View file

@ -599,7 +599,7 @@ Options:
format is "concise". In preview mode, the default serialization
format is "full" [env: RUFF_OUTPUT_FORMAT=] [possible values: text,
concise, full, json, json-lines, junit, grouped, github, gitlab,
pylint, azure, sarif]
pylint, rdjson, azure, sarif]
-o, --output-file <OUTPUT_FILE>
Specify file to write the linter output to (default: stdout) [env:
RUFF_OUTPUT_FILE=]