-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Document when a rule was added #21035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f58ab25
Add version, file, and line metadata to `Violation` structs
ntBre 6718d87
[WIP] initial attempt with claude
ntBre 74016c0
update ViolationMetadata derive with violation_metadata attribute
ntBre 6718eb9
add file and line information while we're at it
ntBre 3270346
cleaning up rule script
ntBre ccacc8b
further script improvements and much better results
ntBre 1d68663
script for actually writing the metadata
ntBre c4f7fae
wire up metadata methods to Rule
ntBre c46899d
include metadata in generated docs
ntBre def4d33
delete totally unused scripts from claude
ntBre 9d65b86
include RuleGroup in added metadata
ntBre a73876e
move group to metadata macros
ntBre c0e6545
switch back from String
ntBre 08f4aa0
update add_rule.py
ntBre 3904813
improve rule issue link
ntBre 90e4fcf
update docs with rule group
ntBre 329edc6
combine version with RuleGroup
ntBre 9adea4c
add SourceLocation to `ruff rule` JSON output
ntBre c28ef67
filter file path to avoid path separator issues
ntBre 08f4215
update get_nested_attrs name and add some docs
ntBre b0248ae
remove RuleGroup from map_codes calls
ntBre 32d8f45
add metadata to rules
ntBre b1be096
ignore doctest
ntBre e1ee1ad
delete helper scripts and csv file
ntBre 34ec0fc
update removed rule versions
ntBre 026e775
update stable rule versions
ntBre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import csv | ||
| import glob | ||
| import json | ||
| import re | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| Rule = str | ||
| Version = str | ||
|
|
||
|
|
||
| def kebab_to_pascal(name): | ||
| return "".join(word.capitalize() for word in name.split("-")) | ||
|
|
||
|
|
||
| def load_rules() -> dict[Rule, str]: | ||
| rules = json.loads( | ||
| subprocess.run( | ||
| ["ruff", "rule", "--all", "--output-format", "json"], | ||
| capture_output=True, | ||
| check=True, | ||
| ).stdout | ||
| ) | ||
|
|
||
| return {rule["code"]: kebab_to_pascal(rule["name"]) for rule in rules} | ||
|
|
||
|
|
||
| def load_versions() -> dict[Rule, Version]: | ||
| versions = {} | ||
| with open("ruff_rules_metadata.csv") as f: | ||
| reader = csv.reader(f) | ||
| for i, line in enumerate(reader): | ||
| if i == 0: | ||
| continue | ||
| rule, version, _ = line | ||
| versions[rule] = version | ||
|
|
||
| return versions | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| rules = load_rules() | ||
| versions = load_versions() | ||
|
|
||
| for rule, name in rules.items(): | ||
| print(f"searching for {rule}") | ||
| pattern = re.compile(rf"pub(\(crate\))? struct {name}( [{{]|;|<|\()", re.I) | ||
| for path in glob.glob("crates/ruff_linter/src/rules/**/*.rs", recursive=True): | ||
| path = Path(path) | ||
| contents = path.read_text() | ||
| if pattern.search(contents): | ||
| new_contents = pattern.sub( | ||
| rf'#[violation_metadata(version = "{versions[rule]}")]\n\g<0>', | ||
| contents, | ||
| ) | ||
| path.write_text(new_contents) | ||
| break | ||
| else: | ||
| raise ValueError(f"failed to locate definition for {name}") |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.