From 82d7814101551038b04692532ead147439256d41 Mon Sep 17 00:00:00 2001 From: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com> Date: Wed, 25 Jan 2023 01:45:34 +0100 Subject: [PATCH] Update .pre-commit-config.yml (#2139) --- .pre-commit-config.yaml | 4 +++- scripts/_utils.py | 5 ++--- scripts/add_plugin.py | 9 ++++----- scripts/add_rule.py | 19 +++++++++++++------ scripts/pyproject.toml | 6 ++++-- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8e86b36586..076985834d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,6 +3,8 @@ repos: rev: v0.0.233 hooks: - id: ruff + args: [--fix] + exclude: ^resources - repo: https://github.com/abravalheri/validate-pyproject rev: v0.10.1 @@ -13,6 +15,6 @@ repos: hooks: - id: cargo-fmt name: cargo fmt - entry: cargo fmt -- + entry: cargo +nightly fmt -- language: rust types: [rust] diff --git a/scripts/_utils.py b/scripts/_utils.py index 65a23e984f..5f2e8f8bb9 100644 --- a/scripts/_utils.py +++ b/scripts/_utils.py @@ -1,8 +1,7 @@ -import os import re from pathlib import Path -ROOT_DIR = Path(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +ROOT_DIR = Path(__file__).resolve().parent.parent def dir_name(linter_name: str) -> str: @@ -15,4 +14,4 @@ def pascal_case(linter_name: str) -> str: def get_indent(line: str) -> str: - return re.match(r"^\s*", line).group() # pyright: ignore[reportOptionalMemberAccess] + return re.match(r"^\s*", line).group() # type: ignore[union-attr] diff --git a/scripts/add_plugin.py b/scripts/add_plugin.py index 19b247bdc7..d83a29d863 100755 --- a/scripts/add_plugin.py +++ b/scripts/add_plugin.py @@ -10,15 +10,14 @@ Example usage: """ import argparse -import os from _utils import ROOT_DIR, dir_name, get_indent, pascal_case def main(*, plugin: str, url: str, prefix_code: str) -> None: + """Generate boilerplate for a new plugin.""" # Create the test fixture folder. - os.makedirs( - ROOT_DIR / "resources/test/fixtures" / dir_name(plugin), + (ROOT_DIR / "resources/test/fixtures" / dir_name(plugin)).mkdir( exist_ok=True, ) @@ -56,7 +55,7 @@ mod tests { } } """ - % dir_name(plugin) + % dir_name(plugin), ) # Create a subdirectory for rules and create a `mod.rs` placeholder @@ -84,7 +83,7 @@ mod tests { fp.write(f"{indent}// {plugin}") fp.write("\n") - elif line.strip() == '/// Ruff-specific rules': + elif line.strip() == "/// Ruff-specific rules": fp.write(f"/// [{plugin}]({url})\n") fp.write(f'{indent}#[prefix = "{prefix_code}"]\n') fp.write(f"{indent}{pascal_case(plugin)},") diff --git a/scripts/add_rule.py b/scripts/add_rule.py index 2e08bbc4df..8e7ee84d02 100755 --- a/scripts/add_rule.py +++ b/scripts/add_rule.py @@ -16,12 +16,17 @@ from _utils import ROOT_DIR, dir_name, get_indent def snake_case(name: str) -> str: """Convert from PascalCase to snake_case.""" - return "".join(f"_{word.lower()}" if word.isupper() else word for word in name).lstrip("_") + return "".join( + f"_{word.lower()}" if word.isupper() else word for word in name + ).lstrip("_") def main(*, name: str, code: str, linter: str) -> None: + """Generate boilerplate for a new rule.""" # Create a test fixture. - with (ROOT_DIR / "resources/test/fixtures" / dir_name(linter) / f"{code}.py").open("a"): + with (ROOT_DIR / "resources/test/fixtures" / dir_name(linter) / f"{code}.py").open( + "a", + ): pass plugin_module = ROOT_DIR / "src/rules" / dir_name(linter) @@ -35,13 +40,15 @@ def main(*, name: str, code: str, linter: str) -> None: for line in content.splitlines(): if line.strip() == "fn rules(rule_code: Rule, path: &Path) -> Result<()> {": indent = get_indent(line) - fp.write(f'{indent}#[test_case(Rule::{name}, Path::new("{code}.py"); "{code}")]') + fp.write( + f'{indent}#[test_case(Rule::{name}, Path::new("{code}.py"); "{code}")]', + ) fp.write("\n") fp.write(line) fp.write("\n") - # Add the exports + # Add the exports rules_dir = plugin_module / "rules" rules_mod = rules_dir / "mod.rs" @@ -83,14 +90,14 @@ impl Violation for %s { } } """ - % (name, name) + % (name, name), ) fp.write("\n") fp.write( f""" /// {code} pub fn {rule_name_snake}(checker: &mut Checker) {{}} -""" +""", ) fp.write("\n") diff --git a/scripts/pyproject.toml b/scripts/pyproject.toml index 9bafb4d715..7b3f265041 100644 --- a/scripts/pyproject.toml +++ b/scripts/pyproject.toml @@ -1,8 +1,10 @@ [tool.ruff] select = ["ALL"] ignore = [ - "S101", # assert-used - "PLR2004", # magic-value-comparison + "E501", # line-too-long + "INP001", # implicit-namespace-package + "PLR2004", # magic-value-comparison + "S101", # assert-used ] [tool.ruff.pydocstyle]