Merge registry into codes (#4651)

* Document codes.rs

* Refactor codes.rs before merging

Helper script:
```python
# %%

from pathlib import Path

codes = Path("crates/ruff/src/codes.rs").read_text().splitlines()
rules = Path("a.txt").read_text().strip().splitlines()
rule_map = {i.split("::")[-1]: i for i in rules}

# %%

codes_new = []
for line in codes:
    if ", Rule::" in line:
        left, right = line.split(", Rule::")
        right = right[:-2]
        line = left + ", " + rule_map[right] + "),"
    codes_new.append(line)

# %%

Path("crates/ruff/src/codes.rs").write_text("\n".join(codes_new))
```

Co-authored-by: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com>
This commit is contained in:
konstin 2023-06-02 12:33:01 +02:00 committed by GitHub
parent c4fdbf8903
commit 602b4b3519
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 847 additions and 1610 deletions

View file

@ -103,8 +103,7 @@ pub struct {name};
impl Violation for {name} {{
#[derive_message_formats]
fn message(&self) -> String {{
todo!("implement message");
format!("TODO: write message")
format!("TODO: write message: {{}}", todo!("implement message"))
}}
}}
""",
@ -116,56 +115,6 @@ pub(crate) fn {rule_name_snake}(checker: &mut Checker) {{}}
""",
)
# Add the relevant code-to-violation pair to `src/registry.rs`.
content = (ROOT_DIR / "crates/ruff/src/registry.rs").read_text()
seen_macro = False
has_written = False
has_seen_linter = False
with (ROOT_DIR / "crates/ruff/src/registry.rs").open("w") as fp:
lines = []
for line in content.splitlines():
if has_written:
fp.write(line)
fp.write("\n")
continue
if line.startswith("ruff_macros::register_rules!"):
seen_macro = True
fp.write(line)
fp.write("\n")
continue
if not seen_macro:
fp.write(line)
fp.write("\n")
continue
if line.strip() == f"// {linter}":
indent = get_indent(line)
lines.append(f"{indent}rules::{dir_name(linter)}::rules::{name},")
has_seen_linter = True
fp.write(line)
fp.write("\n")
continue
if not has_seen_linter:
fp.write(line)
fp.write("\n")
continue
if not line.strip().startswith("// "):
lines.append(line)
else:
lines.sort()
fp.write("\n".join(lines))
fp.write("\n")
fp.write(line)
fp.write("\n")
has_written = True
assert has_written
text = ""
with (ROOT_DIR / "crates/ruff/src/codes.rs").open("r") as fp:
while (line := next(fp)).strip() != f"// {linter}":
@ -177,17 +126,15 @@ pub(crate) fn {rule_name_snake}(checker: &mut Checker) {{}}
lines.append(line)
variant = pascal_case(linter)
rule = f"""rules::{linter.split(" ")[0]}::rules::{name}"""
lines.append(
" " * 8
+ f"""({variant}, "{code}") => (RuleGroup::Unspecified, Rule::{name}),\n""",
+ f"""({variant}, "{code}") => (RuleGroup::Unspecified, {rule}),\n""",
)
lines.sort()
text += "".join(lines)
text += "\n"
text += fp.read()
with (ROOT_DIR / "crates/ruff/src/codes.rs").open("w") as fp:
fp.write(text)